csharp 在Unity中使用SharpNav.dll

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 在Unity中使用SharpNav.dll相关的知识,希望对你有一定的参考价值。

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

以上是关于csharp 在Unity中使用SharpNav.dll的主要内容,如果未能解决你的问题,请参考以下文章

csharp 在Unity中拾取对象的脚本。

csharp 在Unity中启动屏幕抖动的脚本

csharp 在Unity中绘制调用批处理:场景优化

csharp ConcurrentQueue使用锁在旧的.NET项目中使用(如在Unity3D中)

Unity3D研究院之通过C#使用Advanced CSharp Messenger(五十)

csharp 从Unity生成的.sln文件中剥离设置