Unity 框架

UIBehaviourInspector.cs 15KB

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