Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. The 2023.1 beta is now available for testing. To find out what's new, have a look at our 2023.1 feature highlights.
    Dismiss Notice

Using Prebaked / Prefab Navmeshes

Discussion in 'AI & Navigation Previews' started by davis252, Apr 25, 2017.

  1. davis252

    davis252

    Joined:
    Mar 17, 2015
    Posts:
    2
    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?
     
  2. davis252

    davis252

    Joined:
    Mar 17, 2015
    Posts:
    2
    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:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5. using NavMeshBuilder = UnityEngine.AI.NavMeshBuilder;
    6.  
    7. public class NavMeshUpdateOnEnable : MonoBehaviour {
    8.  
    9.     public NavMeshData m_NavMeshData;
    10.     private NavMeshDataInstance m_NavMeshInstance;
    11.  
    12.     void OnEnable () {
    13.         m_NavMeshInstance = NavMesh.AddNavMeshData(m_NavMeshData);
    14.     }
    15.  
    16.     void OnDisable () {
    17.         NavMesh.RemoveNavMeshData(m_NavMeshInstance);
    18.     }
    19. }
    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.
     
  3. Miestry

    Miestry

    Joined:
    Mar 21, 2020
    Posts:
    2
    Thanks a lot, I had problem with loading baked navmesh after starting play, Yours script help a lot :)