2023-09-24 15:59:15 +08:00
|
|
|
|
using System.Reflection;
|
2024-07-08 23:37:20 +08:00
|
|
|
|
using UnityEditor;
|
2022-11-11 12:30:10 +08:00
|
|
|
|
using UnityEditor.UIElements;
|
2024-07-08 23:37:20 +08:00
|
|
|
|
using UnityEngine;
|
2022-11-11 12:30:10 +08:00
|
|
|
|
using UnityEngine.UIElements;
|
|
|
|
|
|
2022-11-11 12:39:58 +08:00
|
|
|
|
namespace nadena.dev.modular_avatar.core.editor
|
2022-11-11 12:30:10 +08:00
|
|
|
|
{
|
|
|
|
|
// This class performs common setup for Modular Avatar editors, including ensuring that only one instance of the\
|
|
|
|
|
// logo is rendered per container.
|
2024-07-08 23:37:20 +08:00
|
|
|
|
public abstract class MAEditorBase : Editor
|
2022-11-11 12:30:10 +08:00
|
|
|
|
{
|
2023-09-24 15:59:15 +08:00
|
|
|
|
private MAVisualElement _visualElement;
|
|
|
|
|
private bool _suppressOnceDefaultMargins;
|
2022-11-11 12:30:10 +08:00
|
|
|
|
|
2023-09-24 15:59:15 +08:00
|
|
|
|
protected virtual VisualElement CreateInnerInspectorGUI()
|
2022-11-11 12:30:10 +08:00
|
|
|
|
{
|
2023-09-24 15:59:15 +08:00
|
|
|
|
return null;
|
2022-11-11 12:30:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-24 15:59:15 +08:00
|
|
|
|
private void RebuildUI()
|
2022-11-11 12:30:10 +08:00
|
|
|
|
{
|
2023-09-24 15:59:15 +08:00
|
|
|
|
CreateInspectorGUI();
|
2022-11-11 12:30:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-24 15:59:15 +08:00
|
|
|
|
public sealed override VisualElement CreateInspectorGUI()
|
2022-11-11 12:30:10 +08:00
|
|
|
|
{
|
2023-09-24 15:59:15 +08:00
|
|
|
|
if (_visualElement == null)
|
2022-11-11 12:30:10 +08:00
|
|
|
|
{
|
2023-09-24 15:59:15 +08:00
|
|
|
|
_visualElement = new MAVisualElement();
|
|
|
|
|
Localization.OnLangChange += RebuildUI;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_visualElement.Clear();
|
2022-11-11 12:30:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-24 15:59:15 +08:00
|
|
|
|
_visualElement.Add(new LogoElement());
|
2022-11-11 12:30:10 +08:00
|
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
|
2023-03-04 13:31:23 +08:00
|
|
|
|
bool innerIsImgui = (inner == null);
|
|
|
|
|
if (innerIsImgui)
|
|
|
|
|
{
|
|
|
|
|
var throwaway = new InspectorElement();
|
2024-07-08 23:37:20 +08:00
|
|
|
|
|
|
|
|
|
var m_CreateIMGUIInspectorFromEditor = typeof(InspectorElement)
|
|
|
|
|
.GetMethod("CreateIMGUIInspectorFromEditor", BindingFlags.NonPublic | BindingFlags.Instance);
|
|
|
|
|
var m_CreateInspectorElementUsingIMGUI = typeof(InspectorElement)
|
|
|
|
|
.GetMethod("CreateInspectorElementUsingIMGUI", BindingFlags.NonPublic | BindingFlags.Instance);
|
|
|
|
|
|
|
|
|
|
Debug.Log($"CreateIMGUIInspectorFromEditor found: {m_CreateIMGUIInspectorFromEditor != null}");
|
|
|
|
|
Debug.Log($"CreateInspectorElementUsingIMGUI found: {m_CreateInspectorElementUsingIMGUI != null}");
|
|
|
|
|
|
|
|
|
|
if (m_CreateIMGUIInspectorFromEditor != null)
|
|
|
|
|
{
|
|
|
|
|
inner = m_CreateIMGUIInspectorFromEditor.Invoke(throwaway,
|
|
|
|
|
new object[] { serializedObject, this, false }) as VisualElement;
|
|
|
|
|
}
|
|
|
|
|
else if (m_CreateInspectorElementUsingIMGUI != null)
|
|
|
|
|
{
|
|
|
|
|
inner = m_CreateInspectorElementUsingIMGUI.Invoke(throwaway,
|
|
|
|
|
new object[] { this }) as VisualElement;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Debug.Log(
|
|
|
|
|
"Neither CreateIMGUIInspectorFromEditor nor CreateInspectorElementUsingIMGUI found, for inspector " +
|
|
|
|
|
GetType());
|
|
|
|
|
inner = new VisualElement();
|
|
|
|
|
}
|
2023-03-04 13:31:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-11 12:30:10 +08:00
|
|
|
|
_visualElement.Add(inner);
|
|
|
|
|
|
2023-03-04 13:31:23 +08:00
|
|
|
|
_suppressOnceDefaultMargins = innerIsImgui;
|
2022-11-11 12:30:10 +08:00
|
|
|
|
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();
|
2023-09-24 15:59:15 +08:00
|
|
|
|
|
|
|
|
|
protected virtual void OnDestroy()
|
|
|
|
|
{
|
|
|
|
|
Localization.OnLangChange -= RebuildUI;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal class MAVisualElement : VisualElement
|
|
|
|
|
{
|
2022-11-11 12:30:10 +08:00
|
|
|
|
}
|
|
|
|
|
}
|