private void OnGUI() { EditorGUILayout.BeginVertical(); roadWidth = EditorGUILayout.FloatField("路宽度", roadWidth); roadHeight = EditorGUILayout.FloatField("路整体高度", roadHeight); lineCount = EditorGUILayout.IntField("点个数", lineCount); roadType = (RoadMeshType)EditorGUILayout.EnumPopup("mesh的创建方法", roadType); roadHeightType = (RoadMeshHeightType)EditorGUILayout.EnumPopup("mesh的高度获得方式", roadHeightType); if (GUILayout.Button("查找出场景Navigation烘焙的mesh", GUILayout.Height(80))) { AStarHelpEditor.FindNavigationMeshAndInstantiate(); } if (GUILayout.Button("创建中心路mesh", GUILayout.Height(80))) { CreateNodeMesh(); } if (GUILayout.Button("直接创建mesh", GUILayout.Height(80))) { AStarHelpEditor.FindNavigationMeshAndInstantiate(); CreateNodeMesh(); } EditorGUILayout.EndVertical(); }
void CreateNodeMesh() { //CopyAllNavigation(FindAllNavigationMesh()); AstarPath aStarPath = GameObject.FindObjectOfType <AstarPath>(); if (aStarPath != null) { Transform nodeRoot = aStarPath.transform.Find("NodeRoot"); if (nodeRoot) { int count = 1; //List<Vector3> offsets = new List<Vector3>() { Vector3.zero, -Vector3.left * 0.5f, Vector3.left * 0.5f, -Vector3.forward * 0.5f, Vector3.forward * 0.5f, }; List <float> offsets = new List <float>() { 0, -0.3f, 0.3f, -0.6f, 0.6f }; for (int j = 0; j < count; j++) { GameObject old = GameObject.Find("road_layer" + j.ToString()); if (old != null) { GameObject.DestroyImmediate(old); } GameObject road_layer = new GameObject("road_layer" + j.ToString()); NodeLink[] nodeLinks = nodeRoot.GetComponentsInChildren <NodeLink>(); switch (roadType) { case RoadMeshType.ROAD_BATCH: { int maxVerticesCount = 10000; List <Vector3> vertices = new List <Vector3>(); List <int> triangles = new List <int>(); for (int i = 0; i < nodeLinks.Length; i++) { if (nodeLinks[i].Start != null && nodeLinks[i].End != null) { Vector3 normal = Vector3.Cross((nodeLinks[i].End.position - nodeLinks[i].Start.position), Vector3.up).normalized; AStarHelpEditor.AddOneMesh(nodeLinks[i].Start.position + offsets[j] * normal, nodeLinks[i].End.position + offsets[j] * normal, roadWidth, lineCount, vertices, triangles, roadHeightType); if (i == nodeLinks.Length - 1 || vertices.Count > maxVerticesCount) { GameObject road = AStarHelpEditor.CreateMesh(vertices, triangles); road.transform.parent = road_layer.transform; GameObjectUtility.SetStaticEditorFlags(road, StaticEditorFlags.NavigationStatic); GameObjectUtility.SetNavMeshArea(road, 3 + j); vertices.Clear(); triangles.Clear(); } } } break; } case RoadMeshType.ROAD_SINGLE: { //单独创建mesh for (int i = 0; i < nodeLinks.Length; i++) { if (nodeLinks[i].Start != null && nodeLinks[i].End != null) { Vector3 normal = Vector3.Cross((nodeLinks[i].End.position - nodeLinks[i].Start.position), Vector3.up).normalized; GameObject road = AStarHelpEditor.CreateMesh(nodeLinks[i].Start.position + offsets[j] * normal, nodeLinks[i].End.position + offsets[j] * normal, roadWidth, lineCount, roadHeightType); road.transform.parent = road_layer.transform; GameObjectUtility.SetStaticEditorFlags(road, StaticEditorFlags.NavigationStatic); GameObjectUtility.SetNavMeshArea(road, 3 + j); } } break; } } road_layer.transform.position = new Vector3(0, roadHeight, 0); } } } UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene()); }
static void NodeLerp() { if (lerpLength <= 0) { Open(); } if (Selection.gameObjects.Length == 2) { Transform start = null; Transform end = null; { NodeLink[] links = Selection.gameObjects[0].GetComponents <NodeLink>(); for (int i = 0; i < links.Length; i++) { if (links[i].End == Selection.gameObjects[1].transform) { start = Selection.gameObjects[0].transform; end = Selection.gameObjects[1].transform; GameObject.DestroyImmediate(links[i]); } } } { NodeLink[] links = Selection.gameObjects[1].GetComponents <NodeLink>(); for (int i = 0; i < links.Length; i++) { if (links[i].End == Selection.gameObjects[0].transform) { start = Selection.gameObjects[1].transform; end = Selection.gameObjects[0].transform; GameObject.DestroyImmediate(links[i]); } } } float length = Vector3.Distance(Selection.gameObjects[0].transform.position, Selection.gameObjects[1].transform.position); if (length > lerpLength) { float floatCount = length / lerpLength; int count = Mathf.FloorToInt(length / lerpLength); //有小数的话数量加一 if (floatCount > count) { count += 1; } ////起始点结束点已经有,要差值的点数量-1 //count -= 1; if (count > 0) { if (start == null || end == null) { start = Selection.gameObjects[0].transform; end = Selection.gameObjects[1].transform; } Delete(start, end); NodeCell nodeCell = AddNodeCell(start.gameObject, end.gameObject); Transform pre = start; for (int i = 1; i < count; i++) { pre = AddLerpObj(start.transform, pre, Vector3.Lerp(start.position, end.position, (float)i / (float)count)); nodeCell.lst.Add(pre); } AddLink(end.gameObject, pre); } } AStarHelpEditor.NodeAttachFloor(); UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene()); } }