代碼
using UnityEngine;
public class GenerateSystem : MonoBehaviour
{
[Header("Grid Info")]
public int x;
public int y;
[Header("Block Info")]
public float gridX;
public float gridY;
public float gap;
private GameObject _parent;
private void Awake()
{
_parent = GameObject.Find("Parent");
}
void Start()
{
Chessboard();
}
void Chessboard()
{
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
Block(new Vector3( i * (gap + gridX), j * (gap + gridY), 0));
}
}
}
void Block(Vector3 position)
{
GameObject newObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
newObject.transform.SetParent(_parent.transform);
newObject.transform.localScale = new Vector3(gridX, gridY, 0.1f);
newObject.transform.position = position;
}
}