Skip to content

Instantly share code, notes, and snippets.

@dimmduh
Created March 12, 2019 00:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dimmduh/fdace708af30e2389b27f2b6b7598b97 to your computer and use it in GitHub Desktop.
Save dimmduh/fdace708af30e2389b27f2b6b7598b97 to your computer and use it in GitHub Desktop.
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class NavMeshMerge : MonoBehaviour
{
public NavMeshMerge Instance;
public MeshFilter meshFilter;
private void Start()
{
Instance = this;
Add();
}
public NavMeshSurface mainSurface;
public NavMeshSurface[] surfaces;
public void Add()
{
var data = NavMesh.CalculateTriangulation();
var mesh = new Mesh();
mesh.vertices = data.vertices;
mesh.triangles = data.indices;
mesh.RecalculateNormals();
mesh.RecalculateBounds();
meshFilter.mesh = mesh;
foreach (var surface in surfaces)
{
surface.enabled = false;
}
//create new
var source = new NavMeshBuildSource();
source.transform = meshFilter.transform.localToWorldMatrix;
source.shape = NavMeshBuildSourceShape.Mesh;
source.sourceObject = meshFilter.mesh;
source.area = 0;
var sources = new List<NavMeshBuildSource>();
sources.Add(source);
var settings = NavMesh.GetSettingsByIndex(0);
settings.agentRadius = 0.01f;
var epochStart = new System.DateTime(1970, 1, 1, 0, 0, 0,System.DateTimeKind.Utc);
var timestamp = (System.DateTime.UtcNow - epochStart).TotalMilliseconds;
Debug.Log("start");
var newData = NavMeshBuilder.BuildNavMeshData(settings, sources, mesh.bounds, meshFilter.transform.position, meshFilter.transform.rotation);
NavMesh.AddNavMeshData(newData);
var timestampEnd = (System.DateTime.UtcNow - epochStart).TotalMilliseconds;
Debug.Log("end " + (timestampEnd - timestamp));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment