BattleFieldUtil.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections.Generic;
  4. using Mono.Xml;
  5. using System.Security;
  6. public class BattleFieldUtil : EditorWindow
  7. {
  8. private TowerTeamBornInfoPoint teamBornInfo = null;
  9. private List<TowerBattleInfo> battleFieldList = new List<TowerBattleInfo>();
  10. private List<TowerBattleInfoPoint> pointList = new List<TowerBattleInfoPoint>();
  11. public static bool s_openAddPoint = false;
  12. private GameObject teamBornGo = null;
  13. [MenuItem("RO_Tool/战场/战场区域配置")]
  14. static void CreateBattleField()
  15. {
  16. if (Application.isPlaying)
  17. {
  18. EditorUtility.DisplayDialog("无法生成", "运行中不能执行该操作。", "确定");
  19. return;
  20. }
  21. BattleFieldUtil window = (BattleFieldUtil)EditorWindow.GetWindow(typeof(BattleFieldUtil));
  22. window.minSize = new Vector2(200, 300);
  23. }
  24. private void OnEnable()
  25. {
  26. teamBornGo = GameObject.Find("TeamBorn");
  27. if (teamBornGo == null)
  28. {
  29. teamBornGo = new GameObject("TeamBorn");
  30. teamBornGo.transform.position = Vector3.zero;
  31. teamBornGo.transform.rotation = Quaternion.identity;
  32. }
  33. teamBornInfo = teamBornGo.GetComponent<TowerTeamBornInfoPoint>();
  34. if (teamBornInfo == null)
  35. teamBornInfo = teamBornGo.AddComponent<TowerTeamBornInfoPoint>();
  36. }
  37. void OnGUI()
  38. {
  39. if (GUILayout.Button("加载当前场景的出生点配置"))
  40. {
  41. LoadCfg();
  42. }
  43. else if (GUILayout.Button("保存当前场景的出生点配置"))
  44. {
  45. SaveCfg();
  46. }
  47. if (!s_openAddPoint)
  48. {
  49. if (GUILayout.Button("添加新的战场"))
  50. {
  51. s_openAddPoint = true;
  52. SceneView.onSceneGUIDelegate = OnSceneGUI;
  53. }
  54. }
  55. else
  56. {
  57. if (GUILayout.Button("关闭增加战场"))
  58. {
  59. s_openAddPoint = false;
  60. }
  61. }
  62. if (Selection.activeGameObject != null && Selection.activeGameObject.GetComponent<TowerBattleInfoPoint>() != null)
  63. {
  64. if (GUILayout.Button("删除当前选中的战场点"))
  65. {
  66. DeleteBattleField(Selection.activeGameObject);
  67. }
  68. }
  69. }
  70. TowerBattleInfoPoint CreatePointGo(TowerBattleInfo cfg, int cnt)
  71. {
  72. GameObject go = GameObject.CreatePrimitive(PrimitiveType.Capsule);
  73. go.name = "BattleField" + cnt;
  74. go.transform.localScale = new Vector3(5,5,5);
  75. TowerBattleInfoPoint point = go.AddComponent<TowerBattleInfoPoint>();
  76. point.transform.position = cfg.fieldPos;
  77. point.transform.eulerAngles = cfg.fieldRot;
  78. point.spawnDist = cfg.spawnDist;
  79. point.battleDist = cfg.battleDist;
  80. if(cfg.CamCfgList!=null)
  81. {
  82. if (point.CamCfgList == null)
  83. point.CamCfgList = new List<MapCameraConfig>();
  84. else
  85. point.CamCfgList.Clear();
  86. point.CamCfgList.AddRange(cfg.CamCfgList);
  87. }
  88. pointList.Add(point);
  89. cfg.Point = point;
  90. point.tag = "SpawnPoint";
  91. return point;
  92. }
  93. public void AddNewBattleField(Vector3 pos)
  94. {
  95. GameObject go = GameObject.CreatePrimitive(PrimitiveType.Capsule);
  96. go.name = "BattleField" + (pointList.Count + 1);
  97. go.transform.localScale = new Vector3(5, 5, 5);
  98. TowerBattleInfoPoint point = go.AddComponent<TowerBattleInfoPoint>();
  99. point.transform.position = pos;
  100. point.transform.rotation = Quaternion.identity;
  101. TowerBattleInfo newBI = new TowerBattleInfo();
  102. newBI.SaveCfg(point);
  103. battleFieldList.Add(newBI);
  104. pointList.Add(point);
  105. point.tag = "SpawnPoint";
  106. }
  107. public void DeleteBattleField(GameObject go)
  108. {
  109. if (go == null) return;
  110. TowerBattleInfoPoint point = go.GetComponent<TowerBattleInfoPoint>();
  111. if (point == null) return;
  112. RemoveCfg(point);
  113. pointList.Remove(point);
  114. GameObject.DestroyImmediate(go);
  115. }
  116. void RemoveCfg(TowerBattleInfoPoint point)
  117. {
  118. for(int idx =0; idx < battleFieldList.Count;idx++)
  119. {
  120. if(battleFieldList[idx].Point == point)
  121. {
  122. battleFieldList.RemoveAt(idx);
  123. break;
  124. }
  125. }
  126. }
  127. void LoadCfg()
  128. {
  129. string cfgFileName = "TowerBattleCfg.xml";
  130. string filePath = Application.dataPath + "/Content/Xml/" + cfgFileName;
  131. if (!FileSystem.Exists(filePath))
  132. {
  133. Debug.LogError("爬塔战场配置文件不存在");
  134. return;
  135. }
  136. TextAsset ta = AssetDatabase.LoadAssetAtPath<TextAsset>(Constants.XmlConfig + "/" + cfgFileName);
  137. if (ta == null)
  138. {
  139. Debug.LogError("配置文件有问题");
  140. return;
  141. }
  142. ClearAll();
  143. battleFieldList.Clear();
  144. pointList.Clear();
  145. SecurityParser doc = new SecurityParser();
  146. try
  147. {
  148. doc.LoadXml(ta.text);
  149. }
  150. catch (System.Exception e)
  151. {
  152. DebugHelper.Assert(false, "exception = {0}", e.Message);
  153. return;
  154. }
  155. SecurityElement root = doc.SelectSingleNode("BattleFields");
  156. if (root == null || root.Children == null) return;
  157. for (int idx = 0; idx < root.Children.Count; idx++)
  158. {
  159. var node = root.Children[idx] as SecurityElement;
  160. if(node.Tag.CompareTo("BattleField") == 0)
  161. {
  162. TowerBattleInfo battleFieldCfg = new TowerBattleInfo();
  163. battleFieldCfg.LoadCfgXml(node);
  164. battleFieldList.Add(battleFieldCfg);
  165. }
  166. else if(node.Tag.CompareTo("TeamBorn") == 0)
  167. {
  168. teamBornInfo.LoadCfgXml(node);
  169. }
  170. }
  171. for (int idx = 0; idx < battleFieldList.Count; idx++)
  172. {
  173. TowerBattleInfo cfg = battleFieldList[idx];
  174. TowerBattleInfoPoint sp = CreatePointGo(cfg, idx + 1);
  175. }
  176. }
  177. void SaveCfg()
  178. {
  179. string cfgFileName = "TowerBattleCfg.xml";
  180. string filePath = Application.dataPath + "/Content/Xml/" + cfgFileName;
  181. SecurityElement root = new SecurityElement("BattleFields");
  182. for (int idx = 0; idx < battleFieldList.Count; idx++)
  183. {
  184. TowerBattleInfo cfg = battleFieldList[idx];
  185. SecurityElement node = cfg.SaveCfgToXml();
  186. root.AddChild(node);
  187. }
  188. SecurityElement bornNode = teamBornInfo.SaveCfgToXml();
  189. root.AddChild(bornNode);
  190. SecurityTools.DumpSecurityElementToXml(root, filePath);
  191. AssetDatabase.Refresh();
  192. }
  193. void ClearAll()
  194. {
  195. for (int idx = 0; idx < pointList.Count; idx++)
  196. {
  197. if (pointList[idx] != null)
  198. GameObject.DestroyImmediate(pointList[idx].gameObject);
  199. }
  200. GameObject[] points = GameObject.FindGameObjectsWithTag("SpawnPoint");
  201. for (int idx = 0; idx < points.Length; idx++)
  202. GameObject.DestroyImmediate(points[idx]);
  203. }
  204. public static bool IsLimitSceneSelectGameObject = true;
  205. static void OnSceneGUI(SceneView sceneview)
  206. {
  207. Event e = Event.current;
  208. int controlID = GUIUtility.GetControlID(FocusType.Passive);
  209. if (IsLimitSceneSelectGameObject && e.type == EventType.Layout)
  210. {
  211. HandleUtility.AddDefaultControl(controlID);
  212. }
  213. if (IsLimitSceneSelectGameObject && s_openAddPoint)
  214. {
  215. if (e.type == EventType.MouseDown && e.button == 0)
  216. {
  217. GameObject go = GameObject.FindGameObjectWithTag("Terrain");
  218. Vector3 worldPos = GetWorldPosition(sceneview, go.transform);
  219. worldPos.y = 0;
  220. BattleFieldUtil window = (BattleFieldUtil)EditorWindow.GetWindow(typeof(BattleFieldUtil));
  221. window.AddNewBattleField(worldPos);
  222. }
  223. }
  224. }
  225. static Vector3 GetWorldPosition(SceneView sceneView, Transform parent)
  226. {
  227. Camera cam = sceneView.camera;
  228. Vector3 mousePos = Event.current.mousePosition;
  229. mousePos.z = -cam.worldToCameraMatrix.MultiplyPoint(parent.position).z;
  230. mousePos.y = cam.pixelHeight - mousePos.y;
  231. mousePos = sceneView.camera.ScreenToWorldPoint(mousePos);
  232. return mousePos;
  233. }
  234. }