Apr
21
2017

Using SharpNav with Unity

Noticed this SharpNav repository (Advanced Pathfinding for C#) at https://github.com/Robmaister/SharpNav

Since new unity versions support .NET 4.6, this should work in unity too,
the sample script below runs, but not sure how to access the navmesh or navigation features..

Instructions:
– Download SharpNav https://github.com/Robmaister/SharpNav
– Open SharpNav-master\SharpNav-master\Source\SharpNav.sln in VisualStudio
– Right click SharpNav on the Solution Explorer, select Build *Note: this compiles with 4.5.1
– Copy ‘SharpNav.DLL ‘, ‘Newtonsoft.Json.dll’ and ‘YamlDotNet.dll’ files from SharpNav-master\SharpNav-master\Binaries\SharpNav\Debug\ folder into unity project, Plugins/ – folder
– In Unity project, select .NET 4.6 in the player settings *Need to restart unity after this
– Download example mesh and add to scene, from: https://github.com/Robmaister/SharpNav/blob/96ddd939f292a4f4e76b460c1f783cdf1e9bcf41/Source/SharpNav.Examples/nav_test.obj
– Attach script below into empty gameobject
– Drag example mesh into meshFilter field in the empty gameobject script
– Hit Play! (nothing really happens.. would have to access the navmesh, but you are on your own now..*please post comment if you get it working, i’d like to know also : )

**Note: here is older unity project for Recast/Detour library: https://github.com/unitycoder/rcdtcs

Source:


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);
}
}

view raw

SharpNavTest.cs

hosted with ❤ by GitHub


Leave a comment

Connect

Twitter View LinkedIn profile Youtube Github Join Discord Twitch Instagram BlueSky

UnityLauncherPro

Get UnityLauncherPRO and work faster with Unity Projects!
*free unity hub alternative

@unitycoder_com

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.