Using Prebaked / Prefab Navmeshes

My game has a series of ~10 different meshes I may need to switch between. I could bake a navmesh at runtime but I suppose for performance reasons I may as well pre-bake them. (specifically, I have a list that stores these 10 as NavMeshData objects). However, I cannot figure out how to switch between these prefabs at runtime. Is this possible yet?

I think I answered my own question. Here is what I did:

On each terrain GameObject that I want to turn on and off, I added this script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using NavMeshBuilder = UnityEngine.AI.NavMeshBuilder;

public class NavMeshUpdateOnEnable : MonoBehaviour {

    public NavMeshData m_NavMeshData;
    private NavMeshDataInstance m_NavMeshInstance;

    void OnEnable () {
        m_NavMeshInstance = NavMesh.AddNavMeshData(m_NavMeshData);
    }

    void OnDisable () {
        NavMesh.RemoveNavMeshData(m_NavMeshInstance);
    }
}

Then all I had to do was drag the prefab NavMesh to that object in the editor. Now coding it was as simple as setting each object to .setActive(true) and .setActive(false) and the script does the rest.

10 Likes

Thanks a lot, I had problem with loading baked navmesh after starting play, Yours script help a lot :slight_smile: