/// <summary> /// Adds the new area. /// </summary> public NavEditArea CreateArea() { NavEditArea area = new NavEditArea(); this.m_lstArea.Add(area); return(area); }
/// <summary> /// Destroy this instance. /// </summary> public void Destroy() { foreach (NavEditArea item in this.m_lstArea) { item.Destroy(); } this.m_lstArea.Clear(); this.m_cFrameArea = null; }
/// <summary> /// Adds the frame area. /// </summary> /// <returns>The frame area.</returns> public NavEditArea CreateFrameArea() { if(this.m_cFrameArea != null ) { this.m_lstArea.Remove(this.m_cFrameArea); this.m_cFrameArea.Destroy(); this.m_cFrameArea = null; } NavEditArea area = new NavEditArea(); this.m_cFrameArea = area; this.m_lstArea.Insert(0,this.m_cFrameArea); return this.m_cFrameArea; }
/// <summary> /// Draws the area. /// </summary> /// <param name="areaid">Areaid.</param> private void DrawArea(NavEditArea area, bool selectarea) { if (area == null) { return; } List <GameObject> allPoints = area.m_lstPoints; if (allPoints.Count <= 0) { return; } if (selectarea) { Gizmos.color = Color.red; } else { Gizmos.color = Color.green; } for (int i = 0; i < allPoints.Count; i++) { if (allPoints[i] == null) { NavEditAreaManager.sInstance.CheckAllPoints(); Debug.LogError("there a null in the point gameobj lst." + i); return; } else { if (i != allPoints.Count - 1) { if (allPoints[i + 1] == null) { NavEditAreaManager.sInstance.CheckAllPoints(); Debug.LogError("there a null in the point gameobj lst."); return; } Gizmos.DrawLine(allPoints[i].transform.position, allPoints[i + 1].transform.position); } else { Gizmos.DrawLine(allPoints[i].transform.position, allPoints[0].transform.position); } } } }
/// <summary> /// Adds the frame area. /// </summary> /// <returns>The frame area.</returns> public NavEditArea CreateFrameArea() { if (this.m_cFrameArea != null) { this.m_lstArea.Remove(this.m_cFrameArea); this.m_cFrameArea.Destroy(); this.m_cFrameArea = null; } NavEditArea area = new NavEditArea(); this.m_cFrameArea = area; this.m_lstArea.Insert(0, this.m_cFrameArea); return(this.m_cFrameArea); }
//========================= draw select point ======================== /// <summary> /// Draws the select point. /// </summary> private void DrawSelectPoint() { NavEditArea area = NavEditAreaManager.sInstance.GetArea(this.m_iSelGroup, this.m_iSelArea); if (area == null) { return; } if (this.m_iSelPoint < 0 || this.m_iSelPoint >= area.m_lstPoints.Count) { return; } Vector3 pos = area.m_lstPoints[this.m_iSelPoint].transform.position; pos.y += 1; Gizmos.DrawIcon(pos, "010.tif"); }
/// <summary> /// Draws the area. /// </summary> /// <param name="areaid">Areaid.</param> private void DrawArea( NavEditArea area , bool selectarea ) { if(area == null ) return; List<GameObject> allPoints = area.m_lstPoints; if (allPoints.Count <= 0) return; if ( selectarea ) Gizmos.color = Color.red; else Gizmos.color = Color.green; for (int i = 0; i < allPoints.Count; i++) { if (allPoints[i] == null) { NavEditAreaManager.sInstance.CheckAllPoints(); Debug.LogError("there a null in the point gameobj lst." +i); return; } else { if (i != allPoints.Count - 1) { if (allPoints[i + 1] == null) { NavEditAreaManager.sInstance.CheckAllPoints(); Debug.LogError("there a null in the point gameobj lst."); return; } Gizmos.DrawLine(allPoints[i].transform.position, allPoints[i + 1].transform.position); } else { Gizmos.DrawLine(allPoints[i].transform.position, allPoints[0].transform.position); } } } }
//========================== draw area ======================================== /// <summary> /// Draws all areas. /// </summary> private void DrawAllAreas() { if (this.m_bShowArea) { for (int i = 0; i < NavEditAreaManager.sInstance.m_lstAreaGroup.Count; i++) { NavEditAreaGroup group = NavEditAreaManager.sInstance.GetGroup(i); bool selectGroup = this.m_iSelGroup == i; for (int j = 0; j < group.m_lstArea.Count; j++) { NavEditArea area = group.GetArea(j); bool selectArea = false; if (selectGroup) { selectArea = this.m_iSelArea == j; } DrawArea(area, selectArea); } } } }
void OnSceneGUI() { if (Event.current == null) { return; } Event e = Event.current; if (this.m_cNavMono == null) { return; } // only in edit mode will auto add point with mouse click; if (m_eState == EditState.StateEditArea) { if (e.button == 0 && e.type == EventType.MouseDown) { NavEditAreaGroup group = NavEditAreaManager.sInstance.GetGroup(this.m_cNavMono.m_iSelGroup); if (group == null) { return; } NavEditArea area = group.GetArea(this.m_cNavMono.m_iSelArea); if (area == null) { return; } Ray ray = HandleUtility.GUIPointToWorldRay(e.mousePosition); //int layerMask = 1 << 9; RaycastHit hit; if (Physics.Raycast(ray.origin, ray.direction, out hit, Mathf.Infinity)) { Vector3 placePos = hit.point; //placePos.y = pointHeight; // generate obj GameObject point = GameObject.CreatePrimitive(PrimitiveType.Sphere); point.transform.position = placePos; point.transform.parent = m_cParent.transform; point.transform.localScale /= 5f; point.name = "point"; area.Insert(point, this.m_cNavMono.m_iSelPoint); this.m_cNavMono.m_iSelPoint++; if (this.m_cNavMono.m_iSelPoint >= area.m_lstPoints.Count) { this.m_cNavMono.m_iSelPoint = area.m_lstPoints.Count - 1; } } e.Use(); } } else if (m_eState == EditState.StateFindArea) { if (e.button == 0 && e.type == EventType.MouseDown) { Ray ray = HandleUtility.GUIPointToWorldRay(e.mousePosition); //int layerMask = 1 << 9; RaycastHit hit; if (Physics.Raycast(ray.origin, ray.direction, out hit, Mathf.Infinity)) { if (m_iIndex == 0) { sPos = hit.point; m_iIndex++; } else { m_iIndex = 0; ePos = hit.point; this.m_cNavMono.Seek(sPos, ePos); } } } } }
override public void OnInspectorGUI() { EditorGUILayout.BeginVertical(); { NavEditAreaGroup group = NavEditAreaManager.sInstance.GetGroup(this.m_cNavMono.m_iSelGroup); NavEditArea area = null; if (group != null) { area = group.GetArea(this.m_cNavMono.m_iSelArea); } //========================= groups ============================= GUILayout.BeginVertical(); { GUILayout.Label("All Groups"); groupUIPos = GUILayout.BeginScrollView(groupUIPos, GUILayout.Height(150)); { List <string> lst = new List <string>(); for (int i = 0; i < NavEditAreaManager.sInstance.m_lstAreaGroup.Count; i++) { lst.Add("Group(" + i.ToString() + ")"); } this.m_cNavMono.m_iSelGroup = GUILayout.SelectionGrid( this.m_cNavMono.m_iSelGroup, lst.ToArray(), 1); if (m_iLastSelGroup != this.m_cNavMono.m_iSelGroup) { m_iLastSelGroup = this.m_cNavMono.m_iSelGroup; this.m_cNavMono.m_iSelArea = 0; this.m_cNavMono.m_iSelPoint = 0; //FocusEditPanel(); } } GUILayout.EndScrollView(); GUILayout.BeginHorizontal(); { if (GUILayout.Button("Create Group")) { Debug.Log("create group"); NavEditAreaManager.sInstance.AddGroup(); this.m_cNavMono.m_iSelGroup = NavEditAreaManager.sInstance.m_lstAreaGroup.Count - 1; } if (GUILayout.Button("Delete Group")) { Debug.Log("delete group"); NavEditAreaManager.sInstance.RemoveGroup(this.m_cNavMono.m_iSelGroup); this.m_cNavMono.m_iSelGroup--; if (this.m_cNavMono.m_iSelGroup < 0) { this.m_cNavMono.m_iSelGroup = 0; } this.m_cNavMono.m_iSelArea = 0; this.m_cNavMono.m_iSelPoint = 0; } } GUILayout.EndHorizontal(); } GUILayout.EndVertical(); //======================== areas ======================== GUILayout.BeginVertical(); { GUILayout.Label("All Areas"); areaUIPos = GUILayout.BeginScrollView(areaUIPos, GUILayout.Height(150)); { if (group != null) { List <string> lst = new List <string>(); for (int i = 0; i < group.m_lstArea.Count; i++) { lst.Add("Area(" + i.ToString() + ")"); } this.m_cNavMono.m_iSelArea = GUILayout.SelectionGrid( this.m_cNavMono.m_iSelArea, lst.ToArray(), 1); if (m_iLastSelArea != this.m_cNavMono.m_iSelArea) { m_iLastSelArea = this.m_cNavMono.m_iSelArea; this.m_cNavMono.m_iSelPoint = 0; } // FocusEditPanel(); } } GUILayout.EndScrollView(); GUILayout.BeginHorizontal(); { GUI.enabled = group != null; if (GUILayout.Button("Create Frame Area")) { Debug.Log("create frame area"); group.CreateFrameArea(); this.m_cNavMono.m_iSelArea = 0; this.m_cNavMono.m_iSelPoint = 0; } if (GUILayout.Button("Create Area")) { Debug.Log("create area"); group.CreateArea(); this.m_cNavMono.m_iSelArea = group.m_lstArea.Count - 1; this.m_cNavMono.m_iSelPoint = 0; } if (GUILayout.Button("Delete Area")) { Debug.Log("delete area"); group.RemoveArea(this.m_cNavMono.m_iSelArea); this.m_cNavMono.m_iSelArea--; if (this.m_cNavMono.m_iSelArea < 0) { this.m_cNavMono.m_iSelArea = 0; } this.m_cNavMono.m_iSelPoint = 0; } } GUILayout.EndHorizontal(); } GUILayout.EndVertical(); //====================== points ============================== GUILayout.BeginVertical(); { GUILayout.Label("All Points"); pointUIPos = GUILayout.BeginScrollView(pointUIPos, GUILayout.Height(150)); { if (group != null && area != null) { List <string> lst = new List <string>(); for (int i = 0; i < area.m_lstPoints.Count; i++) { lst.Add("Point(" + i + ")"); } this.m_cNavMono.m_iSelPoint = GUILayout.SelectionGrid( this.m_cNavMono.m_iSelPoint, lst.ToArray(), 1); // FocusEditPanel(); } } GUILayout.EndScrollView(); GUI.enabled = m_eState != EditState.StateEditArea; if (GUILayout.Button("Editor Point", GUILayout.Height(30))) { m_eState = EditState.StateEditArea; } GUI.enabled = m_eState != EditState.StateFindArea; if (GUILayout.Button("Find Path", GUILayout.Height(30))) { m_eState = EditState.StateFindArea; } GUI.enabled = (m_eState == EditState.StateEditArea || m_eState == EditState.StateFindArea); if (GUILayout.Button("Finish Point", GUILayout.Height(30))) { m_eState = EditState.StateFinishArea; } if (GUILayout.Button("Delete Point", GUILayout.Height(30))) { Debug.Log("delete point"); if (area != null) { area.RemoveAt(this.m_cNavMono.m_iSelPoint); this.m_cNavMono.m_iSelPoint--; if (this.m_cNavMono.m_iSelPoint < 0) { this.m_cNavMono.m_iSelPoint = 0; } } } GUI.enabled = true; } GUILayout.EndVertical(); //===================== ============================ } EditorGUILayout.EndVertical(); GUILayout.Label("NavMesh"); this.m_cNavMono.m_bShowMesh = GUILayout.Toggle(this.m_cNavMono.m_bShowMesh, "NavMesh Show"); this.m_cNavMono.m_bShowArea = GUILayout.Toggle(this.m_cNavMono.m_bShowArea, "Area Show"); if (GUILayout.Button("Create NavMesh", GUILayout.Height(30))) { this.m_cNavMono.CreateNavMesh(); } GUILayout.Label("Area Group Save/Load"); GUILayout.BeginHorizontal(); { if (GUILayout.Button("Save AreaGroup", GUILayout.Height(30))) { Debug.Log("save area group"); string pathfile = EditorUtility.SaveFilePanel("Save Area Group", Application.dataPath, "map", UNWALK_EXTENSION); NavEditAreaManager.sInstance.SaveAreaGroup(pathfile); } if (GUILayout.Button("Load AreaGroup", GUILayout.Height(30))) { Debug.Log("load area group"); string pathfile = EditorUtility.OpenFilePanel("Open Area Group", Application.dataPath, UNWALK_EXTENSION); NavEditAreaManager.sInstance.LoadAreaGroup(pathfile, m_cParent); } } GUILayout.EndHorizontal(); GUILayout.Label("NavMesh Save/Load"); GUILayout.BeginHorizontal(); { if (GUILayout.Button("Save NavMesh", GUILayout.Height(30))) { Debug.Log("save navmesh"); string pathfile = EditorUtility.SaveFilePanel("Save NavMesh", Application.dataPath, "map", NAVMESH_EXTENSION); this.m_cNavMono.SaveNavMesh(pathfile); } if (GUILayout.Button("Load NavMesh", GUILayout.Height(30))) { Debug.Log("load navmesh"); string pathfile = EditorUtility.OpenFilePanel("Open NavMesh", Application.dataPath, NAVMESH_EXTENSION); this.m_cNavMono.LoadNavMesh(pathfile); } } GUILayout.EndHorizontal(); }
/// <summary> /// Loads the area. /// </summary> /// <param name="path">Path.</param> /// <param name="parent">Parent.</param> public void LoadAreaGroup( string filePath , GameObject parentPoint ) { if (parentPoint == null) { Debug.LogError("the parent is null."); return; } float pointHeight = parentPoint.transform.position.y; // check file exist if (!File.Exists(filePath)) return; RemoveAllGroup(); // open file FileStream fs = File.Open(filePath, FileMode.Open); BinaryReader binReader = new BinaryReader(fs); try { // read version string currVersion = new string(binReader.ReadChars(EDITVERSION.Length)); if (currVersion == EDITVERSION) { int areaGroupCount = binReader.ReadInt32(); for( int k = 0 ; k < areaGroupCount ; k++ ) { NavEditAreaGroup group = new NavEditAreaGroup(); // read areas count int areaCount = binReader.ReadInt32(); for (int i = 0; i < areaCount; i++) { NavEditArea area = new NavEditArea(); //read point count int pointCount = binReader.ReadInt32(); for (int j = 0; j < pointCount; j++) { // read pos float x = binReader.ReadSingle(); //float z = binReader.ReadSingle(); float z = binReader.ReadSingle(); //为了和以前的格式保存一直,这里需要y float y = binReader.ReadSingle(); // auto generate point gameobject GameObject point = GameObject.CreatePrimitive(PrimitiveType.Sphere); float height = RayHeight(x, z); point.transform.position = new UnityEngine.Vector3(x, height, z); point.transform.parent = parentPoint.transform; point.transform.localScale /= 5; area.m_lstPoints.Add(point); } //the frame area. if( i == 0 ) group.m_cFrameArea = area; group.m_lstArea.Add(area); } this.m_lstAreaGroup.Add(group); } } else { Debug.LogError("version not match"); } } catch (EndOfStreamException e) { Debug.LogError(e.Message); } finally { binReader.Close(); fs.Close(); } Debug.Log("加载数据成功!"); }
/// <summary> /// Destroy this instance. /// </summary> public void Destroy() { foreach( NavEditArea item in this.m_lstArea ) { item.Destroy(); } this.m_lstArea.Clear(); this.m_cFrameArea = null; }
/// <summary> /// Removes the frame area. /// </summary> public void RemoveFrameArea() { this.m_lstArea.Remove(this.m_cFrameArea); this.m_cFrameArea.Destroy(); this.m_cFrameArea = null; }
/// <summary> /// Adds the new area. /// </summary> public NavEditArea CreateArea() { NavEditArea area = new NavEditArea(); this.m_lstArea.Add(area); return area; }
/// <summary> /// Loads the area. /// </summary> /// <param name="path">Path.</param> /// <param name="parent">Parent.</param> public void LoadAreaGroup(string filePath, GameObject parentPoint) { if (parentPoint == null) { Debug.LogError("the parent is null."); return; } float pointHeight = parentPoint.transform.position.y; // check file exist if (!File.Exists(filePath)) { return; } RemoveAllGroup(); // open file FileStream fs = File.Open(filePath, FileMode.Open); BinaryReader binReader = new BinaryReader(fs); try { // read version string currVersion = new string(binReader.ReadChars(EDITVERSION.Length)); if (currVersion == EDITVERSION) { int areaGroupCount = binReader.ReadInt32(); for (int k = 0; k < areaGroupCount; k++) { NavEditAreaGroup group = new NavEditAreaGroup(); // read areas count int areaCount = binReader.ReadInt32(); for (int i = 0; i < areaCount; i++) { NavEditArea area = new NavEditArea(); //read point count int pointCount = binReader.ReadInt32(); for (int j = 0; j < pointCount; j++) { // read pos float x = binReader.ReadSingle(); //float z = binReader.ReadSingle(); float z = binReader.ReadSingle(); //为了和以前的格式保存一直,这里需要y float y = binReader.ReadSingle(); // auto generate point gameobject GameObject point = GameObject.CreatePrimitive(PrimitiveType.Sphere); float height = RayHeight(x, z); point.transform.position = new UnityEngine.Vector3(x, height, z); point.transform.parent = parentPoint.transform; point.transform.localScale /= 5; area.m_lstPoints.Add(point); } //the frame area. if (i == 0) { group.m_cFrameArea = area; } group.m_lstArea.Add(area); } this.m_lstAreaGroup.Add(group); } } else { Debug.LogError("version not match"); } } catch (EndOfStreamException e) { Debug.LogError(e.Message); } finally { binReader.Close(); fs.Close(); } Debug.Log("加载数据成功!"); }