Search Unity

Limit LocalNavMeshBuilder to children only...

Discussion in 'Navigation' started by christougher, May 4, 2022.

  1. christougher

    christougher

    Joined:
    Mar 6, 2015
    Posts:
    558
    If anyone is interested here's how I modified LocalNavMeshBuilder and NavMeshSourceTag.cs to collect only children of the LocalNavMeshBuilder gameobject:

    add this to NavMeshSourceTag.cs
    Code (CSharp):
    1.  /// <summary>
    2.         /// Collect all the navmesh build sources for enabled objects tagged by this component
    3.         /// </summary>
    4.         /// <param name="sources">Collected NavMesh build sources</param>
    5.         public static void CollectChildrenOnly(GameObject thisGO, ref List<NavMeshBuildSource> sources)
    6.         {
    7.             sources.Clear();
    8.  
    9.             for (var i = 0; i < m_Meshes.Count; ++i)
    10.             {
    11.                 var mf = m_Meshes[i];
    12.                 if (mf == null)
    13.                     continue;
    14.  
    15.                 if (!mf.transform.IsChildOf(thisGO.transform))
    16.                     continue;
    17.  
    18.                     var m = mf.sharedMesh;
    19.                 if (m == null)
    20.                     continue;
    21.  
    22.                 var s = new NavMeshBuildSource();
    23.                 s.shape = NavMeshBuildSourceShape.Mesh;
    24.                 s.sourceObject = m;
    25.  
    26.                 s.transform = mf.transform.localToWorldMatrix;
    27.                 s.area = 0;
    28.                 sources.Add(s);
    29.             }
    30.  
    31.             for (var i = 0; i < m_Terrains.Count; ++i)
    32.             {
    33.                 var t = m_Terrains[i];
    34.                 if (t == null)
    35.                     continue;
    36.                 if (!t.transform.IsChildOf(thisGO.transform))
    37.                     continue;
    38.  
    39.  
    40.                 var s = new NavMeshBuildSource();
    41.                 s.shape = NavMeshBuildSourceShape.Terrain;
    42.                 s.sourceObject = t.terrainData;
    43.                 // Terrain system only supports translation - so we pass translation only to back-end
    44.                 s.transform = Matrix4x4.TRS(t.transform.position, Quaternion.identity, Vector3.one);
    45.                 s.area = 0;
    46.                 sources.Add(s);
    47.             }
    48.         }

    Add this to top of UpdateNavMesh in LocalNavMeshBuilder:
    Code (CSharp):
    1.             if (CollectChildrenOnly)
    2.                 NavMeshSourceTag.CollectChildrenOnly(thisGO, ref m_Sources);
    3.             else
    4.                 NavMeshSourceTag.Collect(ref m_Sources);
    add this to top of LocalNavMeshBuilder:
    Code (CSharp):
    1.         private GameObject thisGO;
    2.  
    3.         [SerializeField]
    4.         private bool CollectChildrenOnly= false;
    5.        
    aaaand lastly edit OnEnable() in LocalNavMeshBuilder to include:
    Code (CSharp):
    1.  
    2.  void OnEnable()
    3.         {
    4.             if (thisGO == null)
    5.                 thisGO = this.gameObject;
    6.  
    I'm no expert so if anyone has any suggestions to improve this feel free to suggestions...