DKnight Posted January 3, 2022 Posted January 3, 2022 (edited) This script will add a NavMesh component to each terrain in the Terrain Loader, and build the navmesh. There is still a little tweaking to do in the settling the layers, for the navmesh baking, The baking does take some time, so for large worlds, this will take some time. This script belongs in a Editors directory. using Gaia; using System; using System.Collections; using System.Collections.Generic; using UnityEditor; using UnityEngine; using UnityEngine.AI; public class SpawnNavMesh : MonoBehaviour { [MenuItem("Tools/Add NavMesh to All Terrains")] public static void AddNavMeshtoTerrain() { Action<Terrain> action = (terrain) => AddNavMesh(terrain); GaiaUtils.CallFunctionOnDynamicLoadedTerrains(action, true ); } private static void AddNavMesh(Terrain terrain) { GameObject go = GameObject.Find(terrain.name); NavMeshSurface surface = go.GetComponent<NavMeshSurface>(); if ( surface == null ) { surface = go.AddComponent<NavMeshSurface>(); } surface.BuildNavMesh(); } } DKnight Edited January 3, 2022 by DKnight 4
Josh Posted January 4, 2022 Posted January 4, 2022 12 hours ago, DKnight said: This script will add a NavMesh component to each terrain in the Terrain Loader, and build the navmesh. There is still a little tweaking to do in the settling the layers, for the navmesh baking, The baking does take some time, so for large worlds, this will take some time. Nice stuff, would recommend doing: GameObject go = terrain.gameObject Instead of the GameObject.Find() this can be an expensive request and since you already have the terrain you can grab the gameobject right from it since it's a monobehaviour.
DKnight Posted January 4, 2022 Author Posted January 4, 2022 nice suggestion, i will modify mine. thank you
Recommended Posts