BaseTreeMenuItem.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #if ODIN_INSPECTOR
  2. using Sirenix.OdinInspector;
  3. using Sirenix.OdinInspector.Editor;
  4. using Sirenix.Utilities.Editor;
  5. using UnityEngine;
  6. namespace ET.PackageManager.Editor
  7. {
  8. [HideReferenceObjectPicker]
  9. public abstract class BaseTreeMenuItem : BasePackageToolModule
  10. {
  11. private bool m_InitEnd;
  12. public BaseTreeMenuItem()
  13. {
  14. }
  15. protected BaseTreeMenuItem(OdinMenuEditorWindow autoTool, OdinMenuTree tree)
  16. {
  17. AutoTool = autoTool;
  18. Tree = tree;
  19. }
  20. public override void SelectionMenu()
  21. {
  22. Init();
  23. Select();
  24. }
  25. private void Init()
  26. {
  27. if (m_InitEnd) return;
  28. Initialize();
  29. m_InitEnd = true;
  30. }
  31. protected abstract void Select();
  32. }
  33. [HideReferenceObjectPicker]
  34. public class TreeMenuItem<T> : BaseTreeMenuItem where T : BasePackageToolModule, new()
  35. {
  36. [ShowInInspector]
  37. [HideLabel]
  38. [HideReferenceObjectPicker]
  39. public T Instance { get; internal set; }
  40. public TreeMenuItem()
  41. {
  42. }
  43. public TreeMenuItem(OdinMenuEditorWindow autoTool, OdinMenuTree tree, string menuName, Texture icon) : base(autoTool,
  44. tree)
  45. {
  46. Tree.Add(menuName, this, icon);
  47. ModuleName = menuName;
  48. }
  49. public TreeMenuItem(OdinMenuEditorWindow autoTool, OdinMenuTree tree, string menuName, EditorIcon icon) : base(autoTool,
  50. tree)
  51. {
  52. Tree.Add(menuName, this, icon);
  53. ModuleName = menuName;
  54. }
  55. public TreeMenuItem(OdinMenuEditorWindow autoTool, OdinMenuTree tree, string menuName) : base(autoTool,
  56. tree)
  57. {
  58. Tree.Add(menuName, this);
  59. ModuleName = menuName;
  60. }
  61. public override void Initialize()
  62. {
  63. Instance = new T
  64. {
  65. AutoTool = AutoTool,
  66. Tree = Tree,
  67. ModuleName = ModuleName,
  68. };
  69. Instance.UserData = this.UserData;
  70. Instance.Initialize();
  71. }
  72. public override void OnDestroy()
  73. {
  74. Instance?.OnDestroy();
  75. }
  76. protected override void Select()
  77. {
  78. Instance?.SelectionMenu();
  79. }
  80. }
  81. }
  82. #endif