//在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); }
public void MakeStart(ScriptableObject bakedMesh, byte [] myNavData) { try { mNavmeshData = bakedMesh; navMeshData = myNavData; /* * Debug.Log("got navMeshData version1: " + BitConverter.ToUInt32(new byte[] * { navMeshData[0] , navMeshData[1] , navMeshData[2], navMeshData[3] * } , 0)); * Debug.Log("got navMeshData version2: " + BitConverter.ToUInt32(new byte[] * { navMeshData[0] , navMeshData[1] , navMeshData[2], navMeshData[3], * navMeshData[4] , navMeshData[5] , navMeshData[6], navMeshData[7] * } , 0)); */ if (mNavmeshData == null && navMeshData == null) { Debug.Log("have not get a baked mesh , UnityAgentController shut down"); isSetup = false; } else { //Debug.Log("starting NavManager now!"); NavManager.ActiveManager = CreateManager(); //Debug.Log("NavManager.ActiveManager == "+ NavManager.ActiveManager + " after CreateManager()"); mManager = NavManager.ActiveManager; mManager.theModeNow = this.theAgentMode; mManager.StartThreadUpdate(); isSetup = true; //Debug.Log("Navigation Agent controller started!!!!!"); } } catch (Exception X) { Debug.LogError("UnityAgentController Start with error:" + X.Message + "--" + X.ToString()); isSetup = false; } }
public NavManager CreateManager() { CheckCrowdAvoidanceSet(); if (!(mNavmeshData && NavmeshData.HasNavmesh)) { Debug.LogError("Aborted initialization. Navigation mesh not available."); return(null); } //Debug.Log("NavmeshData-------"+ NavmeshData); Navmesh navmesh = NavmeshData.GetNavmesh(); if (navmesh == null) { NavStatus theStatus = Navmesh.Create(navMeshData, out navmesh); Debug.Log("Navmesh.Create ---->" + theStatus + "---->" + (int)(theStatus & NavStatus.Sucess)); if (NavUtil.Failed(theStatus)) { Debug.LogError("NavUtil.Failed(Navmesh.Create(navMeshData, out navmesh) Fail!"); } Debug.Log("--------------------\n" + navMeshData + "---" + navMeshData.Length + "\n-----------------\nNavmesh-------" + navmesh); } if (navmesh == null) { Debug.LogError(" navmesh is null"); return(null); } NavmeshQuery query; NavStatus status = NavmeshQuery.Create(navmesh, mMaxQueryNodes, out query); if ((status & NavStatus.Sucess) == 0) { Debug.LogError(" Aborted initialization. Failed query creation: " + status.ToString()); return(null); } CrowdManager crowd = CrowdManager.Create(mMaxCrowdAgents, mMaxAgentRadius, navmesh); if (crowd == null) { Debug.LogError("Aborted initialization. Failed crowd creation."); return(null); } for (int i = 0; i < CrowdManager.MaxAvoidanceParams; i++) { crowd.SetAvoidanceConfig(i, CrowdAvoidanceConfig[i]); } NavGroup mGroup = new NavGroup(navmesh, query, crowd, crowd.QueryFilter, mExtents, false); int count = AgentGroupSettingManager.GetGroupCount(); Dictionary <byte, NavAgentGroups> mAgentGroups = new Dictionary <byte, NavAgentGroups>(count); for (int i = 0; i < count; i++) { byte groupId; NavAgentGroups group = AgentGroupSettingManager.CreateAgentGroup(i, mMaxPath, mMaxStraightPath, out groupId); group.angleAt = mAngleAt; group.heightTolerance = mHeightTolerance; group.turnThreshold = mTurnThreshold; mAgentGroups.Add(groupId, group); } return(NavManager.Create(mMaxAgents, mGroup, mAgentGroups)); }