using System; using System.Collections; using System.Collections.Generic; using Sirenix.OdinInspector.Editor; using UnityEngine; namespace Ether { public abstract class OdinWindowBase : OdinMenuEditorWindow where T : OdinWindowBase { protected OdinWindowBase() { OnInit(); } protected abstract void OnInit(); protected bool SupportsMultiSelect = false; //允许多选 protected bool DrawSearchToolbar = false; //显示搜索框 protected List<(Type, OdinSubWindowBase)> SubWindows = new List<(Type, OdinSubWindowBase)>(); protected List SubWindowNames = new List(); private OdinSubWindowBase selectedSubWindow; /// /// 添加子界面 /// /// 子界面名称(列表中显示) /// 子界面类型 protected void AddSubWindow(string windowName) where K : OdinSubWindowBase { Type type = typeof(K); OdinSubWindowBase subWindow = (OdinSubWindowBase)Activator.CreateInstance(type); SubWindowNames.Add(windowName); SubWindows.Add((type, subWindow)); } protected override OdinMenuTree BuildMenuTree() { var tree = new OdinMenuTree(); tree.Selection.SupportsMultiSelect = SupportsMultiSelect; tree.Config.DrawSearchToolbar = DrawSearchToolbar; for (int i = 0; i < SubWindows.Count; i++) { var subWindowItem = SubWindows[i]; if (subWindowItem.Item2 != null) continue; Type type = subWindowItem.Item1; OdinSubWindowBase subWindow = (OdinSubWindowBase)Activator.CreateInstance(type); subWindowItem.Item2 = subWindow; } for (int i = 0; i < SubWindows.Count; i++) { tree.Add(SubWindowNames[i], SubWindows[i].Item2); } tree.Selection.SelectionChanged += (type) => { if (tree.Selection.SelectedValue == null) { return; } selectedSubWindow?.OnClose(); OdinSubWindowBase selectWindow = tree.Selection.SelectedValue as OdinSubWindowBase; selectWindow.OnShow(); selectedSubWindow = selectWindow; }; return tree; } protected override void OnImGUI() { base.OnImGUI(); if (this.MenuTree.Selection.SelectedValue == null) { return; } OdinSubWindowBase selectWindow = this.MenuTree.Selection.SelectedValue as OdinSubWindowBase; selectWindow.OnGUI(); } protected override void OnDestroy() { selectedSubWindow?.OnClose(); } } }