Jump to content

Add NavMesh to Gaia Multi Terrains Script


DKnight

Recommended Posts

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 by DKnight
  • Like 4
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • Tell a friend

    Love Canopy - Procedural Worlds? Tell a friend!
  • Need help?

    We work with some of the biggest brands in global gaming, automotive, technology, and government to create environments, games, simulations, and product launches for desktop, mobile, and VR.

    Our unique expertise and technology enable us to deliver solutions that look and run better at a fraction of the time and cost of a typical project.

    Check out some of our non-NDA work in the Gallery, and then Contact Us to accelerate your next project!

×
×
  • Create New...