运行时,动态创建NavMesh,动态Bake NavMesh, 动态烘培NavMesh
根据unity官方的demo,解析一下,发现动态创建NavMesh导航并不难.
其实只需要 网格信息,还有 需要创建的范围即可。
可以认为是固定公式了
简化写法如下,是不是需要缓存某些信息,就看具体逻辑实现了文章来源:https://www.toymoban.com/news/detail-635896.html
using UnityEngine;
using UnityEngine.AI;
using System.Collections.Generic;
using NavMeshBuilder = UnityEngine.AI.NavMeshBuilder;
public class NavMeshHelper : MonoBehaviour
{
private NavMeshDataInstance m_NavMeshDataInstance;
private void OnDestroy()
{
m_NavMeshDataInstance.Remove();
}
public void CreateNavMesh(List<Mesh> listMesh, int nSizeX, int nSizeY, int nSizeZ)
{
List<NavMeshBuildSource> listBuildSource = new List<NavMeshBuildSource>();
if (listMesh != null && listMesh.Count > 0)
{
for (int i = 0; i < listMesh.Count; i++)
{
var s = new NavMeshBuildSource();
s.shape = NavMeshBuildSourceShape.Mesh;
s.sourceObject = listMesh[i];
s.transform = Matrix4x4.identity;
s.area = 0;
listBuildSource.Add(s);
}
}
var v3Size = new Vector3(nSizeX, nSizeY, nSizeZ);
var bounds = new Bounds(Quantize(transform.position, 0.1f * v3Size), v3Size);
var nmd = new NavMeshData();
m_NavMeshDataInstance = NavMesh.AddNavMeshData(nmd);
var defaultBuildSettings = NavMesh.GetSettingsByID(0);
NavMeshBuilder.UpdateNavMeshData(nmd, defaultBuildSettings, listBuildSource, bounds);
}
private Vector3 Quantize(Vector3 v, Vector3 quant)
{
float x = quant.x * Mathf.Floor(v.x / quant.x);
float y = quant.y * Mathf.Floor(v.y / quant.y);
float z = quant.z * Mathf.Floor(v.z / quant.z);
return new Vector3(x, y, z);
}
}
程序学无止尽。
欢迎大家沟通,有啥不明确的,或者不对的,也可以和我私聊
我的QQ 334524067 神一般的狄狄文章来源地址https://www.toymoban.com/news/detail-635896.html
到了这里,关于留个档,Unity Navigation. 动态创建NavMesh,动态Bake NavMesh, 动态烘培NavMesh的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!