上一章节我们已经看了怎么获得NavMesh数据保存为obj
这一章节我们来读取NavMesh数据
首先我们要定义两个结构体
点 和 三角形
为什么不用unity自带的Vector3呢?
相信你们应该已经知道 我们的寻路不能使用浮点运算
这时候我们就要确定一个精度
我这里设置为10000
namespace AStar { public class AStarConfig { /// <summary> /// 精度 /// </summary> public const int precision = 10000; } }
点结构体:
using UnityEngine; namespace AStar { /// <summary> /// 点. /// </summary> public struct AStarPonit { public int x; public int y; public int z; /// <summary> /// 构造函数 /// </summary> /// <param name="point"></param> public AStarPonit(Vector3 point) { x = (int)(point.x * AStarConfig.precision); y = (int)(point.y * AStarConfig.precision); z = (int)(point.z * AStarConfig.precision); } } }
三角形结构体:
namespace AStar { /// <summary> /// 三角形 /// </summary> public struct AStarTriangle { //三角形的三个点坐标 public AStarPonit a; public AStarPonit b; public AStarPonit c; /// <summary> /// 三角形重心 /// </summary> public AStarPonit centroid; /// <summary> /// 构造函数 /// </summary> public AStarTriangle(AStarPonit a, AStarPonit b, AStarPonit c) { this.a = a; this.b = b; this.c = c; //计算重心 centroid.x = (a.x + b.x + c.x) / 3; centroid.y = (a.y + b.y + c.y) / 3; centroid.z = (a.z + b.z + c.z) / 3; } } }
我们需要一个类来保存导航网格信息方便 方便算法使用数据
using System.Collections.Generic; namespace AStar { /// <summary> /// 导航网格信息 /// </summary> public class NavMeshInfo { /// <summary> /// 所有的三角形 /// </summary> public List<AStarTriangle> allTriangle = new List<AStarTriangle>(); /// <summary> /// 所有的重心 /// </summary> public List<AStarPonit> allCentroid = new List<AStarPonit>(); /// <summary> /// 三角形索引 key点 .value点构成的所有三角形 正常情况为三个 /// </summary> public Dictionary<AStarPonit, List<AStarTriangle>> pointIndexes = new Dictionary<AStarPonit, List<AStarTriangle>>(); } }
读取导航网格信息代码:
using UnityEngine; using System.IO; using System.Collections.Generic; namespace AStar { public class NavMeshLoad { List<Vector3> allPoint = new List<Vector3>(); /// <summary> /// 读取导航网格信息 /// </summary> /// <param name="path">路径</param> public NavMeshInfo Load(string path) { List<string> fileInfo = LoadFile(path); NavMeshInfo navMeshInfo = ReadInfo(fileInfo); return navMeshInfo; } /// <summary> /// 读取文件 /// </summary> /// <param name="path">路径</param> List<string> LoadFile(string path) { StreamReader sr = new StreamReader(path); string line; List<string> fileInfo = new List<string>(); while ((line = sr.ReadLine()) != null) { //一行一行的读取 //将每一行的内容存入数组链表容器中 fileInfo.Add(line); } //关闭流 sr.Close(); //销毁流 sr.Dispose(); //将数组链表容器返回 return fileInfo; } /// <summary> /// 读取数据 /// </summary> /// <param name="fileInfo">文件信息</param> /// <returns></returns> NavMeshInfo ReadInfo(List<string> fileInfo) { NavMeshInfo navMeshInfo = new NavMeshInfo(); for (int i = 0; i < fileInfo.Count; i++) { string str = fileInfo[i]; string[] Split = str.Split(' '); if (Split[0] == "v") { allPoint.Add(new Vector3(float.Parse(Split[2]), float.Parse(Split[3]), float.Parse(Split[4]))); } else if (Split[0] == "f") { int a = int.Parse(Split[1]); int b = int.Parse(Split[2]); int c = int.Parse(Split[3]); Vector3 aa = allPoint[a - 1]; Vector3 bb = allPoint[b - 1]; Vector3 cc = allPoint[c - 1]; AStarPonit aaa = new AStarPonit(aa); AStarPonit bbb = new AStarPonit(bb); AStarPonit ccc = new AStarPonit(cc); AStarTriangle triangle = new AStarTriangle(aaa, bbb, ccc); navMeshInfo.allCentroid.Add(triangle.centroid); navMeshInfo.allTriangle.Add(triangle); AddPointIndexes(navMeshInfo.pointIndexes, aaa, triangle); AddPointIndexes(navMeshInfo.pointIndexes, bbb, triangle); AddPointIndexes(navMeshInfo.pointIndexes, ccc, triangle); } } return navMeshInfo; } /// <summary> /// 添加顶点索引 /// </summary> /// <param name="navMeshInfo"></param> /// <param name="ponit"></param> /// <param name="triangle"></param> void AddPointIndexes(Dictionary<AStarPonit, List<AStarTriangle>> navMeshInfo, AStarPonit ponit, AStarTriangle triangle) { if (navMeshInfo.ContainsKey(ponit)) { navMeshInfo[ponit].Add(triangle); } else { List<AStarTriangle> list = new List<AStarTriangle>(); list.Add(triangle); navMeshInfo.Add(ponit, list); } } } }
我们来写个测试代码看下是否能获得:
using UnityEngine; using AStar; public class test : MonoBehaviour { void Start() { NavMeshLoad navMeshLoad = new NavMeshLoad(); NavMeshInfo navMeshInfo = navMeshLoad.Load(Application.dataPath + "/AStar/obj/test.obj"); } }
断点后 我们看navMeshInfo变量 已经可以看到有数据了
链接: https://pan.baidu.com/s/1Y3BDlp506x7_diq7_d48zw 密码: ihvh