public void AddTileGrid() { if (!Application.isPlaying) { throw new Exception("Must be on play mode"); } ClearAllGrids(); // Create a grid of GridSize by GridSize, using the cellPrefab. tileGrid = SimpleGrid.RectXY(gameObject, gridSize, gridSize, cellPrefab, cellDimensions); // Get (or add) a grid's event trigger controller to attach events to change the color of the cell when clicking it. var eventTrigger = tileGrid.AddComponent <GridEventTrigger>(); eventTrigger.UICamera = Camera.main; eventTrigger.OnLeftMouseButtonDown.AddListener(point => { tileGrid.Grid[point].GetRequiredComponent <SpriteRenderer>().color = colors.Count > 0 ? colors[0] : Color.white; } ); eventTrigger.OnRightMouseButtonDown.AddListener(point => { tileGrid.Grid[point].GetRequiredComponent <SpriteRenderer>().color = colors.Count > 0 ? colors[1] : Color.black; } ); // Colorize the grid. foreach (var point in tileGrid.Grid.Points) { tileGrid.Grid[point].GetComponent <SpriteRenderer>().color = colors[point.GetColor(colorFunction) % colors.Count]; } }
public void ClearAllGrids() { if (!Application.isPlaying) { throw new Exception("Must be on play mode"); } transform.DestroyChildrenUniversal(); DestroyUniversal(gameObject.GetComponent <GridEventTrigger>()); if (this.GetRequiredComponent <MeshFilter>().sharedMesh != null) { DestroyUniversal(this.GetRequiredComponent <MeshFilter>().sharedMesh); mesh = null; } tileGrid = null; meshGrid = null; }