IndieGame/client/Assets/Ether/Editor/Extension/OdinWindow/OdinWindowBase.cs

99 lines
2.4 KiB
C#
Raw Normal View History

using System;
using System.Collections;
using System.Collections.Generic;
using Sirenix.OdinInspector.Editor;
using UnityEngine;
namespace Ether
{
public abstract class OdinWindowBase<T> : OdinMenuEditorWindow where T : OdinWindowBase<T>
{
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<string> SubWindowNames = new List<string>();
private OdinSubWindowBase selectedSubWindow;
/// <summary>
/// 添加子界面
/// </summary>
/// <param name="windowName">子界面名称(列表中显示)</param>
/// <typeparam name="K">子界面类型</typeparam>
protected void AddSubWindow<K>(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();
}
}
}