Skip to content

Instantly share code, notes, and snippets.

@eriseven
Forked from unitycoder/SharpNavTest.cs
Created July 1, 2017 14:05
Show Gist options
  • Save eriseven/dda27c63a8b5889eb1f8055aa1c3fe2d to your computer and use it in GitHub Desktop.
Save eriseven/dda27c63a8b5889eb1f8055aa1c3fe2d to your computer and use it in GitHub Desktop.
Using SharpNav.dll with Unity
using SharpNav;
using SharpNav.Geometry;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Vector3 = UnityEngine.Vector3;
public class SharpNavTest : MonoBehaviour
{
// assign your mesh here, try the example mesh provided in https://github.com/Robmaister/SharpNav/blob/96ddd939f292a4f4e76b460c1f783cdf1e9bcf41/Source/SharpNav.Examples/nav_test.obj
public MeshFilter meshFilter;
void Start()
{
// get mesh data
var vertices = meshFilter.mesh.vertices;
var indices = meshFilter.mesh.triangles;
var triangleCount = meshFilter.mesh.triangles.Length;
// convert into sharpnav Vector3 array
SharpNav.Geometry.Vector3[] navVerts = new SharpNav.Geometry.Vector3[vertices.Length];
for (int i = 0, length = vertices.Length; i < length; i++)
{
navVerts[i] = ToSharpVector(vertices[i]);
}
//prepare the geometry from your mesh data
var tris = TriangleEnumerable.FromVector3(navVerts, 0, 1, vertices.Length / 3);
// check bounds
var bounds = tris.GetBoundingBox();
Debug.DrawLine(ToUnityVector(bounds.Min.Xzy), ToUnityVector(bounds.Max.Xzy), Color.red, 99);
//use the default generation settings
var settings = NavMeshGenerationSettings.Default;
settings.AgentHeight = 1.7f;
settings.AgentRadius = 0.6f;
//generate the mesh
var navMesh = NavMesh.Generate(tris, settings);
// TODO: now what???
}
// converts sharpnav vector3 to unity vector3
Vector3 ToUnityVector(SharpNav.Geometry.Vector3 vector)
{
return new Vector3(vector.X, vector.Y, vector.Z);
}
// converts unity vector3 to sharpnav vector3
SharpNav.Geometry.Vector3 ToSharpVector(Vector3 vector)
{
return new SharpNav.Geometry.Vector3(vector.x, vector.y, vector.z);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment