TrainCampScene.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections.Generic;
  4. public class TeamActorInfo
  5. {
  6. public int id =0;
  7. public int level = 0;
  8. public Role_Gender gender = Role_Gender.Male; //性别 1: 女,2男
  9. public int skillId = 0;
  10. public int skillLv = 1;
  11. public bool invincible = false;
  12. public TeamActorInfo(int id,int level,int skillId,int skillLv,Role_Gender gender,bool invincible)
  13. {
  14. this.id = id;
  15. this.level = level;
  16. this.skillId = skillId;
  17. this.skillLv = skillLv;
  18. this.gender = gender;
  19. this.invincible = invincible;
  20. }
  21. }
  22. public class EnemyActorInfo
  23. {
  24. public int id = 0;
  25. public int level = 0;
  26. public int posVal = 1;
  27. public bool invincible = false;
  28. public EnemyActorInfo(int id, int level, int posVal,bool invincible)
  29. {
  30. this.id = id;
  31. this.level = level;
  32. this.posVal = posVal;
  33. this.invincible = invincible;
  34. }
  35. }
  36. public class TrainCampScene : EditorWindow
  37. {
  38. const int c_team_num = 4;
  39. const int c_enemy_num = 6;
  40. private List<TeamActorInfo> teamActorInfos = new List<TeamActorInfo>();
  41. private List<EnemyActorInfo> enemyActorInfos = new List<EnemyActorInfo>();
  42. private string[] _arrProfessionName;
  43. private int[] _arrProfessionId;
  44. private string[] _arrFellowName;
  45. private int[] _arrFellowId;
  46. private string[] _arrSkillName;
  47. private int[] _arrSkillId;
  48. private string[] _arrNpcName;
  49. private int[] _arrNpcId;
  50. private bool enableLog = false;
  51. bool initedCfg = false;
  52. [MenuItem("Skill_Tool/启动训练营测试")]
  53. public static void OpenPreviewWindow()
  54. {
  55. ConfigMgr.CreateInstance();
  56. EditorWindow.GetWindow(typeof(TrainCampScene));
  57. }
  58. private void OnEnable()
  59. {
  60. }
  61. private void OnDestroy()
  62. {
  63. }
  64. private void Update()
  65. {
  66. if(!initedCfg && ConfigMgr.InitFinished)
  67. {
  68. ReadSkillCfg();
  69. ReadFellowNpcCfg();
  70. ReadNpcCfg();
  71. ReadProfessionCfg();
  72. ReadCache();
  73. initedCfg = true;
  74. }
  75. else
  76. {
  77. if (teamActorInfos.Count == 0)
  78. {
  79. ReadCache();
  80. }
  81. }
  82. }
  83. void ReadCache()
  84. {
  85. teamActorInfos.Clear();
  86. enemyActorInfos.Clear();
  87. for(int idx =0; idx < c_team_num; idx++)
  88. {
  89. string idKey = string.Format("TrainCamp_HeroID_{0}", idx);
  90. string levelKey = string.Format("TrainCamp_HeroLevel_{0}", idx);
  91. string skillIdKey = string.Format("TrainCamp_HeroSkillId_{0}", idx);
  92. string skillLvKey = string.Format("TrainCamp_HeroSkillLv_{0}", idx);
  93. string genderKey = string.Format("TrainCamp_Gender_{0}", idx);
  94. string invincibleKey = string.Format("TrainCamp_Invincible_{0}", idx);
  95. int id = 0, lv = 0, skillId = 0, skillLv = 0;
  96. Role_Gender gender = Role_Gender.Male;
  97. bool invincible = false;
  98. if (PlayerPrefs.HasKey(idKey))
  99. id = PlayerPrefs.GetInt(idKey);
  100. if (PlayerPrefs.HasKey(levelKey))
  101. lv = PlayerPrefs.GetInt(levelKey);
  102. if (PlayerPrefs.HasKey(skillIdKey))
  103. skillId = PlayerPrefs.GetInt(skillIdKey);
  104. if (PlayerPrefs.HasKey(skillLvKey))
  105. skillLv = PlayerPrefs.GetInt(skillLvKey);
  106. if (PlayerPrefs.HasKey(genderKey))
  107. gender = (Role_Gender)PlayerPrefs.GetInt(genderKey);
  108. if (PlayerPrefs.HasKey(invincibleKey))
  109. invincible = PlayerPrefs.GetInt(invincibleKey)>0?true:false;
  110. TeamActorInfo info = new TeamActorInfo(id,lv,skillId,skillLv,gender, invincible);
  111. teamActorInfos.Add(info);
  112. }
  113. for(int idx = 0; idx < c_enemy_num; idx++)
  114. {
  115. string idKey = string.Format("TrainCamp_NpcID_{0}", idx);
  116. string levelKey = string.Format("TrainCamp_NpcLevel_{0}", idx);
  117. string posKey = string.Format("TrainCamp_NpcPos_{0}", idx);
  118. string invincibleKey = string.Format("TrainCamp_NpcInvincible_{0}", idx);
  119. int id = 0, level = 0,pos = 0;
  120. bool invincible = false;
  121. if (PlayerPrefs.HasKey(idKey))
  122. id = PlayerPrefs.GetInt(idKey);
  123. if (PlayerPrefs.HasKey(levelKey))
  124. level = PlayerPrefs.GetInt(levelKey);
  125. if (PlayerPrefs.HasKey(posKey))
  126. pos = PlayerPrefs.GetInt(posKey);
  127. if (PlayerPrefs.HasKey(invincibleKey))
  128. invincible = PlayerPrefs.GetInt(invincibleKey) > 0 ? true : false;
  129. EnemyActorInfo info = new EnemyActorInfo(id, level,pos, invincible);
  130. enemyActorInfos.Add(info);
  131. }
  132. if (PlayerPrefs.HasKey("EnabledLog"))
  133. enableLog = PlayerPrefs.GetInt("EnabledLog") > 0 ? true : false;
  134. }
  135. void SaveCache()
  136. {
  137. for (int idx = 0; idx < c_team_num; idx++)
  138. {
  139. string idKey = string.Format("TrainCamp_HeroID_{0}", idx);
  140. string levelKey = string.Format("TrainCamp_HeroLevel_{0}", idx);
  141. string skillIdKey = string.Format("TrainCamp_HeroSkillId_{0}", idx);
  142. string skillLvKey = string.Format("TrainCamp_HeroSkillLv_{0}", idx);
  143. string genderKey = string.Format("TrainCamp_Gender_{0}", idx);
  144. string invincibleKey = string.Format("TrainCamp_Invincible_{0}", idx);
  145. TeamActorInfo info = teamActorInfos[idx];
  146. PlayerPrefs.SetInt(idKey, info.id);
  147. PlayerPrefs.SetInt(levelKey, info.level);
  148. PlayerPrefs.SetInt(skillIdKey, info.skillId);
  149. PlayerPrefs.SetInt(skillLvKey, info.skillLv);
  150. PlayerPrefs.SetInt(genderKey, (int)info.gender);
  151. PlayerPrefs.SetInt(invincibleKey, info.invincible?1:0);
  152. }
  153. for (int idx = 0; idx < c_enemy_num; idx++)
  154. {
  155. string idKey = string.Format("TrainCamp_NpcID_{0}", idx);
  156. string levelKey = string.Format("TrainCamp_NpcLevel_{0}", idx);
  157. string posKey = string.Format("TrainCamp_NpcPos_{0}", idx);
  158. string invincibleKey = string.Format("TrainCamp_NpcInvincible_{0}", idx);
  159. EnemyActorInfo info = enemyActorInfos[idx];
  160. PlayerPrefs.SetInt(idKey, info.id);
  161. PlayerPrefs.SetInt(levelKey, info.level);
  162. PlayerPrefs.SetInt(posKey, info.posVal);
  163. PlayerPrefs.SetInt(invincibleKey, info.invincible?1:0);
  164. }
  165. PlayerPrefs.SetInt("EnabledLog", enableLog ? 1 : 0);
  166. }
  167. void ReadSkillCfg()
  168. {
  169. List<int> skillIds = new List<int>();
  170. List<string> skillNames = new List<string>();
  171. Dictionary<string, Dictionary<string, string>> dic = ConfigMgr.Instance.getTable(Config.SkillCfgName);
  172. if(dic != null)
  173. {
  174. foreach(var cfg in dic)
  175. {
  176. if (cfg.Key.ToLower() == "id" || cfg.Key.ToLower() == "int") continue;
  177. int uniqueId = 0;
  178. int.TryParse(cfg.Key, out uniqueId);
  179. if (uniqueId <= 0) continue;
  180. Dictionary<string, string> temp = cfg.Value;
  181. if(temp!=null)
  182. {
  183. int skillId = 0;
  184. if (temp.ContainsKey("SkillId"))
  185. {
  186. int.TryParse(temp["SkillId"], out skillId);
  187. }
  188. if (skillId == 0) continue;
  189. if (skillIds.Contains(skillId)) continue;
  190. skillIds.Add(skillId);
  191. skillNames.Add(temp["Name"]);
  192. }
  193. }
  194. }
  195. _arrSkillId = skillIds.ToArray();
  196. _arrSkillName = skillNames.ToArray();
  197. }
  198. void ReadNpcCfg()
  199. {
  200. List<int> npcIds = new List<int>();
  201. List<string> npcNames = new List<string>();
  202. Dictionary<string, Dictionary<string, string>> dic = ConfigMgr.Instance.getTable(Config.NpcCfgName);
  203. if(dic!=null)
  204. {
  205. foreach (var cfg in dic)
  206. {
  207. if (cfg.Key.ToLower() == "id" || cfg.Key.ToLower() == "int") continue;
  208. int npcId = 0;
  209. int.TryParse(cfg.Key, out npcId);
  210. if (npcId <= 0) continue;
  211. Dictionary<string, string> temp = cfg.Value;
  212. if (temp != null)
  213. {
  214. if (npcIds.Contains(npcId)) continue;
  215. npcIds.Add(npcId);
  216. npcNames.Add(temp["Name"]);
  217. }
  218. }
  219. }
  220. _arrNpcId = npcIds.ToArray();
  221. _arrNpcName = npcNames.ToArray();
  222. }
  223. void ReadFellowNpcCfg()
  224. {
  225. List<int> fellowIds = new List<int>();
  226. List<string> fellowNames = new List<string>();
  227. Dictionary<string, Dictionary<string, string>> dic = ConfigMgr.Instance.getTable(Config.FellowCfgName);
  228. if (dic != null)
  229. {
  230. foreach (var cfg in dic)
  231. {
  232. if (cfg.Key.ToLower() == "parterid" || cfg.Key.ToLower() == "int") continue;
  233. int fellowId = 0;
  234. int.TryParse(cfg.Key, out fellowId);
  235. if (fellowId <= 0) continue;
  236. Dictionary<string, string> temp = cfg.Value;
  237. if (temp != null)
  238. {
  239. if (fellowIds.Contains(fellowId)) continue;
  240. fellowIds.Add(fellowId);
  241. fellowNames.Add(temp["Name"]);
  242. }
  243. }
  244. }
  245. _arrFellowId = fellowIds.ToArray();
  246. _arrFellowName = fellowNames.ToArray();
  247. }
  248. void ReadProfessionCfg()
  249. {
  250. List<int> professionIds = new List<int>();
  251. List<string> professionNames = new List<string>();
  252. Dictionary<string, Dictionary<string, string>> dic = ConfigMgr.Instance.getTable(Config.JobCfgName);
  253. if (dic != null)
  254. {
  255. professionIds.Add(0);
  256. professionNames.Add("0");
  257. foreach (var cfg in dic)
  258. {
  259. if (cfg.Key.ToLower() == "id" || cfg.Key.ToLower() == "int") continue;
  260. int professionId = 0;
  261. int.TryParse(cfg.Key, out professionId);
  262. if (professionId <= 0) continue;
  263. Dictionary<string, string> temp = cfg.Value;
  264. if (temp != null)
  265. {
  266. if (professionIds.Contains(professionId)) continue;
  267. professionIds.Add(professionId);
  268. professionNames.Add(temp["Desc"]);
  269. }
  270. }
  271. }
  272. _arrProfessionId = professionIds.ToArray();
  273. _arrProfessionName = professionNames.ToArray();
  274. }
  275. protected void OnGUI()
  276. {
  277. if (!initedCfg) return;
  278. EditorGUILayout.LabelField("-------------------------我方英雄设置---------------------");
  279. // 选择英雄
  280. for (int idx =0; idx < c_team_num && idx < teamActorInfos.Count;idx++)
  281. {
  282. EditorGUILayout.BeginHorizontal();
  283. if (idx == 0)
  284. {
  285. EditorGUILayout.LabelField("职业ID:",GUILayout.Width(80));
  286. teamActorInfos[idx].id = EditorGUILayout.IntPopup(teamActorInfos[idx].id, _arrProfessionName, _arrProfessionId,GUILayout.Width(100));
  287. EditorGUILayout.LabelField(teamActorInfos[idx].id.ToString(),GUILayout.Width(100));
  288. EditorGUILayout.LabelField("等级:", GUILayout.Width(80));
  289. teamActorInfos[idx].level = EditorGUILayout.IntField(teamActorInfos[idx].level, GUILayout.Width(100));
  290. EditorGUILayout.LabelField("性别:", GUILayout.Width(80));
  291. teamActorInfos[idx].gender =(Role_Gender)EditorGUILayout.EnumPopup(teamActorInfos[idx].gender, GUILayout.Width(80));
  292. }
  293. else
  294. {
  295. EditorGUILayout.LabelField("伙伴ID:", GUILayout.Width(80));
  296. teamActorInfos[idx].id = EditorGUILayout.IntPopup(teamActorInfos[idx].id, _arrFellowName, _arrFellowId, GUILayout.Width(100));
  297. EditorGUILayout.LabelField(teamActorInfos[idx].id.ToString(), GUILayout.Width(100));
  298. EditorGUILayout.LabelField("等级:", GUILayout.Width(80));
  299. teamActorInfos[idx].level = EditorGUILayout.IntField(teamActorInfos[idx].level, GUILayout.Width(100));
  300. teamActorInfos[idx].gender = Role_Gender.Male;
  301. }
  302. EditorGUILayout.LabelField("测试技能:", GUILayout.Width(80));
  303. teamActorInfos[idx].skillId = EditorGUILayout.IntPopup(teamActorInfos[idx].skillId, _arrSkillName, _arrSkillId, GUILayout.Width(100));
  304. EditorGUILayout.LabelField(teamActorInfos[idx].skillId.ToString(), GUILayout.Width(100));
  305. EditorGUILayout.LabelField("技能等级:", GUILayout.Width(80));
  306. teamActorInfos[idx].skillLv = EditorGUILayout.IntField(teamActorInfos[idx].skillLv, GUILayout.Width(100));
  307. EditorGUILayout.LabelField("无敌:", GUILayout.Width(40));
  308. teamActorInfos[idx].invincible = EditorGUILayout.Toggle("", teamActorInfos[idx].invincible, GUILayout.Width(20));
  309. EditorGUILayout.EndHorizontal();
  310. }
  311. EditorGUILayout.Space();
  312. EditorGUILayout.LabelField("----------------------------------------怪物设置-------------------------------------");
  313. for (int idx = 0; idx < c_enemy_num && idx < enemyActorInfos.Count; idx++)
  314. {
  315. EditorGUILayout.BeginHorizontal();
  316. EditorGUILayout.LabelField("npcid:", GUILayout.Width(80));
  317. enemyActorInfos[idx].id = EditorGUILayout.IntPopup(enemyActorInfos[idx].id, _arrNpcName, _arrNpcId, GUILayout.Width(100));
  318. EditorGUILayout.LabelField(enemyActorInfos[idx].id.ToString(), GUILayout.Width(100));
  319. EditorGUILayout.LabelField("等级:", GUILayout.Width(80));
  320. enemyActorInfos[idx].level = EditorGUILayout.IntField(enemyActorInfos[idx].level, GUILayout.Width(100));
  321. EditorGUILayout.LabelField("站位:", GUILayout.Width(80));
  322. enemyActorInfos[idx].posVal = EditorGUILayout.IntField(enemyActorInfos[idx].posVal, GUILayout.Width(100));
  323. EditorGUILayout.LabelField("无敌:", GUILayout.Width(40));
  324. enemyActorInfos[idx].invincible = EditorGUILayout.Toggle("", enemyActorInfos[idx].invincible, GUILayout.Width(20));
  325. EditorGUILayout.EndHorizontal();
  326. }
  327. EditorGUILayout.Space();
  328. EditorGUILayout.BeginHorizontal();
  329. EditorGUILayout.LabelField("开启日志:");
  330. enableLog = EditorGUILayout.Toggle(enableLog);
  331. EditorGUILayout.LabelField(enableLog.ToString());
  332. EditorGUILayout.EndHorizontal();
  333. EditorGUILayout.Space();
  334. // 开始预览
  335. if (GUILayout.Button("开始预览"))
  336. {
  337. StartPreview();
  338. }
  339. }
  340. void StartPreview()
  341. {
  342. SaveCache();
  343. GameObject bootObj = null;
  344. if (EditorApplication.isPlaying)
  345. {
  346. bootObj = GameObject.Find("BootObj");
  347. if (null != bootObj)
  348. {
  349. if (bootObj.GetComponent<EditorBattleMgr>() != null)
  350. {
  351. bootObj.GetComponent<EditorBattleMgr>().Restart();
  352. }
  353. return;
  354. }
  355. EditorApplication.ExecuteMenuItem("Edit/Play");
  356. }
  357. // 打开启动场景
  358. EditorApplication.OpenScene("Assets/Scenes_Discard/Test_TrainCamp/TrainCamp.unity");
  359. // 开始播放
  360. EditorApplication.ExecuteMenuItem("Edit/Play");
  361. // 将GameFramework替换为预览组件
  362. bootObj = GameObject.Find("BootObj");
  363. if (bootObj != null)
  364. {
  365. EditorBattleMgr mgr = bootObj.GetComponent<EditorBattleMgr>();
  366. if (mgr != null)
  367. GameObject.DestroyImmediate(mgr);
  368. bootObj.AddComponent<EditorBattleMgr>();
  369. }
  370. }
  371. }