public void NavMeshCanBeRemovedAndAdded() { surface.BuildNavMesh(); Assert.IsTrue(HasNavMeshAtOrigin()); surface.RemoveData(); Assert.IsFalse(HasNavMeshAtOrigin()); surface.AddData(); Assert.IsTrue(HasNavMeshAtOrigin()); }
// called by startcoroutine whenever you want to build the navmesh IEnumerator BuildNavmesh(NavMeshSurface surface) { // wait until everything has rendered yield return(new WaitForEndOfFrame()); // get the data for the surface var data = InitializeBakeData(surface); // start building the navmesh var async = surface.UpdateNavMesh(data); // wait until the navmesh has finished baking yield return(async); Debug.Log("Finished updating NavMeshSurface."); // you need to save the baked data back into the surface surface.navMeshData = data; // save the nav mesh data SaveNavMeshData(data); // call AddData() to finalize it surface.AddData(); }
public const int MAX_WALL = 7; //can change this to create a geant map // Start is called before the first frame update void Start() { float begin = Time.realtimeSinceStartup; this.seed = Mathf.FloorToInt(Random.value * int.MaxValue); Random.InitState(seed); MapGeneration(); surface.BuildNavMesh(); surface.AddData(); //Add the surface to the singleton Nav Mesh NavMeshTriangulation navMeshTriangulation = NavMesh.CalculateTriangulation(); DebugDisplayTriangle(navMeshTriangulation); MapGraph mapGraph = GenerateGraph(navMeshTriangulation); mapGraph.DeugDrawGrahp(); Debug.Log("Generation took " + (Time.realtimeSinceStartup - begin) + " seconds"); GenerateLights(mapGraph); TemporaryPlayerGeneration(mapGraph); surface.RemoveData(); //Remove the surface to the singleton NavMesh surface_enemy.BuildNavMesh(); GenerateEnemies(5, mapGraph); gameObject.BroadcastMessage("StartTheGame"); }
protected override void OnUpdate() { var commandBuffer = new EntityCommandBuffer(Allocator.TempJob); var chunks = hybridQuery.CreateArchetypeChunkArray(Allocator.TempJob); var entityType = GetArchetypeChunkEntityType(); var translationType = GetArchetypeChunkComponentType <Translation>(true); var matrixType = GetArchetypeChunkComponentType <CellSystem.MatrixComponent>(true); var localToWorldType = GetArchetypeChunkComponentType <LocalToWorld>(true); var meshType = GetArchetypeChunkSharedComponentType <RenderMesh>(); for (int c = 0; c < chunks.Length; c++) { var chunk = chunks[c]; var entities = chunk.GetNativeArray(entityType); var translations = chunk.GetNativeArray(translationType); var matrixComponents = chunk.GetNativeArray(matrixType); var localToWorldComponents = chunk.GetNativeArray(localToWorldType); for (int e = 0; e < entities.Length; e++) { Entity entity = entities[e]; float3 position = translations[e].Value; CellSystem.MatrixComponent matrix = matrixComponents[e]; float4x4 localToWorld = localToWorldComponents[e].Value; Mesh mesh = chunk.GetSharedComponentData <RenderMesh>(meshType, entityManager).mesh; GameObject sectorGameObject = GameObject.Instantiate(sectorPrefab, position, Quaternion.identity); sectorGameObject.GetComponent <MeshCollider>().sharedMesh = mesh; NavMeshSurface navMeshComponent = sectorGameObject.GetComponent <NavMeshSurface>(); NavMeshBuildSettings settings = NavMesh.GetSettingsByID(0); List <NavMeshBuildSource> sources = CreateNavMeshSource(matrix.width, navMeshComponent, mesh, localToWorld); Bounds bounds = CalculateWorldBounds(sources, position); NavMeshData data = NavMeshBuilder.BuildNavMeshData(settings, sources, bounds, position, Quaternion.identity); if (data != null) { data.name = sectorGameObject.name; navMeshComponent.RemoveData(); navMeshComponent.navMeshData = data; if (navMeshComponent.isActiveAndEnabled) { navMeshComponent.AddData(); } } commandBuffer.AddComponent <Tags.HybridGameObjectCreated>(entity, new Tags.HybridGameObjectCreated()); commandBuffer.AddSharedComponent <GameObjectComponent>(entity, new GameObjectComponent { gameObject = sectorGameObject }); } } commandBuffer.Playback(entityManager); commandBuffer.Dispose(); chunks.Dispose(); }
IEnumerator AsyncBake() { if (surface == null) { yield break; } data = InitializeBakeData(surface); operation = surface.UpdateNavMesh(data); if (operation == null) { yield break; } while (!operation.isDone) { yield return(null); } surface.RemoveData(); surface.navMeshData = data; if (surface.isActiveAndEnabled) { surface.AddData(); } operation = null; }
IEnumerator CreateTile(int polygonId, Mesh mesh, Vector3 center, Vector3 orientation, Color color, float height, SphereMap map) { yield return(new WaitForSecondsRealtime(0.0f)); GameObject meshGo = new GameObject(); MeshFilter mf2 = meshGo.AddComponent <MeshFilter>(); MeshRenderer mr2 = meshGo.AddComponent <MeshRenderer>(); mr2.sharedMaterial = new Material(this.GetComponent <MeshRenderer>().sharedMaterial);// new Material(Shader.Find("Diffuse")); mr2.sharedMaterial.color = color; // mr2.material = material; GameObject cellGo = new GameObject(); cellGo.transform.parent = this.transform; cellGo.transform.localScale = Vector3.one; //cellGo.transform.localRotation = Quaternion.identity; cellGo.transform.LookAt(center + orientation); cellGo.transform.Rotate(90, 0, 0); meshGo.transform.parent = cellGo.transform; //go2.transform.localPosition = Vector3.zero; //meshGo.transform.localRotation = Quaternion.identity; meshGo.transform.localScale = Vector3.one; meshGo.transform.localPosition = Vector3.zero; meshGo.hideFlags = HideFlags.HideInHierarchy; /* * MeshCollider collider = meshGo.AddComponent<MeshCollider>(); * collider.sharedMesh = mesh; * collider.convex = true; * collider.inflateMesh = true; */ cellGo.transform.localPosition = center; mesh.RecalculateNormals(); mesh.RecalculateBounds(); mf2.sharedMesh = mesh; bool walkable = height > 0.1f; SphereMapCell cell = cellGo.AddComponent <SphereMapCell>(); cell.PositionSpherical = FromCartesian(center); cellGo.name = "[" + (int)(cell.PositionSpherical.x * Mathf.Rad2Deg + 360) % 360 + "," + (int)(cell.PositionSpherical.y * Mathf.Rad2Deg + 360) % 360 + "]"; cell.index = polygonId; cell.sphere = map; map.cells[polygonId] = cell; cell.walkable = walkable; NavMeshSurface navSurface = cellGo.AddComponent <NavMeshSurface>(); navSurface.collectObjects = CollectObjects.Children; navSurface.defaultArea = walkable ? 0 : 1; navSurface.AddData(); navSurface.BuildNavMesh(); }
private void Awake() { GetComponent <MeshFilter>().mesh = meshz = new Mesh(); if (useCollider) { meshCollider = gameObject.AddComponent <MeshCollider>(); } if (useNavSurface) { navSurface = gameObject.AddComponent <NavMeshSurface>(); navSurface.AddData(); } meshz.name = "meshz"; }
public static AsyncOperation BuildNavMeshAsync(this NavMeshSurface surface) { surface.RemoveData(); surface.navMeshData = new NavMeshData(surface.agentTypeID) { name = surface.gameObject.name, position = surface.transform.position, rotation = surface.transform.rotation }; if (surface.isActiveAndEnabled) { surface.AddData(); } return(surface.UpdateNavMesh(surface.navMeshData)); }
private void Update() { if (operation == null) { return; } if (operation.isDone) { surface.RemoveData(); surface.navMeshData = data; if (surface.isActiveAndEnabled) { surface.AddData(); } SceneView.RepaintAll(); operation = null; } }
// called by startcoroutine whenever you want to build the navmesh IEnumerator BuildNavmesh(NavMeshSurface surface) { Debug.Log("building navmesh"); foreach (NPC_Navigation npc in NPCs) { npc.gameObject.SetActive(false); } // get the data for the surface var data = InitializeBakeData(surface); surface.RemoveData(); surface.navMeshData = null; Application.backgroundLoadingPriority = ThreadPriority.Low; // start building the navmesh asyncop = surface.UpdateNavMesh(data); // wait until the navmesh has finished baking yield return(asyncop); //Debug.Log("finished"); // you need to save the baked data back into the surface surface.navMeshData = data; // call AddData() to finalize it surface.AddData(); foreach (NPC_Navigation npc in NPCs) { npc.gameObject.SetActive(true); npc.AdjustPosition(); if (Application.isPlaying) { npc.SetNewRandomTarget(); } } }
public IEnumerator BuildNavMesh() { Debug.Log("[Level] Building nav mesh..."); // https://github.com/Unity-Technologies/NavMeshComponents/issues/97 _navMeshSurface.RemoveData(); _navMeshSurface.navMeshData = new NavMeshData(_navMeshSurface.agentTypeID) { name = _navMeshSurface.gameObject.name, position = _navMeshSurface.transform.position, rotation = _navMeshSurface.transform.rotation }; _navMeshSurface.AddData(); AsyncOperation asyncOp = _navMeshSurface.UpdateNavMesh(_navMeshSurface.navMeshData); while (!asyncOp.isDone) { yield return(null); } }
void buildTheMesh() { //sets the nav mesh settings of WRLD object/children SetNavMeshSettings(); //Loads the nav mesh data NavMeshData meshdata = (NavMeshData)AssetDatabase.LoadAssetAtPath(navMeshPath, typeof(NavMeshData)); //if the nav mesh data doesn't exist if (meshdata == null) { Debug.Log("Currently updating NavMeshSurface."); //creates a new nav mesh surface StartCoroutine(BuildNavmesh(crowdSurface)); } //if the nav mesh data exists else { //add the nav mesh data to the nav mesh crowdSurface.navMeshData = meshdata; crowdSurface.AddData(); } }
/// <summary> /// Builds the NavMesh based on current active chunks that have colliders active. /// </summary> private IEnumerator BuildNavMeshAsync() { if (currentNavmeshBuilderId != int.MaxValue) { currentNavmeshBuilderId++; } else { currentNavmeshBuilderId = 0; } int navMeshBuilderId = currentNavmeshBuilderId; IsBuildingNavmesh = true; // Get the data for the surface NavMeshData data = InitializeBakeData(); // Start building the navmesh AsyncOperation async = navMeshSurface.UpdateNavMesh(data); // Wait until the navmesh has finished baking yield return(async); if (currentNavmeshBuilderId == navMeshBuilderId) { // Remove current data navMeshSurface.RemoveData(); // Save the new data into the surface navMeshSurface.navMeshData = data; // Finalize / Apply navMeshSurface.AddData(); IsBuildingNavmesh = false; } }
// called by startcoroutine whenever you want to build the navmesh IEnumerator BuildNavmesh(NavMeshSurface surface) { // get the data for the surface var data = InitializeBakeData(surface); // start building the navmesh ao = surface.UpdateNavMesh(data); loading = true; // wait until the navmesh has finished baking yield return(ao); Debug.Log("finished"); // you need to save the baked data back into the surface surface.navMeshData = data; // call AddData() to finalize it surface.AddData(); loading = false; i.fillAmount = 0; text.SetActive(false); OnNavBake.Invoke(); }