Skip to content

Instantly share code, notes, and snippets.

@chuwilliamson
Created December 4, 2015 18:20
Show Gist options
  • Save chuwilliamson/804ef3c689f40111df4a to your computer and use it in GitHub Desktop.
Save chuwilliamson/804ef3c689f40111df4a to your computer and use it in GitHub Desktop.
Grid Gen for Shelby
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class GridGenerator : MonoBehaviour
{
[SerializeField]
private GameObject _gridPrefab;
public int rows;
public int cols;
List<GameObject> _gridList = new List<GameObject>();
[ContextMenu("Generate Grids")]
public void GenerateGrids()
{
float width = _gridPrefab.GetComponent<BoxCollider>().size.x;
float height = _gridPrefab.GetComponent<BoxCollider>().size.y;
float xPos = 0;
float yPos = 0;
float zPos = 0;
//z value
for (int i = 0; i < rows; i++)
{
zPos = i * width;
//x value
for (int j = 0; j < cols; j++)
{
xPos = j * width;
yPos = 0;
Vector3 newPos = new Vector3(xPos, yPos, zPos);
GameObject go = Instantiate(_gridPrefab, newPos, Quaternion.identity) as GameObject;
go.name = "Grid " + "[" + j + "]" + "[" + i + "]";
go.transform.parent = transform;
_gridList.Add(go);
}
}
}
[ContextMenu("Destroy Grid")]
public void DestroyGrid()
{
if (_gridList.Count == 0)
{
foreach (Transform t in transform)
{
_gridList.Add(t.gameObject);
}
foreach (GameObject go in _gridList)
{
DestroyImmediate(go);
}
}
else
{
foreach (GameObject go in _gridList)
{
DestroyImmediate(go);
}
}
_gridList = new List<GameObject>();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment