private void Update() { if (!isInited) { return; } if (Input.GetKeyDown(KeyCode.G)) { RequestPathfinding(new PathfindingRequest { agent = testAgent, destination = destination.position, start = testAgent.transform.position }); } int count = 0; while (_requestQueue.Count != 0 && count < FRAME_HANDLE_COUNT) { PathfindingRequest req = _requestQueue.Dequeue(); PathfindingCoroutine pathfindingCoroutine = new PathfindingCoroutine(ProcessRequest(req)); StartCoroutine(pathfindingCoroutine); count++; } }
private IEnumerator ProcessRequest(PathfindingRequest request) { NavMeshPath navmeshPath = new NavMeshPath(); Vector3 start = request.start; Vector3 destination = request.destination; NavMeshAgent agent = request.agent; Vector3[] abstractPath = new Vector3[1000]; int wayPointCount = 0; int currentSeqIndex = 0; int lastSeqIndex = -1; NavmeshTile[] activeTiles = null; GenerateAbstractPath(start, destination, ref abstractPath, ref wayPointCount, ref navmeshPath); yield return(_endOfFrame); List <TileIdentifier> passedTileList = MarkPassedNavmeshTile(ref abstractPath, ref wayPointCount); yield return(_endOfFrame); identifiers = passedTileList; while (!HasReachedDestination(agent, destination)) { // 检查当前所处区块是否变化 CheckAgentCurrentPosition(agent, activeTiles, ref currentSeqIndex); if (lastSeqIndex == currentSeqIndex) { // 仍然处于当前区块 yield return(_endOfFrame); } // 需要加载新区块 else { lastSeqIndex = currentSeqIndex; GenerateNavmeshTile(passedTileList, ref activeTiles, currentSeqIndex); yield return(_endOfFrame); GenerateTileLink(ref activeTiles); yield return(_endOfFrame); GenerateDetailedPath(agent, activeTiles, destination); yield return(_endOfFrame); TryMoveAgent(); } } for (int i = 0; i < activeTiles.Length; i++) { if (activeTiles[i] != null) { activeTiles[i].ReturnToPool(); } } yield return(_endOfFrame); }
public void RequestPathfinding(PathfindingRequest request) { _requestQueue.Enqueue(request); }