private void Awake() { Current = this; NavMesh.avoidancePredictionTime = 0; NavMeshSurface = gameObject.AddComponent <NavMeshSurface>(); NavMeshSurface.buildHeightMesh = true; NavMeshSurface.BuildNavMesh(); }
void Start() { nmg = FindObjectOfType <NavmeshGenerator>(); foreach (GameObject go in models) { go.SetActive(false); } models[currIndex].SetActive(true); nmg.meshFilter = models[currIndex].GetComponent <MeshFilter>(); if (nmg.meshFilter == null) { nmg.meshFilter = models[currIndex].GetComponentInChildren <MeshFilter>(); } Debug.Assert(nmg.meshFilter != null, "No mesh filter found"); }
void OnDrawGizmos() { if (m_mat == null) { m_mat = new Material(Shader.Find("Unlit/Color")); } List <List <IntPoint> > walkablePolygons = new List <List <IntPoint> >() { new List <IntPoint>() { new IntPoint(10, 10), new IntPoint(600, 10), new IntPoint(600, 400), new IntPoint(10, 400), } }; List <List <IntPoint> > blockedPolygons = new List <List <IntPoint> >() { new List <IntPoint>() { new IntPoint(100, 160), new IntPoint(150, 160), new IntPoint(200, 200), new IntPoint(130, 300), }, new List <IntPoint>() { new IntPoint(220, 50), new IntPoint(260, 50), new IntPoint(260, 100), new IntPoint(220, 100), }, new List <IntPoint>() { new IntPoint(300, 150), new IntPoint(350, 150), new IntPoint(350, 200), new IntPoint(300, 200), }, new List <IntPoint>() { new IntPoint(400, 250), new IntPoint(450, 250), new IntPoint(450, 300), new IntPoint(400, 300), }, }; List <DelaunayTriangle> triangles; var nodes = NavmeshGenerator.Generate(walkablePolygons, blockedPolygons, out triangles); //设置起点和终点 DelaunayTriangle start = null, end = null; GraphNode startNode = null, endNode = null; float minX = float.MaxValue, maxX = 0; for (int i = 0; i < triangles.Count; i++) { var t = triangles[i]; var centroid = t.Centroid(); if (centroid.Xf < minX) { minX = centroid.Xf; start = t; startNode = nodes[i]; } if (centroid.Xf > maxX) { maxX = centroid.Xf; end = t; endNode = nodes[i]; } //鼠标所在三角形为终点 if (IsTriangleContains(t, Input.mousePosition)) { maxX = float.MaxValue; end = t; endNode = nodes[i]; } } GraphAStar algo = new GraphAStar(startNode, endNode); algo.Process(); m_mat.SetColor("_Color", Color.black); for (int i = 0; i < blockedPolygons.Count; i++) { DrawPolygon(blockedPolygons[i], true, true); } m_mat.SetColor("_Color", Color.white); for (int i = 0; i < triangles.Count; i++) { DrawTriangle(triangles[i], false); } m_mat.SetColor("_Color", Color.yellow); DrawTriangle(start, false); DrawTriangle(end, false); List <Vector2> path = new List <Vector2>(); var node = endNode; while (node != null) { path.Add(node.Center); node = node.Parent; } m_mat.SetColor("_Color", Color.red); GraphicsTool.DrawPolygon(path, m_mat, false); }
void Start() { nmg = FindObjectOfType <NavmeshGenerator>(); }