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 2022.2 beta is now available for testing. To find out what's new, have a look at our 2022.2 feature highlights.
    Dismiss Notice
  3. We are updating our Terms of Service for all Unity subscription plans, effective October 13, 2022, to create a more streamlined, user-friendly set of terms. Please review them here: unity.com/legal/terms-of-service.
    Dismiss Notice
  4. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice
Dismiss Notice
Submit bug reports tagged with #Beta2022Win_Unity when you encounter unknown issues while testing the 2022.2 betas for a chance to win a year of Unity Pro. For more information, have a look at our Beta Sweepstakes Announcement.

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 :)
     
unityunity