modular-avatar/Editor/Inspector/MAEditorBase.cs
bd_ ddbc3b164b
chore: restructure repository to put package at top-level (#477)
* chore: rearrange package structure to have the package at the root

* ci: update CI workflows

* ci: fixing workflow bugs

* ci: recurse building .zip package

* ci: more fixes

* ci: add back in the nadena.dev VPM repo

* ci: fix tests
2023-10-08 15:39:57 +09:00

83 lines
2.5 KiB
C#

using System.Reflection;
using UnityEditor.UIElements;
using UnityEngine.UIElements;
namespace nadena.dev.modular_avatar.core.editor
{
// This class performs common setup for Modular Avatar editors, including ensuring that only one instance of the\
// logo is rendered per container.
public abstract class MAEditorBase : UnityEditor.Editor
{
private MAVisualElement _visualElement;
private bool _suppressOnceDefaultMargins;
protected virtual VisualElement CreateInnerInspectorGUI()
{
return null;
}
private void RebuildUI()
{
CreateInspectorGUI();
}
public sealed override VisualElement CreateInspectorGUI()
{
if (_visualElement == null)
{
_visualElement = new MAVisualElement();
Localization.OnLangChange += RebuildUI;
}
else
{
_visualElement.Clear();
}
_visualElement.Add(new LogoElement());
// CreateInspectorElementFromEditor does a bunch of extra setup that makes our inspector look a little bit
// nicer. In particular, the label column won't auto-size if we just use IMGUIElement, for some reason
var inner = CreateInnerInspectorGUI();
bool innerIsImgui = (inner == null);
if (innerIsImgui)
{
var throwaway = new InspectorElement();
MethodInfo m = typeof(InspectorElement).GetMethod("CreateIMGUIInspectorFromEditor",
BindingFlags.NonPublic | BindingFlags.Instance);
inner = m.Invoke(throwaway, new object[] {serializedObject, this, false}) as VisualElement;
}
_visualElement.Add(inner);
_suppressOnceDefaultMargins = innerIsImgui;
return _visualElement;
}
public override bool UseDefaultMargins()
{
var useDefaults = !_suppressOnceDefaultMargins;
_suppressOnceDefaultMargins = false;
return useDefaults;
}
public sealed override void OnInspectorGUI()
{
InspectorCommon.DisplayOutOfAvatarWarning(targets);
OnInnerInspectorGUI();
}
protected abstract void OnInnerInspectorGUI();
protected virtual void OnDestroy()
{
Localization.OnLangChange -= RebuildUI;
}
}
internal class MAVisualElement : VisualElement
{
}
}