static void CreatePath() { Object[] objects = Object.FindObjectsOfType(typeof(GameObject)); bool find = false; GameObject parent = null; foreach (GameObject sceneObject in objects) { if (sceneObject.name == mParentName) { parent = sceneObject; find = true; break; } } if (!find) { GameObject go = new GameObject(); go.name = mParentName; go.transform.position = Vector3.zero; go.transform.localScale = Vector3.one; go.transform.localEulerAngles = Vector3.zero; go.GetOrAddComponent <MapDraw>(); parent = go; } MapWayPoint[] mps = parent.GetComponentsInChildren <MapWayPoint>(); int count = mps == null || mps.Length == 0 ? 0 : mps.Length; GameObject node = new GameObject(); node.name = "Path_" + count; node.transform.SetParent(parent.transform); MapWayPoint mp = node.GetOrAddComponent <MapWayPoint>(); }
public static void SaveLevel() { // 场景路径 /* * string scenePath = AssetDatabase.GetAssetPath(selectObject); * * Debug.Log("====================================="); * Debug.Log(sceneName + " path : " + scenePath ); * // 打开这个关卡 * EditorApplication.OpenScene(scenePath); */ //获取场景中全部道具 Object[] objects = Object.FindObjectsOfType(typeof(GameObject)); Dictionary <string, List <string> > post = new Dictionary <string, List <string> >(); foreach (GameObject sceneObject in objects) { if (sceneObject.name == "WayPoint") { foreach (Transform child in sceneObject.transform) { MapWayPoint editor = child.GetComponent <MapWayPoint>(); if (editor != null) { if (editor.pointList.Count <= 0) { Debug.LogError("The point child is null : " + child.transform.position); } List <string> childlist = new List <string>(); for (int i = 0; i < editor.pointList.Count; i++) { if (editor.pointList[i] == null) { continue; } childlist.Add(GetPosString(editor.pointList[i].transform.position)); } post.Add(GetPosString(editor.transform.position), childlist); } } } } //保存文件 string filePath = GetDataFilePath(mapname + ".text"); byte[] byteArray = System.Text.Encoding.Default.GetBytes(JsonMapper.ToJson(post)); WriteByteToFile(byteArray, filePath); Debug.Log(JsonMapper.ToJson(post)); }
static void SavePath() { //获取场景中全部道具 Object[] objects = Object.FindObjectsOfType(typeof(GameObject)); Dictionary <string, List <string> > post = new Dictionary <string, List <string> >(); foreach (GameObject sceneObject in objects) { if (sceneObject.name == mParentName) { MapDraw mapDraw = sceneObject.GetComponent <MapDraw>(); if (mapDraw == null) { Debug.LogError("MapDraw 脚本为null"); return; } foreach (Transform child in sceneObject.transform) { MapWayPoint editor = child.GetComponent <MapWayPoint>(); if (editor != null) { if (editor.pointList.Count <= 0) { Debug.LogError("The point child is null : " + child.transform.position); } List <string> childlist = new List <string>(); childlist.Add(editor.pointList.Count.ToString()); for (int i = 0; i < editor.pointList.Count; ++i) { childlist.Add(Util.GetPosString(editor.pointList[i].position)); } List <Vector3> path = mapDraw.pathDict[editor.name]; for (int i = 0; i < path.Count; ++i) { childlist.Add(Util.GetPosString(path[i])); } post.Add(editor.name, childlist); } } } } //保存文件 string filePath = Util.GetDataFilePath(mapname + ".text"); byte[] byteArray = System.Text.Encoding.Default.GetBytes(JsonMapper.ToJson(post)); Util.WriteByteToFile(byteArray, filePath); AssetDatabase.Refresh(); Debug.Log(JsonMapper.ToJson(post)); }
public override void OnInspectorGUI() { MapWayPoint mapWayPoint = target as MapWayPoint; EditorGUILayout.PropertyField(serializedObject.FindProperty("pointList"), true); if (GUILayout.Button("+")) { Transform[] child = mapWayPoint.transform.GetComponentsInChildren <Transform>(); int count = 1; //if (child.Length == 1) // count = 2; //else // count = 1; for (int i = 0; i < count; ++i) { GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube); cube.name = "point_" + (child.Length - 1); mapWayPoint.AddPoint(cube); cube.transform.position = child[child.Length - 1].position; } } }
public void OnDrawGizmos() { #if UNITY_EDITOR Gizmos.color = Color.blue; Handles.color = Color.blue; //获取场景中全部道具 Object[] objects = Object.FindObjectsOfType(typeof(GameObject)); Dictionary <string, List <string> > post = new Dictionary <string, List <string> >(); foreach (GameObject sceneObject in objects) { if (sceneObject.name == "WayPoint") { foreach (Transform child in sceneObject.transform) { MapWayPoint editor = child.GetComponent <MapWayPoint>(); if (editor != null) { for (int i = 0; i < editor.pointList.Count; i++) { if (editor.pointList[i] == null) { continue; } editor.transform.LookAt(editor.pointList[i].transform); DrawLine(editor.transform, editor.pointList[i].transform, Vector3.Distance(editor.transform.position, editor.pointList[i].transform.position)); } } } } } #endif }
public static void LoadLevel() { List <GameObject> delarr = new List <GameObject>(); GameObject WayPoint = null; foreach (GameObject sceneObject in Object.FindObjectsOfType(typeof(GameObject))) { if (sceneObject.name == "WayPoint") { WayPoint = sceneObject; break; } } if (WayPoint == null) { GameObject tGO = new GameObject("WayPoint"); tGO.AddComponent <MapDraw>(); WayPoint = tGO; } //读取文件 byte[] pointData = ReadByteToFile(GetDataFilePath(mapname + ".text")); if (pointData == null) { return; } foreach (Transform child in WayPoint.transform) { delarr.Add(child.gameObject); } //删除旧物体 foreach (GameObject obj in delarr) { DestroyImmediate(obj); } string str = System.Text.Encoding.Default.GetString(pointData); Debug.Log(str); Dictionary <string, List <string> > post = JsonMapper.ToObject <Dictionary <string, List <string> > >(str); Dictionary <string, MapWayPoint> temp = new Dictionary <string, MapWayPoint>(); foreach (KeyValuePair <string, List <string> > pair in post) { List <string> list = pair.Value; MapWayPoint obj = GetObj(WayPoint, temp, pair.Key); for (int i = 0; i < list.Count; i++) { Debug.Log("add"); MapWayPoint child = GetObj(WayPoint, temp, list[i]); obj.pointList.Add(child.gameObject); } } // Debug.Log(JsonMapper.ToJson(post["0"][0])); }
public void OnDrawGizmos() { #if UNITY_EDITOR Gizmos.color = Color.red; //获取场景中全部道具 Object[] objects = Object.FindObjectsOfType(typeof(GameObject)); Dictionary <string, List <string> > post = new Dictionary <string, List <string> >(); foreach (GameObject sceneObject in objects) { if (sceneObject.name == mParentName) { foreach (Transform child in sceneObject.transform) { MapWayPoint editor = child.GetComponent <MapWayPoint>(); if (editor != null) { float distance = 0; if (editor.pointList == null || editor.pointList.Count == 0) { return; } for (int i = 0; i < editor.pointList.Count; ++i) { if (editor.pointList[i] == null) { editor.pointList.RemoveAt(i); return; } } for (int j = 0; j < editor.pointList.Count - 1; ++j) { distance += Vector3.Distance(editor.pointList[j].position, editor.pointList[j + 1].position); } mCout = Mathf.RoundToInt(distance / mStep); List <Vector3> list = new List <Vector3>(); foreach (Transform go in editor.pointList) { list.Add(go.transform.position); } List <Vector3> path = new List <Vector3>(); for (int k = 0; k < mCout; ++k) { Vector3 pos = Vector3.zero; Util.GenPoint(list, k, mCout, ref pos); path.Add(pos); } mPointNum = path.Count; if (!pathDict.ContainsKey(editor.name)) { pathDict.Add(editor.name, new List <Vector3>()); } pathDict[editor.name] = path; for (int i = 0; i < mPointNum - 1; i++) { if (path[i] == null) { continue; } Gizmos.DrawLine(path[i], path[i + 1]); } } } } } #endif }
public static void LoadPath() { List <GameObject> delarr = new List <GameObject>(); GameObject WayPoint = null; foreach (GameObject sceneObject in Object.FindObjectsOfType(typeof(GameObject))) { if (sceneObject.name == mParentName) { WayPoint = sceneObject; break; } } if (WayPoint == null) { GameObject tGO = new GameObject(mParentName); tGO.AddComponent <MapDraw>(); WayPoint = tGO; } //读取文件 byte[] pointData = Util.ReadByteToFile(Util.GetDataFilePath(mapname + ".text")); if (pointData == null) { return; } foreach (Transform child in WayPoint.transform) { delarr.Add(child.gameObject); } //删除旧物体 foreach (GameObject obj in delarr) { DestroyImmediate(obj); } string str = System.Text.Encoding.Default.GetString(pointData); Debug.Log(str); Dictionary <string, List <string> > post = JsonMapper.ToObject <Dictionary <string, List <string> > >(str); Dictionary <string, MapWayPoint> temp = new Dictionary <string, MapWayPoint>(); foreach (KeyValuePair <string, List <string> > pair in post) { List <string> list = pair.Value; GameObject go = new GameObject(); MapWayPoint mapWayPoint = go.GetOrAddComponent <MapWayPoint>(); go.name = pair.Key; go.transform.SetParent(WayPoint.transform); int pointCount = int.Parse(list[0]); for (int i = 0; i < pointCount; ++i) { GameObject point = GameObject.CreatePrimitive(PrimitiveType.Cube); point.name = "point_" + i; point.transform.SetParent(go.transform); point.transform.localScale = Vector3.one; point.transform.position = Util.StrintToVector3(list[i + 1]); mapWayPoint.AddPoint(point); } } }