[Script] Baking navmeshes for hidden objects.

While using the new NavMesh features that came with Unity 3.5 we found a rather intresting feature. It seems that GameObjects where the Renderer is disabled arent included in the NavMesh. They arent baked at all, and the NavAgents cant find the NavMesh. After filing a bug report our assumptions where confirmed, its supposed to be this way.

The workaround for this feature is to enable all renderers, bake the NavMesh and then disable the renderers again. Luckily Unity is easily extended, which saves us a lot of work. The following script adds a menu item which does this automaticly.

using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

/// <summary>
/// Unity doesnt bake colliders without an active renderer in the navmesh. So enable the renderer, bake, then disable again.
/// </summary>
public class NavMeshFixer : ScriptableObject
{
    [MenuItem("Paladin/Fix Navigation Mesh")]
    public static void FixNavMesh()
    {        
        Undo.RegisterSceneUndo("BakeNavMesh");

        List<Renderer> disabledObjects = new List<Renderer>(); //Keep a list of old objects

        foreach (Renderer item in Object.FindObjectsOfType(typeof(Renderer))) //Loop over all renderers
        {
            //Check if its marked as NavigationStatic, and it has a disabled renderer
            if (GameObjectUtility.AreStaticEditorFlagsSet(item.gameObject, StaticEditorFlags.NavigationStatic)  
                !item.enabled) 
            {
                disabledObjects.Add(item);
                item.renderer.enabled = true; //Temporary enable it.
            }            
        }

        NavMeshBuilder.BuildNavMesh(); //Trigger the navmesh to build.

        disabledObjects.ForEach( obj => obj.enabled = false ); //Disable the objects again.

        Debug.Log(string.Format("Done building navmesh, {0} objects affected.", disabledObjects.Count));
    }
}

So the next time you want to build your mesh, and have invisible objects, use the button at Paladin/Fix Navigation Mesh :slight_smile:

2 Likes

This is incredibly useful. And to think no one has thanked you for it yet! You’re the best.

Yeah, thanks man! great job

Just found this ‘non-bug’ myself. Thank you very much for a good script.

I have worked in an editor script with the same concept.
This might help.

…The NavMesh helper is an editor script that will prepare your scene for the NavMesh baking process. As we know, the Unity’s bake process is based on renderers. So the helper generates temporary objects with renderers created from the original object’s collider

http://luigigarcia.byethost7.com/2015/08/16/navmesh-helper-baking-scene-based-on-colliders/

Hi Garcialuigi!
Im a rookie and i reading your project. This is my project:
I have two map and one character moving on it (two independent map). The maps selected by click Menu. I want to make character moving on two maps at two different times (click menu>selected map). But I can not because I only bake navigation by hand. Can you help me, please!

Besides, NavMesh was created while characters move?? Can you tell me your method? Thanks you!