XGameConfigInspector.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEditor;
  4. using UnityEditorInternal;
  5. using UnityEngine;
  6. using XGame.Framework;
  7. namespace XGame.Editor
  8. {
  9. [CustomEditor(typeof(XGameConfig))]
  10. internal class XGameConfigInspector : UnityEditor.Editor
  11. {
  12. private SerializedProperty script;
  13. private SerializedProperty productName;
  14. /// <summary>
  15. /// 项目的启动Assembly GUID
  16. /// </summary>
  17. public SerializedProperty launchAssemblyGUID;
  18. /// <summary>
  19. /// 项目的启动Assembly 名字
  20. /// </summary>
  21. public SerializedProperty launchAssemblyName;
  22. private SerializedProperty launchSceneGUID;
  23. private SerializedProperty launchSceneName;
  24. /// <summary>
  25. /// 项目的启动脚本GUID
  26. /// </summary>
  27. private SerializedProperty gameLogicGUID;
  28. /// <summary>
  29. /// 项目的启动脚本名字
  30. /// </summary>
  31. private SerializedProperty gameLogicTypeName;
  32. private string _productName;
  33. private AssemblyDefinitionAsset _launchAssembly;
  34. private SceneAsset _launchScene;
  35. private MonoScript _gameLogicScript;
  36. private HashSet<Action> _lsApplies = new HashSet<Action>();
  37. private void OnEnable()
  38. {
  39. script = serializedObject.FindProperty("m_Script");
  40. productName = serializedObject.FindProperty("productName");
  41. launchAssemblyGUID = serializedObject.FindProperty("launchAssemblyGUID");
  42. launchAssemblyName = serializedObject.FindProperty("launchAssemblyName");
  43. launchSceneGUID = serializedObject.FindProperty("launchSceneGUID");
  44. launchSceneName = serializedObject.FindProperty("launchSceneName");
  45. gameLogicGUID = serializedObject.FindProperty("gameLogicGUID");
  46. gameLogicTypeName = serializedObject.FindProperty("gameLogicTypeName");
  47. _productName = productName.stringValue;
  48. if (string.IsNullOrEmpty(launchAssemblyGUID.stringValue))
  49. {
  50. _launchAssembly = null;
  51. }
  52. else
  53. {
  54. _launchAssembly = AssetDatabase.LoadAssetAtPath<AssemblyDefinitionAsset>(AssetDatabase.GUIDToAssetPath(launchAssemblyGUID.stringValue));
  55. }
  56. if (string.IsNullOrEmpty(launchSceneGUID.stringValue))
  57. {
  58. _launchScene = null;
  59. }
  60. else
  61. {
  62. _launchScene = AssetDatabase.LoadAssetAtPath<SceneAsset>(AssetDatabase.GUIDToAssetPath(launchSceneGUID.stringValue));
  63. }
  64. if (string.IsNullOrEmpty(gameLogicGUID.stringValue))
  65. {
  66. _gameLogicScript = null;
  67. }
  68. else
  69. {
  70. _gameLogicScript = AssetDatabase.LoadAssetAtPath<MonoScript>(AssetDatabase.GUIDToAssetPath(gameLogicGUID.stringValue));
  71. }
  72. }
  73. private void OnDisable()
  74. {
  75. _lsApplies.Clear();
  76. }
  77. public override void OnInspectorGUI()
  78. {
  79. serializedObject.Update();
  80. EditorGUILayout.BeginVertical();
  81. {
  82. DrawScript();
  83. DrawSettings();
  84. DrawApply();
  85. serializedObject.ApplyModifiedProperties();
  86. }
  87. EditorGUILayout.EndVertical();
  88. }
  89. private void DrawScript()
  90. {
  91. EditorGUI.BeginDisabledGroup(true);
  92. EditorGUILayout.PropertyField(script);
  93. EditorGUI.EndDisabledGroup();
  94. GUILayout.Space(5);
  95. }
  96. private void DrawSettings()
  97. {
  98. //settings
  99. EditorGUILayout.BeginVertical("Box");
  100. {
  101. GUILayout.Space(5);
  102. GUIStyle labelStyle = new GUIStyle("Label")
  103. {
  104. alignment = TextAnchor.MiddleLeft,
  105. fontStyle = FontStyle.BoldAndItalic,
  106. fontSize = 15
  107. };
  108. EditorGUILayout.LabelField(new GUIContent("Settings:"), labelStyle, GUILayout.Height(20));
  109. GUILayout.Space(5);
  110. EditorGUILayout.BeginHorizontal();
  111. {
  112. GUILayout.Space(15);
  113. EditorGUILayout.BeginVertical("Box");
  114. {
  115. GUILayout.Space(5);
  116. _productName = EditorGUILayout.TextField(new GUIContent("Product Name", "项目名字"), _productName);
  117. _launchAssembly = EditorGUILayout.ObjectField(new GUIContent("Launch Assembly", "项目的启动动态库"), _launchAssembly, typeof(AssemblyDefinitionAsset), true) as AssemblyDefinitionAsset;
  118. _launchScene = EditorGUILayout.ObjectField(new GUIContent("Launch Scene", "启动场景"), _launchScene, typeof(SceneAsset), true) as SceneAsset;
  119. _gameLogicScript = EditorGUILayout.ObjectField(new GUIContent("GameLogic", "启动脚本"), _gameLogicScript, typeof(MonoScript), true) as MonoScript;
  120. AddApply(() => productName.stringValue = _productName);
  121. AddApply(() =>
  122. {
  123. if (_launchAssembly != null)
  124. {
  125. launchAssemblyName.stringValue = _launchAssembly.name;
  126. launchAssemblyGUID.stringValue = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(_launchAssembly));
  127. }
  128. else
  129. {
  130. launchAssemblyName.stringValue = string.Empty;
  131. launchAssemblyGUID.stringValue = string.Empty;
  132. }
  133. });
  134. AddApply(() =>
  135. {
  136. if (_launchScene != null)
  137. {
  138. launchSceneName.stringValue = _launchScene.name;
  139. launchSceneGUID.stringValue = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(_launchScene));
  140. }
  141. else
  142. {
  143. launchSceneName.stringValue = string.Empty;
  144. launchSceneGUID.stringValue = string.Empty;
  145. }
  146. });
  147. AddApply(() =>
  148. {
  149. if (_gameLogicScript != null)
  150. {
  151. gameLogicTypeName.stringValue = _gameLogicScript.GetClass().AssemblyQualifiedName;
  152. gameLogicGUID.stringValue = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(_gameLogicScript));
  153. }
  154. else
  155. {
  156. gameLogicTypeName.stringValue = string.Empty;
  157. gameLogicGUID.stringValue = string.Empty;
  158. }
  159. });
  160. }
  161. EditorGUILayout.EndVertical();
  162. }
  163. EditorGUILayout.EndHorizontal();
  164. GUILayout.Space(20);
  165. }
  166. EditorGUILayout.EndVertical();
  167. }
  168. private void DrawApply()
  169. {
  170. EditorGUILayout.BeginHorizontal();
  171. EditorGUILayout.Separator();
  172. if (GUILayout.Button("Apply", GUILayout.Width(50)))
  173. {
  174. DoApply();
  175. }
  176. EditorGUILayout.EndHorizontal();
  177. }
  178. private void DoApply()
  179. {
  180. foreach (var action in _lsApplies) action.Invoke();
  181. serializedObject.ApplyModifiedProperties();
  182. AssetDatabase.SaveAssets();
  183. AssetDatabase.Refresh();
  184. }
  185. private void AddApply(Action apply)
  186. {
  187. if (apply != null)
  188. {
  189. _lsApplies.Add(apply);
  190. }
  191. }
  192. }
  193. }