Created
September 20, 2019 13:09
-
-
Save IshidaGames/26f5940484028590bdab359de4aacaee to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class SetCube : MonoBehaviour | |
{ | |
//PrefabのTargetオブジェクトを入れる | |
public GameObject cube; | |
//上から見て縦、Z軸のオブジェクトの絶対値 | |
public int vertical = 45; | |
//上から見て横、X軸のオブジェクトの絶対値 | |
public int horizontal = 45; | |
//縦一列のCubeの数 | |
public int verticalCubeNum = 3; | |
//Cubeが同じ位置に重複しないようにListを使う | |
List<int> numbers = new List<int>(); | |
void Awake() | |
{ | |
//横に順にCubeを並べて行く | |
for (int i = -1 * horizontal; i <= horizontal; i++) | |
{ | |
//縦一列並べるごとにListを空にする | |
numbers.Clear(); | |
//重複させないために縦の位置をListに入れる | |
for (int i2 = -1 * vertical; i2 <= vertical; i2++) | |
{ | |
numbers.Add(i2); | |
} | |
//縦一列に並べるごとに縦一列に並べるCubeの数を戻す | |
int verNum = verticalCubeNum; | |
//縦一列のCubeの数だけ繰り返す | |
while (verNum-- > 0) | |
{ | |
//位置をランダムに、重複しないように並べるためにListのCount使う | |
int pos = Random.Range(0, numbers.Count); | |
//Cubeを生成する | |
GameObject setCube = Instantiate(cube, | |
//生成したものを配置する位置 | |
new Vector3( | |
//X軸は横の位置 | |
i, | |
//Y軸は高さ | |
0.5f, | |
//Z軸はListから選んだ位置 | |
numbers[pos] | |
//Quaternion.identityは無回転を指定する | |
), Quaternion.identity); | |
//Hierarchyで表示される名前を横、縦の位置の数字で表す | |
setCube.name = i + " - " + numbers[pos].ToString(); | |
//使った縦の位置はListから消去して重複なくす | |
numbers.RemoveAt(pos); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment