ETPackageAutoTool.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #if ODIN_INSPECTOR
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Reflection;
  5. using Sirenix.OdinInspector.Editor;
  6. using UnityEditor;
  7. using UnityEngine;
  8. using Type = System.Type;
  9. namespace ET.PackageManager.Editor
  10. {
  11. /// <summary>
  12. /// ET包管理 自动化工具
  13. /// </summary>
  14. public class ETPackageAutoTool : OdinMenuEditorWindow
  15. {
  16. [MenuItem("ET/ETPackage 包管理自动化工具")]
  17. public static void OpenWindow()
  18. {
  19. var window = GetWindow<ETPackageAutoTool>("ET包管理");
  20. if (window != null)
  21. window.Show();
  22. }
  23. //[MenuItem("ET/关闭ETPackage 包管理自动化工具")]
  24. //错误时使用的 面板出现了错误 会导致如何都打不开 就需要先关闭
  25. public static void CloseWindow()
  26. {
  27. GetWindow<ETPackageAutoTool>()?.Close();
  28. }
  29. //关闭后刷新资源
  30. public static void CloseWindowRefresh()
  31. {
  32. CloseWindow();
  33. AssetDatabase.SaveAssets();
  34. //AssetDatabase.Refresh();//下面的刷新更NB
  35. EditorApplication.ExecuteMenuItem("Assets/Refresh");
  36. }
  37. public static void UnloadAllAssets()
  38. {
  39. PackageHelper.Unload();
  40. PackageVersionHelper.Unload();
  41. }
  42. public static void ReLoadAllAssets()
  43. {
  44. PackageHelper.LoadAsset();
  45. PackageVersionHelper.LoadAsset();
  46. }
  47. private OdinMenuTree m_OdinMenuTree;
  48. private List<BaseTreeMenuItem> m_AllMenuItem = new List<BaseTreeMenuItem>();
  49. protected override OdinMenuTree BuildMenuTree()
  50. {
  51. m_OdinMenuTree = new OdinMenuTree();
  52. m_OdinMenuTree.Selection.SelectionChanged += OnSelectionChanged;
  53. m_AllMenuItem.Clear();
  54. var assembly = GetAssembly("ET.PackageManager.Editor");
  55. if (assembly == null) return null;
  56. Type[] types = assembly.GetTypes();
  57. var allAutoMenus = new List<ETPackageAutoMenuData>();
  58. foreach (Type type in types)
  59. {
  60. if (type.IsDefined(typeof(ETPackageMenuAttribute), false))
  61. {
  62. ETPackageMenuAttribute attribute = (ETPackageMenuAttribute)Attribute.GetCustomAttribute(type, typeof(ETPackageMenuAttribute));
  63. allAutoMenus.Add(new ETPackageAutoMenuData
  64. {
  65. Type = type,
  66. MenuName = attribute.MenuName,
  67. Order = attribute.Order
  68. });
  69. }
  70. }
  71. allAutoMenus.Sort((a, b) => a.Order.CompareTo(b.Order));
  72. foreach (var attribute in allAutoMenus)
  73. {
  74. m_AllMenuItem.Add(NewTreeMenuItem(attribute.Type, attribute.MenuName));
  75. }
  76. return m_OdinMenuTree;
  77. }
  78. public static Assembly GetAssembly(string assemblyName)
  79. {
  80. var assemblies = AppDomain.CurrentDomain.GetAssemblies();
  81. foreach (var assemblie in assemblies)
  82. {
  83. if (assemblie.GetName().Name == assemblyName)
  84. {
  85. return assemblie;
  86. }
  87. }
  88. Debug.LogError($"没有找到这个程序集 {assemblyName}");
  89. return null;
  90. }
  91. private BaseTreeMenuItem NewTreeMenuItem(Type moduleType, string moduleName)
  92. {
  93. var treeMenuItemType = typeof(TreeMenuItem<>);
  94. var specificTreeMenuItemType = treeMenuItemType.MakeGenericType(moduleType);
  95. var constructor = specificTreeMenuItemType.GetConstructor(new Type[]
  96. {
  97. typeof(OdinMenuEditorWindow),
  98. typeof(OdinMenuTree),
  99. typeof(string)
  100. });
  101. object treeMenuItem = constructor.Invoke(new object[]
  102. {
  103. this,
  104. m_OdinMenuTree,
  105. moduleName
  106. });
  107. return (BaseTreeMenuItem)treeMenuItem;
  108. }
  109. private bool m_FirstInit = true;
  110. private StringPrefs m_LastSelectMenuPrefs = new("ETPackageAutoTool_LastSelectMenu");
  111. private void OnSelectionChanged(SelectionChangedType obj)
  112. {
  113. if (obj != SelectionChangedType.ItemAdded)
  114. {
  115. return;
  116. }
  117. var lastMenuName = m_LastSelectMenuPrefs.Value;
  118. if (m_FirstInit)
  119. {
  120. m_FirstInit = false;
  121. foreach (var menu in m_OdinMenuTree.MenuItems)
  122. {
  123. if (string.IsNullOrEmpty(lastMenuName))
  124. {
  125. lastMenuName = menu.Name;
  126. m_LastSelectMenuPrefs.Value = menu.Name;
  127. }
  128. if (menu.Name != lastMenuName) continue;
  129. menu.Select();
  130. return;
  131. }
  132. return;
  133. }
  134. if (m_OdinMenuTree.Selection.SelectedValue is BaseTreeMenuItem menuItem)
  135. {
  136. menuItem.SelectionMenu();
  137. }
  138. foreach (var menu in m_OdinMenuTree.MenuItems)
  139. {
  140. if (!menu.IsSelected) continue;
  141. m_LastSelectMenuPrefs.Value = menu.Name;
  142. break;
  143. }
  144. }
  145. protected override void Initialize()
  146. {
  147. base.Initialize();
  148. }
  149. protected override void OnDestroy()
  150. {
  151. base.OnDestroy();
  152. foreach (var menuItem in m_AllMenuItem)
  153. {
  154. menuItem.OnDestroy();
  155. }
  156. }
  157. }
  158. }
  159. #endif