Building NavMeshes in separate thread?

Does the new NavMeshSurface (“Components for Runtime NavMesh Building”) supports asynchronous NavMesh building, or everyting must be created in Unity thread?
Is it possible to fire BuildNavMesh as async task and do not stall main thread?

Did you ever find out if there is an async operation? I know theres UpdateNavMesh() but that only works after the navmesh has already been built…

Also appears you can’t use static batching gameobjects with navmeshsurface…?

I would also like to know if this is possible, but it seems that the navmeshbuilder api (Unity - Scripting API: NavMeshBuilder) - requires using UnityEngine.AI to work!

could potentially be thread-safe depending on how used

Navmesh build is async by default but needs to be called from the main thread.

Ah think I figured it out.

Just startcoroutine and pass the navmeshsurface to the coroutine.

        // 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
            var async = surface.UpdateNavMesh(data);

            // wait until the navmesh has finished baking
            yield return async;

            Debug.Log("finished");

            // you need to save the baked data back into the surface
            surface.navMeshData = data;

            // call AddData() to finalize it
            surface.AddData();
        }

        // creates the navmesh data
        static NavMeshData InitializeBakeData(NavMeshSurface surface)
        {
            var emptySources = new List<NavMeshBuildSource>();
            var emptyBounds = new Bounds();

            return UnityEngine.AI.NavMeshBuilder.BuildNavMeshData(surface.GetBuildSettings(), emptySources, emptyBounds, surface.transform.position, surface.transform.rotation);
        }
4 Likes

No, coroutines are not seperate threads. They are run on the main thread.

5 Likes

Running a wrapper in parallel makes no sense unless BuildNaVmeshData runs in parallel
BTW :- Coroutines and Async run on main thread

you better add
surface.RemoveData();
if you need multiple rebuild navmesh

Any solution yet ? I have multiple navMeshSurfaces each for a chunk of infinite terrain, the terrain data generation works on another thread but I need to know if I can do the same with navMeshData.