void Start() { // Load marker prefabs GameObject yellowSpherePrefab = Resources.Load <GameObject>("YellowSphere"); GameObject redSpherePrefab = Resources.Load <GameObject>("RedSphere"); // Get a reference to Grids 2D System's API grid = Grid2D.instance; // Iterate each cell and draws a sphere on the center of the cell as well as on the vertices for (int k = 0; k < grid.cells.Count; k++) { // Add a yellow circle on the center of the cell Vector3 worldSpaceCenter = grid.CellGetPosition(k); GameObject yellowSphere = Instantiate(yellowSpherePrefab); yellowSphere.transform.position = worldSpaceCenter + Vector3.up * 0.1f; // For each vertex of the cell, add a red circle int vertexCount = grid.CellGetVertexCount(k); for (int p = 0; p < vertexCount; p++) { Vector3 worldSpacePosition = grid.CellGetVertexPosition(k, p); GameObject redSphere = Instantiate(redSpherePrefab); redSphere.transform.position = worldSpacePosition + Vector3.up * 0.1f; // note: "up" is -z (grid is x,y) - displaced to avoid z-fighting } } }
IEnumerator MoveShipAlongPath(List <int> path) { for (int k = 0; k < path.Count; k++) { Vector3 position = grid.CellGetPosition(path[k]); token.transform.position = position; yield return(new WaitForSeconds(0.1f)); } }
void ComputeNextDestination() { startTime = Time.time; startPosition = transform.position; if (positions == null || posIndex >= positions.Count) { destination = transform.position; Destroy(this); } else { destination = grid.CellGetPosition(positions [posIndex], elevation); posIndex++; } }
void OnSceneGUI() { if (grid == null || Application.isPlaying) { return; } Event e = Event.current; grid.CheckRay(HandleUtility.GUIPointToWorldRay(e.mousePosition)); if (cellHighlightedIndex != grid.cellHighlightedIndex) { cellHighlightedIndex = grid.cellHighlightedIndex; SceneView.RepaintAll(); } int controlID = GUIUtility.GetControlID(FocusType.Passive); if (e.GetTypeForControl(controlID) == EventType.MouseDown) { if (cellHighlightedIndex != cellSelectedIndex) { cellSelectedIndex = cellHighlightedIndex; if (textureMode > 0) { grid.CellToggle(cellSelectedIndex, true, Color.white, false, textureMode); SceneView.RepaintAll(); } if (cellSelectedIndex >= 0) { cellTerritoryIndex = grid.CellGetTerritoryIndex(cellSelectedIndex); cellColor = grid.CellGetColor(cellSelectedIndex); if (cellColor.a == 0) { cellColor = Color.white; } cellTextureIndex = grid.CellGetTextureIndex(cellSelectedIndex); cellTag = grid.CellGetTag(cellSelectedIndex); } EditorUtility.SetDirty(target); } } if (cellSelectedIndex >= 0 && cellSelectedIndex < grid.cells.Count) { Vector3 pos = grid.CellGetPosition(cellSelectedIndex); Handles.color = colorSelection; Handles.DrawSolidDisc(pos, grid.transform.forward, HandleUtility.GetHandleSize(pos) * 0.075f); } }
// Use this for initialization void Start() { grid = Grid2D.instance; // setup GUI resizer - only for the demo GUIResizer.Init(800, 500); labelStyle = new GUIStyle(); labelStyle.alignment = TextAnchor.MiddleLeft; labelStyle.normal.textColor = Color.white; // Configure the grid crossing cost depending on the textures for (int k = 0; k < grid.cells.Count; k++) { int textureIndex = grid.CellGetTextureIndex(k); if (textureIndex == 2) { grid.CellSetCrossCost(k, 10); } else { grid.CellSetCrossCost(k, 1); } } // Hook into cell click event to toggle start selection or draw a computed path using A* path finding algorithm grid.OnCellClick += BuildPath; // Position the token tokenRow = grid.rowCount - 1; tokenColumn = 5; Vector3 position = grid.CellGetPosition(tokenRow, tokenColumn); token.transform.position = position; // Prepare move points and show available positions movePoints = 10; }