BattleTargetEditor.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using UnityEditor;
  6. using System;
  7. using James.Util;
  8. using System.Text;
  9. namespace JamesGame
  10. {
  11. public static class BattleTargetEditor
  12. {
  13. public static void DrawBattleTargetEditorGUI(this BattleFieldSceneCreator battleEditor)
  14. {
  15. //
  16. if (targetDataChanged)
  17. {
  18. _targetResNames = null;
  19. _targetCtrlAnimNames = null;
  20. }
  21. if (currTargetDataId == -1)
  22. {
  23. if (targetDataIds.Length > 1)
  24. {
  25. currTargetDataId = targetDataIds[1];
  26. }
  27. else
  28. {
  29. currTargetDataId = targetDataIds[0];
  30. }
  31. }
  32. //
  33. GUILayout.BeginHorizontal();
  34. GUILayout.Label("ID:");
  35. var tmpTargetDataId = EditorGUILayout.IntPopup(currTargetDataId, targetDataStrIds, targetDataIds);
  36. if (!ConfigEditor.targetDataCfgs.ContainsKey(tmpTargetDataId))
  37. {
  38. tmpTargetDataId = targetDataIds[1];
  39. }
  40. if (tmpTargetDataId != currTargetDataId)
  41. {
  42. targetDataChanged = true;
  43. }
  44. currTargetDataId = tmpTargetDataId;
  45. var targetData = ConfigEditor.targetDataCfgs[currTargetDataId];
  46. if (targetDataChanged)
  47. {
  48. //
  49. if (targetData.ResourceName.IndexOf("#") > 0)
  50. {
  51. string[] targetExInfo = targetData.ResourceName.Split('#');
  52. if (targetExInfo.Length == 3)
  53. {
  54. targetIsObstacle = true;
  55. targetResNameIdx = IndexOfArray<string>(targetResNames, targetExInfo[0]);
  56. targetStateMachineIdx = IndexOfArray<string>(targetStateMachineNames, targetExInfo[1]);
  57. obstacleAnimType = (TargetExplodeFX.ObstacleAnimType)int.Parse(targetExInfo[2]);
  58. }
  59. else
  60. {
  61. DebugEx.Log("关卡编辑器中的障碍物的prefab名字解析出现问题");
  62. }
  63. }
  64. else
  65. {
  66. targetResNameIdx = IndexOfArray<string>(targetResNames, targetData.ResourceName);
  67. targetIsObstacle = false;
  68. targetStateMachineIdx = 0;
  69. obstacleAnimType = TargetExplodeFX.ObstacleAnimType.Once;
  70. }
  71. currTargetLift = targetData.Life;
  72. currTargetAttribute = (TargetAttribs)targetData.Attrib;
  73. //
  74. if (targetData.TargetPos.Count == 3)
  75. {
  76. currTargetPos = new Vector3(targetData.TargetPos[0], targetData.TargetPos[1], targetData.TargetPos[2]);
  77. }
  78. else
  79. {
  80. currTargetPos = Vector3.zero;
  81. }
  82. if (targetData.TargetRot.Count == 3)
  83. {
  84. currTargetRot = new Vector3(targetData.TargetRot[0], targetData.TargetRot[1], targetData.TargetRot[2]);
  85. }
  86. else
  87. {
  88. currTargetRot = Vector3.zero;
  89. }
  90. currTargetScale = targetData.Scale;
  91. //
  92. currTargetCtrlAnimIdx = IndexOfArray<string>(targetCtrlAnimNames, targetData.CtrlAniFileName);
  93. currTargetCtrlAnimDelay = targetData.CtrlAniStartDelay;
  94. currTargetCtrlAnimSpeed = targetData.CtrlAniSpeed;
  95. //
  96. currTargetHitAnimIdx = IndexOfArray<string>(targetCtrlAnimNames, targetData.Hit);
  97. currTargetDeathAnimIdx = IndexOfArray<string>(targetCtrlAnimNames, targetData.Death);
  98. //
  99. childTargetDataIds = string.IsNullOrEmpty(targetData.Child) ? "none" : targetData.Child;
  100. childContainerNodes = targetData.cNode;
  101. //
  102. targetDieAttribute = (TargetDieAttribs)targetData.DieRule;
  103. targetRebornId = targetData.ReID == 0 ? -1 : targetData.ReID;
  104. }
  105. GUILayout.FlexibleSpace();
  106. GUILayout.Label("筛选:");
  107. var tmpTargetDataIdFilter = EditorGUILayout.TextField(_targetDataIdFilter);
  108. if (_targetDataIdFilter != tmpTargetDataIdFilter)
  109. {
  110. _targetDataIds = null;
  111. }
  112. _targetDataIdFilter = tmpTargetDataIdFilter;
  113. GUILayout.EndHorizontal();
  114. GUIStyle style1 = new GUIStyle(GUI.skin.button);
  115. style1.normal.textColor = Color.black;
  116. GUIStyle style2 = new GUIStyle(GUI.skin.button);
  117. style2.normal.textColor = Color.white;
  118. GUILayout.BeginHorizontal();
  119. GUILayout.Label("ID分页:");
  120. if (GUILayout.Button("0-999条", pageNo == 0 ? style2 : style1))
  121. {
  122. pageNo = 0;
  123. }
  124. if (GUILayout.Button("1000-1999条", pageNo == 1 ? style2 : style1))
  125. {
  126. pageNo = 1;
  127. }
  128. if (GUILayout.Button("2000-2999条", pageNo == 2 ? style2 : style1))
  129. {
  130. pageNo = 2;
  131. }
  132. if (GUILayout.Button("3000-3999条", pageNo == 3 ? style2 : style1))
  133. {
  134. pageNo = 3;
  135. }
  136. _targetDataIds = null;
  137. GUILayout.FlexibleSpace();
  138. GUILayout.EndHorizontal();
  139. GUILayout.BeginHorizontal();
  140. GUILayout.Label("模型:");
  141. targetResNameIdx = EditorGUILayout.Popup(targetResNameIdx, targetResNames);
  142. if (GUILayout.Button("刷新"))
  143. {
  144. _targetResNames = null;
  145. }
  146. GUILayout.FlexibleSpace();
  147. GUILayout.Label("类型:");
  148. currTargetAttribute = (TargetAttribs)EditorGUILayout.EnumPopup(currTargetAttribute);
  149. GUILayout.FlexibleSpace();
  150. GUILayout.EndHorizontal();
  151. EditorGUILayout.Space();
  152. GUILayout.BeginHorizontal();
  153. GUILayout.Label("血量HP:");
  154. currTargetLift = EditorGUILayout.IntField(currTargetLift);
  155. GUILayout.FlexibleSpace();
  156. targetIsObstacle = EditorGUILayout.Toggle("女战士系列:", targetIsObstacle);
  157. GUILayout.FlexibleSpace();
  158. GUILayout.EndHorizontal();
  159. if (targetIsObstacle)
  160. {
  161. GUILayout.BeginHorizontal();
  162. GUILayout.Label("状态机:");
  163. targetStateMachineIdx = EditorGUILayout.Popup(targetStateMachineIdx, targetStateMachineNames);
  164. if (GUILayout.Button("刷新"))
  165. {
  166. _targetStateMachineNames = null;
  167. }
  168. GUILayout.FlexibleSpace();
  169. GUILayout.Label("循环类型:");
  170. obstacleAnimType = (TargetExplodeFX.ObstacleAnimType)EditorGUILayout.EnumPopup(obstacleAnimType);
  171. GUILayout.FlexibleSpace();
  172. GUILayout.EndHorizontal();
  173. EditorGUILayout.Space();
  174. }
  175. GUILayout.BeginHorizontal();
  176. GUILayout.Label("位置:");
  177. currTargetPos = EditorGUILayout.Vector3Field(string.Empty, currTargetPos);
  178. GUILayout.FlexibleSpace();
  179. GUILayout.Label("缩放:");
  180. currTargetScale = EditorGUILayout.FloatField(currTargetScale);
  181. GUILayout.FlexibleSpace();
  182. GUILayout.EndHorizontal();
  183. GUILayout.BeginHorizontal();
  184. GUILayout.Label("活动动画:");
  185. currTargetCtrlAnimIdx = EditorGUILayout.Popup(currTargetCtrlAnimIdx, targetCtrlAnimNames);
  186. GUILayout.FlexibleSpace();
  187. GUILayout.Label("延迟播放:");
  188. currTargetCtrlAnimDelay = EditorGUILayout.FloatField(currTargetCtrlAnimDelay);
  189. GUILayout.FlexibleSpace();
  190. GUILayout.Label("播放速度:");
  191. currTargetCtrlAnimSpeed = EditorGUILayout.FloatField(currTargetCtrlAnimSpeed);
  192. GUILayout.FlexibleSpace();
  193. GUILayout.EndHorizontal();
  194. GUILayout.BeginHorizontal();
  195. GUILayout.Label("击中动画:");
  196. currTargetHitAnimIdx = EditorGUILayout.Popup(currTargetHitAnimIdx, targetCtrlAnimNames);
  197. GUILayout.FlexibleSpace();
  198. GUILayout.Label("死亡动画:");
  199. currTargetDeathAnimIdx = EditorGUILayout.Popup(currTargetDeathAnimIdx, targetCtrlAnimNames);
  200. GUILayout.FlexibleSpace();
  201. GUILayout.EndHorizontal();
  202. GUILayout.BeginHorizontal();
  203. GUILayout.Label("子靶子:");
  204. childTargetDataIds = EditorGUILayout.TextField(childTargetDataIds);
  205. GUILayout.FlexibleSpace();
  206. GUILayout.Label("挂点:");
  207. childContainerNodes = EditorGUILayout.TextField(childContainerNodes);
  208. GUILayout.FlexibleSpace();
  209. GUILayout.EndHorizontal();
  210. GUILayout.BeginHorizontal();
  211. GUILayout.Label("重生方式:");
  212. targetDieAttribute = (TargetDieAttribs)EditorGUILayout.EnumPopup(targetDieAttribute);
  213. GUILayout.FlexibleSpace();
  214. GUILayout.Label("重生ID:");
  215. targetRebornId = EditorGUILayout.IntPopup(targetRebornId, targetDataStrIds, targetDataIds);
  216. GUILayout.FlexibleSpace();
  217. GUILayout.EndHorizontal();
  218. targetDataChanged = false;
  219. GUILayout.BeginHorizontal();
  220. if (GUILayout.Button("保存"))
  221. {
  222. //
  223. if (targetIsObstacle)
  224. {
  225. targetData.ResourceName = string.Format("{0}#{1}#{2}", targetResNames[targetResNameIdx], targetStateMachineNames[targetStateMachineIdx], (int)obstacleAnimType);
  226. DebugEx.Log("targetData.ResourceName =" + targetData.ResourceName);
  227. }
  228. else
  229. {
  230. targetData.ResourceName = targetResNames[targetResNameIdx];
  231. }
  232. targetData.Attrib = (int)currTargetAttribute;
  233. targetData.TargetPos = new List<float> { currTargetPos.x, currTargetPos.y, currTargetPos.z };
  234. targetData.TargetRot = new List<float> { currTargetRot.x, currTargetRot.y, currTargetRot.z };
  235. targetData.Scale = currTargetScale;
  236. if (currTargetCtrlAnimIdx < 0)
  237. {
  238. targetData.CtrlAniFileName = string.Empty;
  239. }
  240. else
  241. {
  242. targetData.CtrlAniFileName = targetCtrlAnimNames[currTargetCtrlAnimIdx];
  243. }
  244. targetData.CtrlAniStartDelay = currTargetCtrlAnimDelay;
  245. targetData.CtrlAniSpeed = currTargetCtrlAnimSpeed;
  246. if (currTargetHitAnimIdx < 0)
  247. {
  248. targetData.Hit = string.Empty;
  249. }
  250. else
  251. {
  252. targetData.Hit = targetCtrlAnimNames[currTargetHitAnimIdx];
  253. }
  254. if (currTargetDeathAnimIdx < 0)
  255. {
  256. targetData.Death = string.Empty;
  257. }
  258. else
  259. {
  260. targetData.Death = targetCtrlAnimNames[currTargetDeathAnimIdx];
  261. }
  262. targetData.Child = childTargetDataIds.Trim();
  263. targetData.cNode = childContainerNodes;
  264. targetData.DieRule = (int)targetDieAttribute;
  265. targetData.ReID = targetRebornId == -1 ? 0 : targetRebornId;
  266. targetData.Life = currTargetLift;
  267. ConfigEditor.SaveTargetData2ExcelAndJson();
  268. battleEditor.Reload();
  269. BattleFieldSceneCreator.CompileGameConfigs();
  270. DataManager.Instance.InitData();
  271. }
  272. GUILayout.EndHorizontal();
  273. GUILayout.Space(10f);
  274. GUILayout.BeginHorizontal();
  275. GUILayout.Label("ID");
  276. newTargetDataId = EditorGUILayout.IntField(newTargetDataId);
  277. GUILayout.FlexibleSpace();
  278. if (GUILayout.Button("新增"))
  279. {
  280. if (newTargetDataId <= 0)
  281. {
  282. foreach (var targetDataId in ConfigEditor.targetDataCfgs.Keys)
  283. {
  284. if (targetDataId > newTargetDataId) newTargetDataId = targetDataId;
  285. }
  286. newTargetDataId++;
  287. }
  288. if (newTargetDataId < 0 || ConfigEditor.targetDataCfgs.ContainsKey(newTargetDataId))
  289. {
  290. Debug.LogError("Error, new target data id: " + newTargetDataId.ToString());
  291. }
  292. else
  293. {
  294. var newTargetData = new TargetDataDefine
  295. {
  296. ID = newTargetDataId,
  297. ResourceName = targetResNames[0],
  298. Life = currTargetLift,
  299. CtrlAniFileName = "none",
  300. Attrib = 0,
  301. CtrlAniStartDelay = 0f,
  302. Child = "none",
  303. cNode = "none",
  304. Material = 0,
  305. Hit = "none",
  306. Death = "none",
  307. CtrlAniSpeed = 1f,
  308. Scale = 1f,
  309. DieRule = (int)TargetDieAttribs.Do_Nothing,
  310. ReID = 0,
  311. TargetPos = new List<float> { 0f, 0f, 0f },
  312. TargetRot = new List<float> { 0f, 0f, 0f },
  313. };
  314. ConfigEditor.targetDataCfgs.Add(newTargetData.ID, newTargetData);
  315. targetDataChanged = true;
  316. currTargetDataId = newTargetData.ID;
  317. }
  318. }
  319. GUILayout.FlexibleSpace();
  320. GUILayout.EndHorizontal();
  321. GUILayout.BeginHorizontal();
  322. if (GUILayout.Button("删除"))
  323. {
  324. if (ConfigEditor.targetDataCfgs.ContainsKey(currTargetDataId))
  325. {
  326. ConfigEditor.targetDataCfgs.Remove(currTargetDataId);
  327. Debug.Log("Remove target data successfully. ID:" + currTargetDataId);
  328. targetDataChanged = true;
  329. _targetDataIds = null;
  330. if (targetDataIds.Length > 1)
  331. {
  332. currTargetDataId = targetDataIds[1];
  333. }
  334. else
  335. {
  336. currTargetDataId = targetDataIds[0];
  337. }
  338. }
  339. }
  340. GUILayout.EndHorizontal();
  341. }
  342. public static void ClearBattleTargetEditorDataCache(this BattleFieldSceneCreator battleEditor)
  343. {
  344. _targetDataIds = null;
  345. _targetResNames = null;
  346. _targetCtrlAnimNames = null;
  347. _targetStateMachineNames = null;
  348. }
  349. public static char PATH_DELIMITER
  350. {
  351. get
  352. {
  353. return Application.platform == RuntimePlatform.WindowsEditor ? '\\' : '/';
  354. }
  355. }
  356. public static int IndexOfArray<T>(T[] arr, T target) where T : IComparable
  357. {
  358. for (var i = 0; i < arr.Length; i++)
  359. {
  360. if (arr[i].Equals(target)) return i;
  361. }
  362. return -1;
  363. }
  364. /**
  365. * Data
  366. */
  367. private static bool targetDataChanged = true;
  368. private static int currTargetDataId = -1;
  369. private static TargetAttribs currTargetAttribute = TargetAttribs.ExistAlways;
  370. private static TargetExplodeFX.ObstacleAnimType obstacleAnimType = TargetExplodeFX.ObstacleAnimType.Once;
  371. private static Vector3 currTargetPos;
  372. private static Vector3 currTargetRot;
  373. private static float currTargetScale = 1f;
  374. private static int currTargetCtrlAnimIdx = 0;
  375. private static float currTargetCtrlAnimDelay = 0f;
  376. private static float currTargetCtrlAnimSpeed = 1f;
  377. private static int currTargetLift = 1;
  378. private static bool targetIsObstacle = false;
  379. private static int currTargetHitAnimIdx = 0;
  380. private static int currTargetDeathAnimIdx = 0;
  381. private static int newTargetDataId = -1;
  382. //
  383. private static string childTargetDataIds = "none";
  384. private static string childContainerNodes;
  385. //
  386. //皇冠相关的子靶子
  387. private static string crownTargetDataIds = "none";
  388. //皇冠靶子挂点
  389. private static string crownTargetNodes;
  390. private static float crownTargetScale;
  391. private static string crownTargetParentName;
  392. //
  393. private static TargetDieAttribs targetDieAttribute = TargetDieAttribs.Do_Nothing;
  394. private static int targetRebornId = -1;
  395. // Target Data Ids
  396. private static int pageNo = 0;
  397. private static string _targetDataIdFilter = string.Empty;
  398. private static string[] targetDataStrIds;
  399. private static int[] _targetDataIds;
  400. private static int[] targetDataIds
  401. {
  402. get
  403. {
  404. if (_targetDataIds == null)
  405. {
  406. int i = 0;
  407. var ints = new Stack<int>();
  408. var strs = new Stack<string>();
  409. foreach (var kv in ConfigEditor.targetDataCfgs)
  410. {
  411. if (kv.Key < 10000) continue;
  412. var strInt = kv.Key.ToString();
  413. if (!strInt.StartsWith(_targetDataIdFilter, StringComparison.Ordinal))
  414. {
  415. continue;
  416. }
  417. if (i / 1000 == pageNo)
  418. {
  419. ints.Push(kv.Key);
  420. strs.Push(strInt);
  421. }
  422. i++;
  423. }
  424. ints.Push(-1);
  425. strs.Push("none");
  426. targetDataStrIds = strs.ToArray();
  427. _targetDataIds = ints.ToArray();
  428. ints.Clear();
  429. strs.Clear();
  430. ints = null;
  431. strs = null;
  432. }
  433. return _targetDataIds;
  434. }
  435. }
  436. public static int[] GetTargetDataIds(this BattleFieldSceneCreator battleEditor)
  437. {
  438. return targetDataIds;
  439. }
  440. public static string[] GetTargetDataStrIds(this BattleFieldSceneCreator battleEditor)
  441. {
  442. return targetDataStrIds;
  443. }
  444. // Target Resource Name
  445. private static int targetResNameIdx = 0;
  446. private static string[] _targetResNames;
  447. private static string[] targetResNames
  448. {
  449. get
  450. {
  451. if (_targetResNames == null)
  452. {
  453. var paths = Directory.GetFiles(Path.Combine(Application.dataPath, "Resources/Prefabs/Targets"), "*.prefab");
  454. var list = new List<string>();
  455. foreach (var path in paths)
  456. {
  457. list.Add(path.Substring(path.LastIndexOf(PATH_DELIMITER) + 1).Replace(".prefab", ""));
  458. }
  459. _targetResNames = list.ToArray();
  460. list.Clear();
  461. list = null;
  462. }
  463. return _targetResNames;
  464. }
  465. }
  466. // Target Ctrl Anim
  467. private static string[] _targetCtrlAnimNames;
  468. private static string[] targetCtrlAnimNames
  469. {
  470. get
  471. {
  472. if (_targetCtrlAnimNames == null)
  473. {
  474. var paths = Directory.GetFiles(Path.Combine(Application.dataPath, "Resources/Prefabs/AnimationClips"), "*.anim");
  475. var list = new List<string> { "none" };
  476. foreach (var path in paths)
  477. {
  478. list.Add(path.Substring(path.LastIndexOf(PATH_DELIMITER) + 1).Replace(".anim", ""));
  479. }
  480. _targetCtrlAnimNames = list.ToArray();
  481. list.Clear();
  482. list = null;
  483. }
  484. return _targetCtrlAnimNames;
  485. }
  486. }
  487. // Target stateMachine overrideController
  488. private static int targetStateMachineIdx = 0;
  489. private static string[] _targetStateMachineNames;
  490. private static string[] targetStateMachineNames
  491. {
  492. get
  493. {
  494. if (_targetStateMachineNames == null)
  495. {
  496. var paths = Directory.GetFiles(Path.Combine(Application.dataPath, "ShootGame/Resources/Animators"), "*.overrideController");
  497. var list = new List<string> { "none" };
  498. foreach (var path in paths)
  499. {
  500. list.Add(path.Substring(path.LastIndexOf(PATH_DELIMITER) + 1).Replace(".overrideController", ""));
  501. }
  502. _targetStateMachineNames = list.ToArray();
  503. list.Clear();
  504. list = null;
  505. }
  506. return _targetStateMachineNames;
  507. }
  508. }
  509. }
  510. }