/// 刷新路标点 <summary> /// 刷新路标点 /// </summary> /// <param name="all">路标点集合(父物体)</param> public void RefreshWaypoints(WaypointMessage all) { if (all.curWaypointsXMLText == null) { //置空路径 all.curWaypointsXMLPath = null; //清空数据 all.WaypointsModelAll.Clear(); //销毁子物体 DestroyChild(all.transform); } else { //获取XML文件路径 all.curWaypointsXMLPath = AssetDatabase.GetAssetPath(all.curWaypointsXMLText); //重新加载以刷新数据 all.LoadXmlToWaypoints(all, all.curWaypointsXMLPath, all.curWaypointsXMLText.text); } if (Debug.isDebugBuild) { Debug.Log("【" + all.WaypointsModelAll.Count.ToString() + "】刷新完成:" + all.curWaypointsXMLPath); } }
/// 保存路标点至XML<summary> /// 保存路标点至XML /// </summary> /// <param name="xmlFileName">xml文件名(不包含路径)</param> /// <param name="all">路标点集合(父物体)</param> public void SaveWaypointsToXml(WaypointMessage all, string xmlPath) { WaypointsXML temWaypointsXML = new WaypointsXML(); int number = 0; //xml是否存在,如果存在就删除 //否则会叠加 if (File.Exists(xmlPath)) { File.Delete(xmlPath); } foreach (Transform child in all.transform) { number++; WaypointsModel temWM = new WaypointsModel(); temWM.Index = int.Parse(child.transform.name); temWM.Position = child.position; temWM.Rotation = child.rotation; temWM.Scale = child.localScale; temWaypointsXML.AddXmlData(temWM, xmlPath); } //保存后刷新路标点 LoadXmlToWaypoints(all, xmlPath); if (Debug.isDebugBuild) { Debug.Log("【" + all.WaypointsModelAll.Count.ToString() + "】保存完成:" + all.curWaypointsXMLPath); } }
/// 加载XML至路标点 <summary> /// 加载XML至路标点 /// </summary> /// <param name="xmlFileName">xml文件名(不包含路径)</param> /// <param name="all">路标点集合(父物体)</param> public void LoadXmlToWaypoints(WaypointMessage all, string xmlPath = null, string xmlString = null) { WaypointsXML temWaypointsXML = new WaypointsXML(); WaypointsModel temWM = new WaypointsModel(); int number = 0; all.WaypointsModelAll.Clear(); //销毁子物体 DestroyChild(all.transform); //获取XML数据至集合 temWaypointsXML.GetXmlData(all.WaypointsModelAll, xmlPath, xmlString); for (int i = 0; i < all.WaypointsModelAll.Count; i++) { number++; //在编辑器中则创建 if (Application.platform == RuntimePlatform.WindowsEditor) { GameObject go = new GameObject(); go.transform.parent = all.transform; go.name = all.WaypointsModelAll[i].Index.ToString(); go.transform.position = all.WaypointsModelAll[i].Position; go.transform.rotation = all.WaypointsModelAll[i].Rotation; go.transform.localScale = all.WaypointsModelAll[i].Scale; } } if (Debug.isDebugBuild) { Debug.Log("【" + all.WaypointsModelAll.Count.ToString() + "】加载完成:" + all.curWaypointsXMLPath); } }
static void SaveWaypoints() { if (WaypointMessage.myTransform == null) { Debug.Log("保存失败:尚未创建路标点"); } else { WaypointMessage WM = WaypointMessage.myTransform.GetComponent <WaypointMessage>(); if (WM.curWaypointsXMLPath == "" || WM.curWaypointsXMLText == null) { Debug.Log("保存失败:尚未指定路标点数据文件"); } else { WM.SaveWaypointsToXml(WM, WM.curWaypointsXMLPath); } } }
static void AddWaypoint() { if (WaypointMessage.myTransform == null) { Debug.Log("添加失败:尚未创建路标点"); } else { WaypointMessage WM = WaypointMessage.myTransform.GetComponent <WaypointMessage>(); if (WM.curWaypointsXMLPath == "" || WM.curWaypointsXMLText == null) { Debug.Log("添加失败:尚未指定路标点数据文件"); } else { WM.AddWaypoint(WM); } } }
/// 添加路标点 <summary> /// 添加路标点 /// </summary> /// <param name="all">路标点集合(父物体)</param> public void AddWaypoint(WaypointMessage all) { GameObject go = new GameObject(); go.transform.parent = all.transform; Transform[] tt = all.transform.GetComponentsInChildren <Transform>(); go.name = (tt.Length - 2).ToString(); Selection.activeTransform = go.transform; //EditorApplication.ExecuteMenuItem("GameObject/Move To View"); //go.transform.rotation = new Quaternion(0f, 0f, 0f, 0f); if (all.WaypointsModelAll != null) { if (all.WaypointsModelAll.Count <= 0) { //生成的路标点移动到视窗内 EditorApplication.ExecuteMenuItem("GameObject/Move To View"); } else { Vector3 pos = all.WaypointsModelAll[all.WaypointsModelAll.Count - 1].Position + Vector3.up * 50f; Quaternion rota = all.WaypointsModelAll[all.WaypointsModelAll.Count - 1].Rotation; //生成的路标点在最后一个路标点的前方 //当前位置 = 上个路标点位置 + 上个路标点旋转 * 世界位置前 * (最大两点距离 - 3f) go.transform.position = pos + rota * Vector3.forward * (maxWaypointDis - 3f); go.transform.rotation = all.WaypointsModelAll[all.WaypointsModelAll.Count - 1].Rotation; } } WaypointsModel wm = new WaypointsModel(); wm.Index = int.Parse(go.name); wm.Position = go.transform.position; wm.Rotation = go.transform.rotation; wm.Scale = go.transform.localScale; WaypointsModelAll.Add(wm); }
static void Help() { WaypointMessage.Help(); }
/* * void OnSceneGUI() * { * //不在编辑器中则返回 * if (Application.platform != RuntimePlatform.WindowsEditor) * return; * * WM = target as WaypointMessage; * * if (WM == null) * return; * * Handles.BeginGUI(); * GUILayout.BeginArea(new Rect(10, 10, 200, 200)); * * if (WM.curWaypointsXMLPath != null) * { * if (GUILayout.Button("Save Waypoints", GUILayout.Width(150), GUILayout.Height(30))) * WM.SaveWaypointsToXml(WM, WM.curWaypointsXMLPath); * * if (GUILayout.Button("Add Waypoint", GUILayout.Width(150), GUILayout.Height(30))) * WM.AddWaypoint(WM); * } * * GUILayout.EndArea(); * Handles.EndGUI(); * }*/ public override void OnInspectorGUI() { //不在编辑器中则返回 if (Application.platform != RuntimePlatform.WindowsEditor) { return; } WM = target as WaypointMessage; if (WM == null) { return; } EditorGUILayout.Space(); GUILayout.BeginHorizontal(); //刷新路标点 if (GUILayout.Button("Refresh", GUILayout.Height(20))) { WM.RefreshWaypoints(WM); } //获取XML文件数据 WM.lastWaypointsXMLText = (TextAsset)EditorGUILayout.ObjectField(WM.curWaypointsXMLText, typeof(TextAsset), false); GUILayout.EndHorizontal(); EditorGUILayout.Space(); //上次数据与当前数据不同时则刷新数据 if (WM.lastWaypointsXMLText != WM.curWaypointsXMLText) { WM.curWaypointsXMLText = WM.lastWaypointsXMLText; WM.RefreshWaypoints(WM); } //两点间最大距离 WM.maxWaypointDis = EditorGUILayout.FloatField("Max Waypoint Dis", WM.maxWaypointDis); //线颜色 WM.lineColor = EditorGUILayout.ColorField("Line Color", WM.lineColor); //线宽度 WM.lineWidth = EditorGUILayout.FloatField("Line Width", WM.lineWidth); //显示隐藏路标点连接线 WM.showWaypoint = EditorGUILayout.Toggle("Show Waypoint", WM.showWaypoint); //显示隐藏路标点方向线 WM.showWaypointDir = EditorGUILayout.Toggle("Show Waypoint Dir", WM.showWaypointDir); //对齐地面 WM.alignGround = EditorGUILayout.Toggle("Align Ground", WM.alignGround); //离地面距离 WM.disGround = EditorGUILayout.FloatField("Dis Ground", WM.disGround); //是否绕圈 WM.isAroundCircle = EditorGUILayout.Toggle("Is Around Circle", WM.isAroundCircle); /* 以下屏蔽代码暂时不用 * showWaypoint = EditorGUILayout.Foldout(showWaypoint, "Waypoints Model All -- " + WM.WaypointsModelAll.Count.ToString()); * if (showWaypoint) * { * EditorGUI.indentLevel = 2; * for (int i = 0; i < WM.WaypointsModelAll.Count; i++) * { * WM.WaypointsModelAll[i].Show = EditorGUILayout.Foldout(WM.WaypointsModelAll[i].Show, "Waypoint " + waypoint.WaypointsModelAll[i].Index.ToString()); * if (WM.WaypointsModelAll[i].Show) * { * EditorGUILayout.Vector3Field("Position", WM.WaypointsModelAll[i].Position); * EditorGUILayout.Vector3Field("Rotation", WM.WaypointsModelAll[i].Rotation.eulerAngles); * EditorGUILayout.Vector3Field("Scale", WM.WaypointsModelAll[i].Scale); * EditorGUILayout.Space(); * } * } * }*/ EditorGUILayout.Space(); GUILayout.BeginHorizontal("Box"); EditorGUILayout.LabelField("Current Waypoints:" + WM.WaypointsModelAll.Count.ToString()); if (GUILayout.Button("Help", GUILayout.Height(20))) { WaypointMessage.Help(); } GUILayout.EndHorizontal(); EditorGUILayout.Space(); //设置已改变 EditorUtility.SetDirty(WM); }