/// <summary> /// 删除网格路面 /// </summary> /// <param name="center"></param> public void DestroyGridGraph(Vector3Int center) { if (dicGridGraph.TryGetValue(center, out GridGraph value)) { AstarData astarData = AstarPath.active.data; astarData.RemoveGraph(value); dicGridGraph.Remove(center); } }
/** * creates the navmesh for our grid */ private void CreateGraph() { AstarData data = AstarPath.active.data; var graphs = data.graphs.ToList(); graphs.ForEach(x => data.RemoveGraph(x)); PointGraph graph = data.AddGraph(typeof(PointGraph)) as PointGraph; AstarPath.active.Scan(graph); // Make sure we only modify the graph when all pathfinding threads are paused AstarPath.active.AddWorkItem(new AstarWorkItem(ctx => { //create the graph //first make node array PointNode[,] nodeArray = new PointNode[map.GetUpperBound(0) + 1, map.GetUpperBound(1) + 1]; for (int y = 0; y <= map.GetUpperBound(0); y++) { for (int x = 0; x <= map.GetUpperBound(1); x++) { nodeArray[x, y] = graph.AddNode((Int3) new Vector3(x, y, 0)); } } int connections = 0; //now connect nodes for (int y = 0; y <= map.GetUpperBound(0); y++) { for (int x = 0; x <= map.GetUpperBound(1); x++) { for (int i = x - 1; i <= x + 1; i++) { for (int j = y - 1; j <= y + 1; j++) { if (InBounds(nodeArray, i, j)) { nodeArray[x, y].AddConnection(nodeArray[i, j], (uint)map[x, y].MoveCost); connections++; } } } } } })); // Run the above work item immediately AstarPath.active.FlushWorkItems(); }