Unity 框架

UIBehaviourInspector.cs 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. using TFramework;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEditor;
  6. using UnityEditorInternal;
  7. using System.IO;
  8. using System.Text;
  9. namespace TFramework
  10. {
  11. [CustomEditor(typeof(UIBehaviour))]
  12. public class UIBehaviourInspector : TEditor
  13. {
  14. SerializedProperty m_id;
  15. SerializedProperty enableAnim;
  16. SerializedProperty m_uiAnimType;
  17. SerializedProperty isScriptUse;
  18. SerializedProperty variableInfos;
  19. SerializedProperty parnetPanel;
  20. Component[] behaviours;
  21. ReorderableList variableList;
  22. List<string> anims = new List<string>();
  23. int select = 0;
  24. List<string> parentPanles = new List<string>();
  25. int selectParent = -1;
  26. Dictionary<string, string> scriptPathDic = new Dictionary<string, string>();
  27. string scriptText;
  28. const string INITFLGS = "base.OnInit();";
  29. private void OnEnable()
  30. {
  31. m_id = serializedObject.FindProperty("m_id");
  32. enableAnim = serializedObject.FindProperty("enableAnim");
  33. m_uiAnimType = serializedObject.FindProperty("m_uiAnimType");
  34. isScriptUse = serializedObject.FindProperty("isScriptUse");
  35. variableInfos = serializedObject.FindProperty("variableInfos");
  36. parnetPanel = serializedObject.FindProperty("parnetPanel");
  37. if (string.IsNullOrEmpty(m_id.stringValue))
  38. {
  39. (target as UIBehaviour).ResetID();
  40. }
  41. anims.Add("None");
  42. foreach (var item in GlobalTool.GetTypesInRunTimeAssemblies(type =>
  43. (type.IsSubclassOf(typeof(UIAnimBase)) || (type.BaseType != null && type.BaseType.IsGenericType && type.BaseType.GetGenericTypeDefinition() == typeof(UIAnimBase)))
  44. && type.IsClass && !type.IsAbstract && type.Name != typeof(UIAnimBase).Name))
  45. {
  46. anims.Add(item.FullName);
  47. }
  48. behaviours = (target as UIBehaviour).GetComponents<Component>();
  49. foreach (var item in GlobalTool.GetTypesInRunTimeAssemblies(type =>
  50. (type.IsSubclassOf(typeof(BasePanel)) || (type.BaseType != null && type.BaseType.IsGenericType && type.BaseType.GetGenericTypeDefinition() == typeof(BasePanel)))
  51. && type.IsClass && !type.IsAbstract && type.Name != typeof(BasePanel).Name))
  52. {
  53. parentPanles.Add(item.FullName);
  54. string[] guids = AssetDatabase.FindAssets(item.Name + ".Designer t:Script");
  55. if (guids.Length <= 0)
  56. guids = AssetDatabase.FindAssets(item.Name + " t:Script");
  57. if (guids.Length > 0)
  58. {
  59. string scriptPath = AssetDatabase.GUIDToAssetPath(guids[0]);
  60. if (!scriptPathDic.ContainsKey(item.FullName))
  61. scriptPathDic.Add(item.FullName, scriptPath);
  62. }
  63. }
  64. variableList = new ReorderableList(serializedObject, variableInfos, false, true, true, true);
  65. variableList.drawHeaderCallback = (rect) =>
  66. {
  67. GUI.Label(rect, "绑定组件");
  68. };
  69. variableList.drawElementCallback = (rect, index, isActive, isFocused) =>
  70. {
  71. SerializedProperty property = variableInfos.GetArrayElementAtIndex(index);
  72. SerializedProperty variableName = property.FindPropertyRelative("variableName");
  73. SerializedProperty compentType = property.FindPropertyRelative("compentType");
  74. float width = rect.width / 2;
  75. rect.width = 63;
  76. GUI.Label(rect, "变量名:");
  77. rect.x += 63;
  78. rect.width = width - 63;
  79. string newName = EditorGUI.TextField(rect, variableName.stringValue);
  80. if (newName != variableName.stringValue)
  81. {
  82. if (!string.IsNullOrEmpty(compentType.stringValue))
  83. {
  84. int indexPos = scriptText.IndexOf($"private {compentType.stringValue} {variableName.stringValue};");
  85. if (indexPos == -1)
  86. {
  87. indexPos = scriptText.IndexOf("#endregion");
  88. scriptText = scriptText.Insert(indexPos, $"private {compentType.stringValue} {newName};\n ");
  89. }
  90. else
  91. {
  92. scriptText = scriptText.Replace($"private {compentType.stringValue} {variableName.stringValue};", $"private {compentType.stringValue} {newName};");
  93. }
  94. int initPos = scriptText.IndexOf($" {variableName.stringValue} = GetComponentInUIID<{compentType.stringValue}>(\"{m_id.stringValue}\");");
  95. if (initPos == -1)
  96. {
  97. initPos = scriptText.IndexOf(INITFLGS) + INITFLGS.Length;
  98. scriptText = scriptText.Insert(initPos, $"\n {newName} = GetComponentInUIID<{compentType.stringValue}>(\"{m_id.stringValue}\");");
  99. }
  100. else
  101. {
  102. scriptText = scriptText.Replace($" {variableName.stringValue} = GetComponentInUIID<{compentType.stringValue}>(\"{m_id.stringValue}\");",
  103. $" {newName} = GetComponentInUIID<{compentType.stringValue}>(\"{m_id.stringValue}\");");
  104. }
  105. string path = Application.dataPath.Remove(Application.dataPath.LastIndexOf("Assets"));
  106. path += scriptPathDic[parnetPanel.stringValue];
  107. StreamWriter baseWriter = new StreamWriter(path, false, UTF8Encoding.UTF8);
  108. baseWriter.Write(scriptText);
  109. baseWriter.Close();
  110. AssetDatabase.Refresh();
  111. }
  112. variableName.stringValue = newName;
  113. }
  114. rect.x += rect.width + 3;
  115. rect.width = 80;
  116. GUI.Label(rect, "组件类型:");
  117. GenericMenu menu = new GenericMenu();
  118. foreach (var item in behaviours)
  119. {
  120. menu.AddItem(new GUIContent(item.GetType().Name), compentType.stringValue == item.GetType().Name, () =>
  121. {
  122. serializedObject.Update();
  123. string oldType = compentType.stringValue;
  124. compentType.stringValue = item.GetType().Name;
  125. if (!variableName.stringValue.Contains(compentType.stringValue))
  126. {
  127. variableName.stringValue += compentType.stringValue;
  128. }
  129. int index = scriptText.IndexOf($"private {oldType} {variableName.stringValue};");
  130. if (index == -1)
  131. {
  132. index = scriptText.IndexOf("#endregion");
  133. scriptText = scriptText.Insert(index, $"private {compentType.stringValue} {variableName.stringValue};\n ");
  134. }
  135. else
  136. {
  137. scriptText = scriptText.Replace($"private {oldType} {variableName.stringValue};", $"private {compentType.stringValue} {variableName.stringValue};");
  138. }
  139. int initPos = scriptText.IndexOf($" {variableName.stringValue} = GetComponentInUIID<{oldType}>(\"{m_id.stringValue}\");");
  140. if (initPos == -1)
  141. {
  142. initPos = scriptText.IndexOf(INITFLGS) + INITFLGS.Length;
  143. scriptText = scriptText.Insert(initPos, $"\n {variableName.stringValue} = GetComponentInUIID<{compentType.stringValue}>(\"{m_id.stringValue}\");");
  144. }
  145. else
  146. {
  147. scriptText = scriptText.Replace($" {variableName.stringValue} = GetComponentInUIID<{oldType}>(\"{m_id.stringValue}\");",
  148. $" {variableName.stringValue} = GetComponentInUIID<{compentType.stringValue}>(\"{m_id.stringValue}\");");
  149. }
  150. if (!scriptText.Contains($"using {item.GetType().Namespace};"))
  151. {
  152. scriptText = scriptText.Insert(0, $"using {item.GetType().Namespace};\n");
  153. }
  154. string path = Application.dataPath.Remove(Application.dataPath.LastIndexOf("Assets"));
  155. path += scriptPathDic[parnetPanel.stringValue];
  156. StreamWriter baseWriter = new StreamWriter(path, false, UTF8Encoding.UTF8);
  157. baseWriter.Write(scriptText);
  158. baseWriter.Close();
  159. AssetDatabase.Refresh();
  160. serializedObject.ApplyModifiedProperties();
  161. });
  162. }
  163. rect.x += 63;
  164. rect.width = width - 80;
  165. if (GUI.Button(rect, compentType.stringValue))
  166. {
  167. menu.ShowAsContext();
  168. }
  169. };
  170. variableList.multiSelect = false;
  171. variableList.onAddCallback = (list) =>
  172. {
  173. serializedObject.Update();
  174. int newIndex = list.serializedProperty.arraySize;
  175. list.serializedProperty.InsertArrayElementAtIndex(newIndex);
  176. list.index = newIndex;
  177. SerializedProperty newItem = list.serializedProperty.GetArrayElementAtIndex(newIndex);
  178. newItem.FindPropertyRelative("variableName").stringValue = (target as UIBehaviour).gameObject.name + "_";
  179. newItem.FindPropertyRelative("compentType").stringValue = "UIBehaviour";
  180. serializedObject.ApplyModifiedProperties();
  181. };
  182. variableList.onRemoveCallback = (list) =>
  183. {
  184. SerializedProperty property = variableInfos.GetArrayElementAtIndex(list.index);
  185. SerializedProperty variableName = property.FindPropertyRelative("variableName");
  186. SerializedProperty compentType = property.FindPropertyRelative("compentType");
  187. int index = scriptText.IndexOf($"private {compentType.stringValue} {variableName.stringValue};");
  188. if (index != -1)
  189. scriptText = scriptText.Replace($"private {compentType.stringValue} {variableName.stringValue};\n ", "");
  190. int initPos = scriptText.IndexOf($" {variableName.stringValue} = GetComponentInUIID<{compentType.stringValue}>(\"{m_id.stringValue}\");");
  191. if (initPos != -1)
  192. {
  193. scriptText = scriptText.Replace($"\n {variableName.stringValue} = GetComponentInUIID<{compentType.stringValue}>(\"{m_id.stringValue}\");", "");
  194. }
  195. string path = Application.dataPath.Remove(Application.dataPath.LastIndexOf("Assets"));
  196. path += scriptPathDic[parnetPanel.stringValue];
  197. StreamWriter baseWriter = new StreamWriter(path, false, UTF8Encoding.UTF8);
  198. baseWriter.Write(scriptText);
  199. baseWriter.Close();
  200. AssetDatabase.Refresh();
  201. ReorderableList.defaultBehaviours.DoRemoveButton(list);
  202. };
  203. if (!string.IsNullOrEmpty(parnetPanel.stringValue))
  204. scriptText = AssetDatabase.LoadAssetAtPath<TextAsset>(scriptPathDic[parnetPanel.stringValue]).text;
  205. }
  206. public override void OnInspectorGUI()
  207. {
  208. serializedObject.Update();
  209. using (new EditorGUILayout.HorizontalScope())
  210. {
  211. GUI.enabled = false;
  212. EditorGUILayout.PropertyField(m_id, new GUIContent("ID"));
  213. GUI.enabled = true;
  214. if(GUILayout.Button("重置",GUILayout.ExpandWidth(false)))
  215. {
  216. (target as UIBehaviour).ResetID();
  217. }
  218. DrawGUILayoutCopyButton(m_id.stringValue);
  219. }
  220. EditorGUILayout.PropertyField(enableAnim, new GUIContent("启用动画"));
  221. if(enableAnim.boolValue)
  222. {
  223. select = anims.IndexOf(m_uiAnimType.stringValue);
  224. select = select == -1 ? 0 : select;
  225. int newSelect = EditorGUILayout.Popup("动画类型", select, anims.ToArray());
  226. if(newSelect!=select)
  227. {
  228. m_uiAnimType.stringValue = anims[newSelect];
  229. select = newSelect;
  230. }
  231. }
  232. EditorGUILayout.PropertyField(isScriptUse, new GUIContent("绑定脚本"));
  233. if(isScriptUse.boolValue)
  234. {
  235. using(new EditorGUILayout.VerticalScope("Box"))
  236. {
  237. selectParent = parentPanles.IndexOf(parnetPanel.stringValue);
  238. //selectParent = selectParent == -1 ? 0 : selectParent;
  239. int newParent = EditorGUILayout.Popup("绑定到面板", selectParent, parentPanles.ToArray());
  240. if (newParent != selectParent)
  241. {
  242. parnetPanel.stringValue = parentPanles[newParent];
  243. selectParent = newParent;
  244. scriptText = string.Empty;
  245. scriptText = AssetDatabase.LoadAssetAtPath<TextAsset>(scriptPathDic[parnetPanel.stringValue]).text;
  246. }
  247. variableList.DoLayoutList();
  248. }
  249. }
  250. serializedObject.ApplyModifiedProperties();
  251. }
  252. }
  253. }