// Use this for initialization public void Generate() { Grid.root = gameObject; if (content != null) { Clear(); } content = new Module2DArray((int)gridSize.x * (int)gridSize.y, (int)gridSize.x); Debug.Log(gridSize); Grid.root.name = "GridRoot"; moduleSize = new Vector3(1, 1, 1); for (int x = 0; x < gridSize.x; x++) { for (int y = 0; y < gridSize.y; y++) { GameObject go = Instantiate(module, new Vector3(transform.position.x + (x + 0.5f - gridSize.x / 2f) * moduleSize.x, 0, transform.position.z + (y + 0.5f - gridSize.y / 2f) * moduleSize.y), Quaternion.identity) as GameObject; go.name = "module" + (x) + " " + (y); go.GetComponent <Module> ().gridPosition.x = x; go.GetComponent <Module> ().gridPosition.y = y; content.set(x, y, go.GetComponent <Module> ()); go.transform.parent = Grid.root.transform; go.layer = 10; } } }
public void Clear() { if (content == null) { var children = new List <GameObject> (); foreach (Transform child in transform) { children.Add(child.gameObject); } children.ForEach(child => DestroyImmediate(child)); } else { for (int i = 0; i < content.Length; i++) { if (content [i] == null) { continue; } var el = content [i]; DestroyImmediate(el.gameObject); } content = null; } }
public Module2DArray cut(int top, int left, int bottom, int right) { Module2DArray ret = new Module2DArray((right + 1 - left) * (bottom + 1 - top), (right + 1 - left)); for (int i = 0; i < ret.Length; i++) { ret [i] = get(left + i % ret.width, top + (int)(i / ret.width)); } return(ret); }