public void PlanUpdateInThread() { if (mNavGroup.crowd.IsDisposed) { return; } //这里进入critter的dll,然后再调用recast的dll //目前有关crowd的方法还在研究中,没有使用 mNavGroup.crowd.Update(threadUpdateTimer); for (int i = 0; i < mPlanners.Length; i++) { NavState aPlan = mPlanners[i]; if (aPlan == null || aPlan.Update()) { continue; } aPlan.Exit(); if (!aPlan.Enter()) { RemoveAgent(i); } } }
//将一个agent的计算方法加入到计算循环中去 //返回的是插入的下标,返回-1说明失败了 public int AddAgent(NavState planner, NavState mover) { if (planner == null || mover == null) { return(-1); } for (int i = 0; i < mPlanners.Length; i++) { if (mPlanners[i] == null) { if (!planner.Enter()) { break; } if (!mover.Enter()) { planner.Exit(); break; } mPlanners[i] = planner; mMovers[i] = mover; return(i); } } return(-1); }
//在Start里面进行的初始化 public bool MakeInitialize() { //NavManager.ActiveManager的获取还需要一个更好的设计 if (!enabled) { Debug.LogError(this.name + ": is not enabled "); return(false); } if (NavManager.ActiveManager == null) { Debug.LogError(this.name + ": no activeManager "); return(false); } mNavManager = NavManager.ActiveManager; //建立navAgent,这是真正用于移动的agent mNavAgent = mNavManager.CreateAgent(agentGroup, transform); mNavAgent.moveSpeed = this.moveSpeed; if (mNavAgent == null) { Debug.LogError(this.name + ": agent create failed"); enabled = false; return(false); } mNavAgent.rotation = transform.rotation; mNavAgent.position = mNavAgent.GetPointSearch(transform.position); if (mNavAgent.position.polyRef == 0) { mNavAgent = null; Debug.LogError(this.name + ": take agent into navmesh failed: " + mNavAgent.position); enabled = false; return(false); } //planner是底层计算推进(可以考虑多线程) //mover是unity位置改变,只能在主线程 planner = new NavPlanner(mNavAgent); //用于表现,也就是修改Unity中的移动的move //UnitySimpleMovePlan unityMovePlan = new UnitySimpleMovePlan(); UnityMoveWithFixdRoute unityMovePlan = new UnityMoveWithFixdRoute(); unityMovePlan.SetNavAgent(mNavAgent); unityMovePlan.yAxisFreeze = this.yAxisFreeze; mover = unityMovePlan; mManagerIndex = mNavManager.AddAgent(planner, unityMovePlan); if (mManagerIndex == -1) { Debug.LogError(this.name + ": add agent to navigation manager failed"); enabled = false; return(false); } Stop(); return(true); }
//暂停,感觉其实就是刷新一下数据 private void Suspend() { mPlanner.Exit(); mPlanner = NavState.Instance; theAgent.RemoveFromCrowd(); theAgent.SetCorridorAssets(false); theAgent.SetPathAssets(true); theAgent.desiredVelocity = Vector3.zero; theAgent.desiredSpeedSq = 0; theAgent.flags &= ~NavFlag.PlannerFailed; }
//单独刷新一个plan的外部方法(这是一种很危险的方法,尽可能不要用) public void UpdateSinglePlan(int index) { NavState aPlan = mPlanners[index]; if (aPlan == null || aPlan.Update()) { return; } aPlan.Exit(); if (!aPlan.Enter()) { RemoveAgent(index); } }
//修改当前选定的移动计划 private void TransitionState(NavState state) { mPlanner.Exit(); mPlanner = state; if (mPlanner.Enter()) { theAgent.flags &= ~NavFlag.PlannerFailed; } else { theAgent.flags |= NavFlag.PlannerFailed; //Debug.LogError(theAgent.transform.name + ": Planner transition failed: " + state); } }
//更新:寻路的计算更新循环 //这个更新是需要找一个unity上层的类来调用进行不断的更新的 public void MakeUpdate() { for (int i = 0; i < mPlanners.Length; i++) { NavState aMove = mMovers[i]; if (aMove == null || aMove.Update()) { continue; } aMove.Exit(); if (!aMove.Enter()) { RemoveAgent(i); } } }