BattleLevelUtil.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEditor.SceneManagement;
  6. using Mono.Xml;
  7. using System.Security;
  8. public class BattleLevelUtil : EditorWindow
  9. {
  10. private List<SpawnPointCfg> spawnPointCfgList = new List<SpawnPointCfg>();
  11. private List<SpawnPoint> spawnPointList = new List<SpawnPoint>();
  12. public static string EditorLevelName
  13. {
  14. get
  15. {
  16. string levelName = EditorSceneManager.GetActiveScene().name;
  17. return levelName;
  18. }
  19. }
  20. public static bool s_openAddPoint = false;
  21. [MenuItem("RO_Tool/战场/出生点配置")]
  22. static void RecreateBorn()
  23. {
  24. if (Application.isPlaying)
  25. {
  26. EditorUtility.DisplayDialog("无法生成", "运行中不能执行该操作。", "确定");
  27. return;
  28. }
  29. BattleLevelUtil window = (BattleLevelUtil)EditorWindow.GetWindow(typeof(BattleLevelUtil));
  30. window.minSize = new Vector2(200, 300);
  31. }
  32. void OnGUI()
  33. {
  34. if(GUILayout.Button("加载当前场景的出生点配置"))
  35. {
  36. LoadCfg();
  37. }else if(GUILayout.Button("保存当前场景的出生点配置"))
  38. {
  39. SaveCfg();
  40. }
  41. if(!s_openAddPoint)
  42. {
  43. if (GUILayout.Button("开启增加出生点"))
  44. {
  45. s_openAddPoint = true;
  46. SceneView.onSceneGUIDelegate = OnSceneGUI;
  47. }
  48. }
  49. else
  50. {
  51. if (GUILayout.Button("关闭增加出生点"))
  52. {
  53. s_openAddPoint = false;
  54. }
  55. }
  56. if(Selection.activeGameObject!=null && Selection.activeGameObject.GetComponent<SpawnPoint>()!=null)
  57. {
  58. if(GUILayout.Button("删除当前选中的出生点"))
  59. {
  60. DeleteSpawnPoint(Selection.activeGameObject);
  61. }
  62. }
  63. }
  64. public static bool IsLimitSceneSelectGameObject = true;
  65. static void OnSceneGUI(SceneView sceneview)
  66. {
  67. Event e = Event.current;
  68. int controlID = GUIUtility.GetControlID(FocusType.Passive);
  69. if (IsLimitSceneSelectGameObject && e.type == EventType.Layout)
  70. {
  71. HandleUtility.AddDefaultControl(controlID);
  72. }
  73. if (IsLimitSceneSelectGameObject && s_openAddPoint)
  74. {
  75. if(e.type == EventType.MouseDown && e.button == 0)
  76. {
  77. GameObject go = GameObject.FindGameObjectWithTag("Terrain");
  78. Vector3 worldPos = GetWorldPosition(sceneview, go.transform);
  79. worldPos.y = 0;
  80. BattleLevelUtil window = (BattleLevelUtil)EditorWindow.GetWindow(typeof(BattleLevelUtil));
  81. window.AddNewSpawnPoint(worldPos);
  82. }
  83. }
  84. }
  85. static Vector3 GetWorldPosition(SceneView sceneView,Transform parent)
  86. {
  87. Camera cam = sceneView.camera;
  88. Vector3 mousePos = Event.current.mousePosition;
  89. mousePos.z = -cam.worldToCameraMatrix.MultiplyPoint(parent.position).z;
  90. mousePos.y = cam.pixelHeight - mousePos.y;
  91. mousePos = sceneView.camera.ScreenToWorldPoint(mousePos);
  92. return mousePos;
  93. }
  94. //添加一个新的点
  95. public void AddNewSpawnPoint(Vector3 pos)
  96. {
  97. GameObject go = new GameObject("MonsterSpawnPoint" + (spawnPointList.Count+1));
  98. SpawnPoint sp = go.AddComponent<SpawnPoint>();
  99. sp.type = SpawnPointType.Monster;
  100. sp.transform.position = pos;
  101. sp.transform.rotation = Quaternion.identity;
  102. GameObject child = new GameObject("p1");
  103. child.transform.SetParent(sp.transform);
  104. child.transform.localEulerAngles = Vector3.zero;
  105. child.transform.localPosition = Vector3.forward * 1.0f + Vector3.right * -1.0f;
  106. child = new GameObject("p2");
  107. child.transform.SetParent(sp.transform);
  108. child.transform.localEulerAngles = Vector3.zero;
  109. child.transform.localPosition = Vector3.forward * 1.0f;
  110. child = new GameObject("p3");
  111. child.transform.SetParent(sp.transform);
  112. child.transform.localEulerAngles = Vector3.zero;
  113. child.transform.localPosition = Vector3.forward * 1.0f + Vector3.right * 1.0f;
  114. child = new GameObject("p4");
  115. child.transform.SetParent(sp.transform);
  116. child.transform.localEulerAngles = Vector3.zero;
  117. child.transform.localPosition = Vector3.forward * -1.0f + Vector3.right * -1.0f;
  118. child = new GameObject("p5");
  119. child.transform.SetParent(sp.transform);
  120. child.transform.localEulerAngles = Vector3.zero;
  121. child.transform.localPosition = Vector3.forward * -1.0f;
  122. child = new GameObject("p6");
  123. child.transform.SetParent(sp.transform);
  124. child.transform.localEulerAngles = Vector3.zero;
  125. child.transform.localPosition = Vector3.forward * -1.0f + Vector3.right * 1.0f;
  126. sp.SavePoint();
  127. SpawnPointCfg spCfg = new SpawnPointCfg();
  128. spCfg.SaveCfg(sp);
  129. spawnPointList.Add(sp);
  130. spawnPointCfgList.Add(spCfg);
  131. sp.tag = "SpawnPoint";
  132. }
  133. //删除一个新的点
  134. void DeleteSpawnPoint(GameObject spGo)
  135. {
  136. if (spGo == null) return;
  137. SpawnPoint sp = spGo.GetComponent<SpawnPoint>();
  138. if (sp == null) return;
  139. RemoveCfg(sp);
  140. spawnPointList.Remove(sp);
  141. GameObject.DestroyImmediate(spGo);
  142. }
  143. void RemoveCfg(SpawnPoint sp)
  144. {
  145. for(int idx =0; idx < spawnPointCfgList.Count;idx++)
  146. {
  147. if(spawnPointCfgList[idx].CurrentSpawnPoint == sp)
  148. {
  149. spawnPointCfgList.RemoveAt(idx);
  150. break;
  151. }
  152. }
  153. }
  154. void ClearAll()
  155. {
  156. for(int idx =0; idx < spawnPointList.Count;idx++)
  157. {
  158. if(spawnPointList[idx]!=null)
  159. GameObject.DestroyImmediate(spawnPointList[idx].gameObject);
  160. }
  161. GameObject[] spGoes = GameObject.FindGameObjectsWithTag("SpawnPoint");
  162. for (int idx = 0; idx < spGoes.Length; idx++)
  163. GameObject.DestroyImmediate(spGoes[idx]);
  164. }
  165. void LoadCfg()
  166. {
  167. string cfgFileName = "Born_" + EditorLevelName + ".xml";
  168. string filePath = Application.dataPath + "/Content/Xml/" + cfgFileName;
  169. if (!FileSystem.Exists(filePath))
  170. {
  171. Debug.LogError("未生成过场景的出生点配置文件");
  172. return;
  173. }
  174. TextAsset ta = AssetDatabase.LoadAssetAtPath<TextAsset>(Constants.XmlConfig+"/"+cfgFileName);
  175. if(ta == null)
  176. {
  177. Debug.LogError("配置文件有问题");
  178. return;
  179. }
  180. ClearAll();
  181. spawnPointCfgList.Clear();
  182. spawnPointList.Clear();
  183. SecurityParser doc = new SecurityParser();
  184. try
  185. {
  186. doc.LoadXml(ta.text);
  187. }
  188. catch (System.Exception e)
  189. {
  190. DebugHelper.Assert(false, "exception = {0}", e.Message);
  191. return;
  192. }
  193. SecurityElement root = doc.SelectSingleNode("SpawnPoints");
  194. if (root == null || root.Children == null) return;
  195. for (int idx = 0; idx < root.Children.Count; idx++)
  196. {
  197. var spNode = root.Children[idx] as SecurityElement;
  198. SpawnPointCfg spCfg = new SpawnPointCfg();
  199. spCfg.LoadCfgXml(spNode);
  200. spawnPointCfgList.Add(spCfg);
  201. }
  202. for(int idx =0; idx < spawnPointCfgList.Count;idx++)
  203. {
  204. SpawnPointCfg spCfg = spawnPointCfgList[idx];
  205. SpawnPoint sp = CreateSpawnPointGo(spCfg, idx + 1);
  206. }
  207. }
  208. void SaveCfg()
  209. {
  210. string cfgFileName = "Born_" + EditorLevelName + ".xml";
  211. string filePath = Application.dataPath + "/Content/Xml/" + cfgFileName;
  212. SecurityElement root = new SecurityElement("SpawnPoints");
  213. for(int idx =0; idx < spawnPointCfgList.Count;idx++)
  214. {
  215. SpawnPointCfg cfg = spawnPointCfgList[idx];
  216. SecurityElement spNode = cfg.SaveCfgToXml();
  217. root.AddChild(spNode);
  218. }
  219. SecurityTools.DumpSecurityElementToXml(root, filePath);
  220. AssetDatabase.Refresh();
  221. }
  222. SpawnPoint CreateSpawnPointGo(SpawnPointCfg cfg, int cnt)
  223. {
  224. GameObject go = new GameObject("MonsterSpawnPoint" + cnt);
  225. SpawnPoint sp = go.AddComponent<SpawnPoint>();
  226. sp.type = cfg.PointType;
  227. sp.transform.position = cfg.Pos;
  228. sp.transform.eulerAngles = cfg.Rot;
  229. sp.fActorReadyDist = cfg.ActorReadyDist;
  230. sp.fHorSpace = cfg.HorSpace;
  231. sp.fVerSpace = cfg.VerSpace;
  232. for(int idx =0; idx < cfg.PointList.Count;idx++)
  233. {
  234. MonsterSpawnLoc pLoc = cfg.PointList[idx];
  235. GameObject child = new GameObject("p" +(idx+1));
  236. child.transform.SetParent(sp.transform);
  237. child.transform.eulerAngles = pLoc.rot;
  238. child.transform.position = pLoc.pos;
  239. sp.npcIds.Add(pLoc.npcId);
  240. }
  241. if(cfg.PointType == SpawnPointType.Boss)
  242. {
  243. GameObject child = new GameObject("transfer");
  244. child.transform.SetParent(sp.transform);
  245. child.transform.eulerAngles = cfg.TransferRot;
  246. child.transform.position = cfg.TransferPos;
  247. sp.camPos = cfg.BossCamPos;
  248. sp.camRot = cfg.BossCamRot;
  249. sp.camFar = cfg.CamFar;
  250. }
  251. sp.SavePoint();
  252. spawnPointList.Add(sp);
  253. cfg.CurrentSpawnPoint = sp;
  254. sp.tag = "SpawnPoint";
  255. return sp;
  256. }
  257. }