BattleFieldSceneCreator.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEditor;
  4. using System.IO;
  5. using System;
  6. using JamesCore;
  7. using System.Diagnostics;
  8. using James.Util;
  9. using UnityEngine.AddressableAssets;
  10. using Debug = UnityEngine.Debug;
  11. namespace JamesGame
  12. {
  13. [ExecuteInEditMode]
  14. public class BattleFieldSceneCreator : EditorWindow
  15. {
  16. private void OnFocus()
  17. {
  18. DataManager.Instance.InitData();
  19. GamePredata.Instance.InitData();
  20. }
  21. private void OnEnable()
  22. {
  23. Core.defaultRun();
  24. }
  25. private static GameObject editorTmpRoot;
  26. public const string EDITOR_TMP_NAME = "__EDITOR_TMp_root__";
  27. public static void AddDistanceSigns(int km)
  28. {
  29. // var gos = LevelBattleFieldLoader.LoadDistanceSigns(km);
  30. var distanceSigns = new GameObject("DistanceSigns");
  31. distanceSigns.transform.position = Vector3.zero;
  32. //var disSignTran = distanceSigns.transform;
  33. //disSignTran.SetParent(editorTmpRoot.transform);
  34. //
  35. // for (int i = 0; i < gos.Count; i++)
  36. LevelBattleFieldLoader.LoadDistanceSigns(km, (i, go) =>
  37. {
  38. var distance = (i + 1) * 20f + 10f;
  39. // var go = gos[i];
  40. var tran = go.transform;
  41. tran.SetParent(distanceSigns.transform);
  42. tran.localPosition = new Vector3(-5f, 0f, distance);
  43. tran.rotation = Quaternion.Euler(Vector3.zero);
  44. tran.localScale = Vector3.one;
  45. //
  46. var clone = Instantiate(go);
  47. clone.transform.SetParent(tran.parent);
  48. clone.transform.localPosition = new Vector3(5f, 0f, distance);
  49. });
  50. }
  51. public static void ClearScene()
  52. {
  53. var tmpRoot = GameObject.Find(EDITOR_TMP_NAME);
  54. if (tmpRoot != null)
  55. {
  56. DestroyImmediate(tmpRoot);
  57. editorTmpRoot = null;
  58. }
  59. }
  60. public static GameObject CreateEditorTmpRoot()
  61. {
  62. if (editorTmpRoot != null) return editorTmpRoot;
  63. var tmpRoot = GameObject.Find(EDITOR_TMP_NAME);
  64. if (tmpRoot != null)
  65. {
  66. editorTmpRoot = tmpRoot;
  67. return editorTmpRoot;
  68. }
  69. //
  70. tmpRoot = new GameObject(EDITOR_TMP_NAME);
  71. var tmpRootTran = tmpRoot.transform;
  72. tmpRootTran.position = Vector3.zero;
  73. tmpRootTran.rotation = Quaternion.Euler(Vector3.zero);
  74. tmpRootTran.localScale = Vector3.one;
  75. editorTmpRoot = tmpRoot;
  76. // Add peek camera
  77. var camGo = new GameObject("PeekCamera");
  78. var camTran = camGo.transform;
  79. camTran.SetParent(editorTmpRoot.transform);
  80. camTran.localPosition = new Vector3(0f, 5f, 0f);
  81. camTran.localRotation = Quaternion.Euler(Vector3.zero);
  82. camTran.localScale = Vector3.one;
  83. var cam = camGo.AddComponent<Camera>();
  84. cam.tag = "MainCamera";
  85. cam.fieldOfView = 50;
  86. // Clear original camera and light
  87. var oriCam = GameObject.Find("Main Camera");
  88. if (oriCam != null) DestroyImmediate(oriCam);
  89. var oriLight = GameObject.Find("Directional Light");
  90. if (oriLight != null) DestroyImmediate(oriLight);
  91. //
  92. // var adapter = Resources.Load("Prefabs/ARCHERY_ADAPTER");
  93. var adapter = AssetDatabase.LoadAssetAtPath<GameObject>("Assets/LevelEditor/Prefabs/ARCHERY_ADAPTER.prefab");
  94. Instantiate(adapter, tmpRootTran);
  95. //
  96. // var global = Resources.Load("Prefabs/Global");
  97. var global = AssetDatabase.LoadAssetAtPath<GameObject>("Assets/LevelEditor/Prefabs/Global.prefab");
  98. Instantiate(global, tmpRootTran);
  99. //加载UI主控制器
  100. // var uiMain = Resources.Load("Prefabs/UIMainController");
  101. var uiMain = AssetDatabase.LoadAssetAtPath<GameObject>("Assets/LevelEditor/Prefabs/UIMainController.prefab");
  102. Instantiate(uiMain, tmpRootTran);
  103. //加载UI主控制器
  104. // var pool = Resources.Load("Prefabs/ObjectPool");
  105. var pool = AssetDatabase.LoadAssetAtPath<GameObject>("Assets/LevelEditor/Prefabs/ObjectPool.prefab");
  106. Instantiate(pool, tmpRootTran);
  107. return editorTmpRoot;
  108. }
  109. [MenuItem("NeoGame/关卡编辑器", false)]
  110. public static void ShowDashboard()
  111. {
  112. EditorWindow.GetWindow<BattleFieldSceneCreator>();
  113. }
  114. [MenuItem("NeoGame/编译数据表")]
  115. public static void CompileGameConfigs()
  116. {
  117. string xlsPath = "../../Data";
  118. string batPath = Path.Combine(Application.dataPath, xlsPath);
  119. if (Application.platform == RuntimePlatform.OSXEditor)
  120. {
  121. ProcessCommand("/bin/bash", batPath, "export.sh", true);
  122. }
  123. else
  124. {
  125. ProcessCommand("export.bat", batPath, "", true);
  126. }
  127. GameDataConverter.WriteTableData("../Data/exports/TableData.bin", "Assets/StreamingAssets/ReadWritePath/Config/UserData.json");
  128. CreateZip(Application.dataPath, "StreamingAssets/ReadWritePath", "ReadWritePath.zip");
  129. AssetDatabase.Refresh();
  130. }
  131. [MenuItem("NeoGame/excel 转 Jsonpart 文件", false)]
  132. public static void Excel2Jsonfiles()
  133. {
  134. DataManager.Instance.InitData();
  135. GamePredata.Instance.InitData();
  136. ConfigEditor.Excel2Jsonparts();
  137. EditorUtility.DisplayDialog("Editor Message", "执行完毕", "OK", "");
  138. }
  139. [MenuItem("NeoGame/Jsonparts 转 excel 文件", false)]
  140. public static void Jsonparts2Excel()
  141. {
  142. ConfigEditor.Jsonparts2Excel();
  143. EditorUtility.DisplayDialog("Editor Message", "执行完毕", "OK", "");
  144. }
  145. /**
  146. * Draw GUI in editor mode
  147. */
  148. public const string TAB_TITLE = "Battle Editor";
  149. public BattleFieldSceneCreator()
  150. {
  151. this.titleContent = new GUIContent(TAB_TITLE);
  152. this.Reload();
  153. }
  154. static void CreateZip(string projectPath, string toZipPath, string zipName, string exclude = null)
  155. {
  156. string zipFolder = Path.Combine(projectPath, toZipPath);
  157. string zippTarget = Path.Combine(projectPath, "StreamingAssets/" + zipName);
  158. if (!Directory.Exists(zipFolder))
  159. return;
  160. if (File.Exists(zippTarget)) File.Delete(zippTarget);
  161. int CompressLevel = 9;
  162. ZipClass zc = new ZipClass();
  163. zc.ZipDirectory(zipFolder, zippTarget, CompressLevel, exclude);
  164. }
  165. //
  166. private static int brokenTargetGroupId = 0;
  167. private static int brokenChallengeDataId = 0;
  168. private static string targetResourceName = string.Empty;
  169. private static List<int> targetSearchGroupIds = new List<int>();
  170. //
  171. private static string toaddSceneName = string.Empty;
  172. //
  173. private static string tmplTargetGroupName = string.Empty;
  174. // Subeditor state
  175. private int subeditorState = 1;
  176. public const int BATTLE_FIELD_STATE_CODE = 1;
  177. public const int TARGET_DATA_STATE_CODE = 2;
  178. public const int AVATAR_DATA_STATE_CODE = 4;
  179. public const int WEAPON_DATA_STATE_CODE = 8;
  180. public const int ORIWEAPON_DATA_STATE_CODE = 16;
  181. public const int TARGET_TOOLKIT_STATE_CODE = 32;
  182. public const int TARGET_PATHFINDIND_STATE_CODE = 64;
  183. // Battle Field
  184. private int currBattleLevelId = 1;
  185. private void OnGUI()
  186. {
  187. GUILayout.BeginVertical();
  188. GUILayout.BeginHorizontal();
  189. //
  190. if (GUILayout.Button("重新载入"))
  191. {
  192. this.Reload();
  193. }
  194. GUILayout.EndHorizontal();
  195. //
  196. var battleFieldEditorExpanded = (subeditorState & BATTLE_FIELD_STATE_CODE) > 0;
  197. GUILayout.Space(10f);
  198. GUILayout.BeginHorizontal();
  199. if (GUILayout.Button(battleFieldEditorExpanded ? "-" : "+"))
  200. {
  201. subeditorState ^= BATTLE_FIELD_STATE_CODE;
  202. }
  203. GUILayout.Space(5f);
  204. GUILayout.Label("战场数据维护", EditorStyles.boldLabel);
  205. GUILayout.FlexibleSpace();
  206. GUILayout.EndHorizontal();
  207. if (battleFieldEditorExpanded) this.DrawBattleFieldEditorGUI(ref currBattleLevelId);
  208. /**
  209. * 靶子数据维护
  210. */
  211. var targetDataEditorExpanded = (subeditorState & TARGET_DATA_STATE_CODE) > 0;
  212. GUILayout.Space(10f);
  213. GUILayout.BeginHorizontal();
  214. if (GUILayout.Button(targetDataEditorExpanded ? "-" : "+"))
  215. {
  216. subeditorState ^= TARGET_DATA_STATE_CODE;
  217. }
  218. GUILayout.Space(5f);
  219. GUILayout.Label("靶子数据维护", EditorStyles.boldLabel);
  220. GUILayout.FlexibleSpace();
  221. GUILayout.EndHorizontal();
  222. if (targetDataEditorExpanded) this.DrawBattleTargetEditorGUI();
  223. /**
  224. * TO BE CONTINUE
  225. */
  226. GUILayout.FlexibleSpace();
  227. GUILayout.BeginHorizontal();
  228. GUILayout.FlexibleSpace();
  229. GUILayout.Label("TO BE CONTINUE", EditorStyles.boldLabel);
  230. GUILayout.FlexibleSpace();
  231. GUILayout.EndHorizontal();
  232. //
  233. GUILayout.BeginHorizontal();
  234. if (GUILayout.Button("Add Distance Sign"))
  235. {
  236. AddDistanceSigns(130);
  237. }
  238. // Clear battle field scene
  239. if (GUILayout.Button("Clear Scene"))
  240. {
  241. ClearScene();
  242. }
  243. GUILayout.EndHorizontal();
  244. //
  245. GUILayout.EndVertical();
  246. }
  247. public void Reload()
  248. {
  249. this.ClearBattleFieldEditorDataCache();
  250. this.ClearBattleTargetEditorDataCache();
  251. ConfigEditor.ClearCacheData();
  252. }
  253. public void ChangeCurrBattleLevelId(int id)
  254. {
  255. this.currBattleLevelId = id;
  256. }
  257. private int testVar = 1;
  258. private static void ProcessCommand(string command, string workDir, string argument, bool isShell)
  259. {
  260. ProcessStartInfo start = new ProcessStartInfo(command);
  261. start.FileName = command;
  262. start.WorkingDirectory = workDir;
  263. start.Arguments = argument;
  264. start.CreateNoWindow = false;
  265. start.ErrorDialog = true;
  266. start.UseShellExecute = isShell;
  267. if (start.UseShellExecute)
  268. {
  269. start.RedirectStandardOutput = false;
  270. start.RedirectStandardError = false;
  271. start.RedirectStandardInput = false;
  272. }
  273. else
  274. {
  275. start.RedirectStandardOutput = true;
  276. start.RedirectStandardError = true;
  277. start.RedirectStandardInput = true;
  278. start.StandardOutputEncoding = System.Text.UTF8Encoding.UTF8;
  279. start.StandardErrorEncoding = System.Text.UTF8Encoding.UTF8;
  280. }
  281. Process p = Process.Start(start);
  282. if (p != null)
  283. {
  284. p.WaitForExit(100000);
  285. }
  286. if (!start.UseShellExecute)
  287. {
  288. PrintOutPut(p.StandardOutput);
  289. PrintOutPut(p.StandardError);
  290. }
  291. }
  292. private static void PrintOutPut(StreamReader reader)
  293. {
  294. string msg = reader.ReadToEnd();
  295. reader.Close();
  296. UnityEngine.Debug.Log(msg);
  297. }
  298. }
  299. }