Unity 框架

BatchTool.cs 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. using System;
  6. using UnityEngine.UI;
  7. using TMPro;
  8. using System.Reflection;
  9. namespace TFramework
  10. {
  11. public class BatchTool : EditorWindow
  12. {
  13. static BatchTool win;
  14. static Transform _root;
  15. string[] _operate = new string[] { "更改UI显示优先级","替换材质" ,"规范命名","组件批处理"};
  16. int _currentOpIndex=0;
  17. private Rect _splitRect = Rect.zero;
  18. private float _splitterWidth = 5;
  19. private bool _isDrag = false;
  20. private float _minWidth = 60;
  21. private float _maxWidth = 250;
  22. private float _opMenuWidth = 100;
  23. Material _overlay;
  24. Material _meshPro;
  25. [MenuItem("TFramework/批处理")]
  26. private static void OpenWin()
  27. {
  28. win = GetWindow<BatchTool>();
  29. win.titleContent.text = "批处理工具";
  30. win.minSize = new Vector2(300, 500);
  31. win.maxSize = new Vector2(500, 500);
  32. win.Show();
  33. }
  34. private void OnEnable()
  35. {
  36. _overlay = AssetDatabase.LoadAssetAtPath<Material>("Assets/TFramework/Runtime/Materails/UI/Overlay.mat");
  37. _meshPro = AssetDatabase.LoadAssetAtPath<Material>("Assets/TFramework/Runtime/Materails/UI/OverlayMeshProUGUI.mat");
  38. InitComponentBatch();
  39. }
  40. private void OnGUI()
  41. {
  42. using(new EditorGUILayout.HorizontalScope())
  43. {
  44. EditorGUILayout.LabelField("根节点");
  45. _root = EditorGUILayout.ObjectField(_root, typeof(Transform), true) as Transform;
  46. }
  47. GUILayout.BeginHorizontal();
  48. DrawOpMenu();
  49. SplitterGUI();
  50. ExecuteOp();
  51. GUILayout.EndHorizontal();
  52. EventHandle();
  53. }
  54. void DrawOpMenu()
  55. {
  56. GUILayout.BeginVertical("PreBackground", GUILayout.Width(_opMenuWidth));
  57. for (int i = 0; i < _operate.Length; i++)
  58. {
  59. string style = _currentOpIndex == i ? "TV Selection" : "PrefixLabel";
  60. int index = i;
  61. if (GUILayout.Button(_operate[i],style))
  62. {
  63. _currentOpIndex = index;
  64. }
  65. }
  66. GUILayout.EndVertical();
  67. }
  68. private void ExecuteOp()
  69. {
  70. switch (_operate[_currentOpIndex])
  71. {
  72. case "更改UI显示优先级":
  73. ChangeUIShowPriority();
  74. break;
  75. case "替换材质":
  76. GUILayout.Label("功能暂未实现");
  77. MatReplace();
  78. break;
  79. case "规范命名":
  80. GUILayout.Label("功能暂未实现");
  81. break;
  82. case "组件批处理":
  83. ComponentBatch();
  84. break;
  85. default:
  86. break;
  87. }
  88. }
  89. private void ChangeUIShowPriority()
  90. {
  91. GUILayout.BeginVertical();
  92. if(_root)
  93. {
  94. using(new EditorGUILayout.HorizontalScope())
  95. {
  96. GUILayout.Label("LegacyUI预备材质");
  97. _overlay = (Material)EditorGUILayout.ObjectField(_overlay, typeof(Material), true);
  98. }
  99. using (new EditorGUILayout.HorizontalScope())
  100. {
  101. GUILayout.Label("MeshProUI预备材质");
  102. _meshPro = (Material)EditorGUILayout.ObjectField(_meshPro, typeof(Material), true);
  103. }
  104. if (GUILayout.Button("开始更改"))
  105. {
  106. Graphic[] graphics = _root.GetComponentsInChildren<Graphic>(true);
  107. for (int i = 0; i < graphics.Length; i++)
  108. {
  109. if (graphics[i] is TextMeshProUGUI)
  110. graphics[i].materialForRendering.shader = _meshPro.shader;
  111. else
  112. {
  113. if (!graphics[i].material.name.Equals("Default UI Material")) continue;
  114. graphics[i].material = _overlay;
  115. }
  116. }
  117. EditorUtility.SetDirty(_root);
  118. AssetDatabase.SaveAssets();
  119. Log.Info("更改完成!");
  120. }
  121. }
  122. else
  123. {
  124. EditorGUILayout.LabelField("请指定一个根节点!");
  125. }
  126. GUILayout.EndVertical();
  127. }
  128. public static void MatReplace()
  129. {
  130. }
  131. private static MethodInfo _selectionAddMethod;
  132. private static Type _componentType;
  133. private static string _componentTypeFilter = "";
  134. private static bool _includeInactive = true;
  135. private static object[] _parameter = new object[1];
  136. public static void InitComponentBatch()
  137. {
  138. if(_selectionAddMethod==null)
  139. {
  140. MethodInfo[] methods = GlobalTool.GetTypeToCurrentDomain("UnityEditor.Selection").GetMethods(BindingFlags.Static | BindingFlags.NonPublic);
  141. for (int i = 0; i < methods.Length; i++)
  142. {
  143. if(methods[i].Name=="Add")
  144. {
  145. ParameterInfo[] parameter = methods[i].GetParameters();
  146. if(parameter!=null&&parameter.Length==1&&parameter[0].ParameterType.Name=="Object")
  147. {
  148. _selectionAddMethod = methods[i];
  149. break;
  150. }
  151. }
  152. }
  153. }
  154. }
  155. public static void ComponentBatch()
  156. {
  157. EditorGUILayout.BeginVertical();
  158. EditorGUILayout.BeginHorizontal();
  159. EditorGUILayout.HelpBox("批处理[ " + (_root ? _root.name : "Root") + " ]下的[ " + (_componentType != null ? _componentType.FullName : "Component") + " ]组件!", MessageType.Info);
  160. EditorGUILayout.EndHorizontal();
  161. EditorGUILayout.BeginHorizontal();
  162. GUILayout.Label("筛选词条:", GUILayout.Width(120));
  163. _componentTypeFilter = EditorGUILayout.TextField(_componentTypeFilter);
  164. EditorGUILayout.EndHorizontal();
  165. EditorGUILayout.BeginHorizontal();
  166. GUILayout.Label("组件:", GUILayout.Width(120));
  167. if (GUILayout.Button(_componentType != null ? _componentType.FullName : "<None>", "MiniPopup"))
  168. {
  169. GenericMenu gm = new GenericMenu();
  170. List<Type> types = GlobalTool.GetTypesInRunTimeAssemblies(type =>
  171. {
  172. return type.IsSubclassOf(typeof(Component)) && type.FullName.ToLower().Contains(_componentTypeFilter.ToLower());
  173. });
  174. gm.AddItem(new GUIContent("<None>"), _componentType == null, () =>
  175. {
  176. _componentType = null;
  177. });
  178. for (int i = 0; i < types.Count; i++)
  179. {
  180. Type type = types[i];
  181. gm.AddItem(new GUIContent(type.FullName.Replace(".", "/")), type == _componentType, () =>
  182. {
  183. _componentType = type;
  184. });
  185. }
  186. gm.ShowAsContext();
  187. }
  188. EditorGUILayout.EndHorizontal();
  189. EditorGUILayout.BeginHorizontal();
  190. GUILayout.Label("包含未激活对象:", GUILayout.Width(120));
  191. _includeInactive = EditorGUILayout.Toggle(_includeInactive);
  192. EditorGUILayout.EndHorizontal();
  193. GUI.enabled = _root && _componentType != null && _selectionAddMethod != null;
  194. EditorGUILayout.BeginHorizontal();
  195. GUI.backgroundColor = Color.green;
  196. if (GUILayout.Button("Collect"))
  197. {
  198. Selection.activeGameObject = null;
  199. Component[] components = _root.transform.GetComponentsInChildren(_componentType, _includeInactive);
  200. for (int i = 0; i < components.Length; i++)
  201. {
  202. _parameter[0] = components[i];
  203. _selectionAddMethod.Invoke(null, _parameter);
  204. }
  205. }
  206. GUI.backgroundColor = Color.white;
  207. EditorGUILayout.EndHorizontal();
  208. GUI.enabled = true;
  209. EditorGUILayout.EndVertical();
  210. }
  211. private void SplitterGUI()
  212. {
  213. GUILayout.Box("", "PreVerticalScrollbarThumb", GUILayout.Width(_splitterWidth), GUILayout.MaxWidth(_splitterWidth), GUILayout.MinWidth(_splitterWidth), GUILayout.ExpandHeight(true));
  214. _splitRect = GUILayoutUtility.GetLastRect();
  215. EditorGUIUtility.AddCursorRect(_splitRect, MouseCursor.SplitResizeLeftRight);
  216. }
  217. private void EventHandle()
  218. {
  219. if (Event.current != null)
  220. {
  221. switch (Event.current.rawType)
  222. {
  223. case EventType.MouseDown:
  224. GUI.FocusControl(null);
  225. if (_splitRect.Contains(Event.current.mousePosition))
  226. _isDrag = true;
  227. break;
  228. case EventType.MouseDrag:
  229. if (_isDrag)
  230. {
  231. _opMenuWidth += Event.current.delta.x;
  232. if (_opMenuWidth <= _minWidth)
  233. _opMenuWidth = _minWidth;
  234. else if (_opMenuWidth >= _maxWidth)
  235. _opMenuWidth = _maxWidth;
  236. GUI.changed = true;
  237. }
  238. break;
  239. case EventType.MouseUp:
  240. _isDrag = false;
  241. break;
  242. }
  243. }
  244. }
  245. }
  246. }