Tri-Inspector/Editor/TriEditor.cs

112 lines
2.7 KiB
C#
Raw Normal View History

2022-01-10 02:39:14 -05:00
using System.Collections.Generic;
2021-12-07 10:20:36 -05:00
using UnityEditor;
2022-01-08 12:45:11 -05:00
using UnityEngine;
2021-12-07 10:20:36 -05:00
using UnityEngine.Profiling;
namespace TriInspector
{
public abstract class TriEditor : Editor
{
2022-01-10 02:39:14 -05:00
private static readonly Stack<Editor> EditorStack = new Stack<Editor>();
2021-12-07 10:20:36 -05:00
private TriPropertyTree _inspector;
private void OnEnable()
{
2022-01-08 12:45:11 -05:00
var mode = TriEditorMode.None;
2022-01-10 02:39:14 -05:00
var isInlineEditor = EditorStack.Count > 0;
2022-01-08 12:45:11 -05:00
if (isInlineEditor)
{
mode |= TriEditorMode.InlineEditor;
}
2021-12-07 10:20:36 -05:00
if (serializedObject.targetObject != null)
{
2022-01-08 12:45:11 -05:00
_inspector = TriPropertyTree.Create(serializedObject, mode);
2021-12-07 10:20:36 -05:00
}
}
private void OnDisable()
{
_inspector?.Destroy();
2022-01-08 12:45:11 -05:00
_inspector = null;
2021-12-07 10:20:36 -05:00
}
public override void OnInspectorGUI()
{
if (_inspector == null)
{
DrawDefaultInspector();
return;
}
serializedObject.UpdateIfRequiredOrScript();
2022-01-05 08:11:51 -05:00
Profiler.BeginSample("TriInspector.Update()");
try
2021-12-07 10:20:36 -05:00
{
2022-01-05 08:11:51 -05:00
_inspector.Update();
2021-12-07 10:20:36 -05:00
}
2022-01-05 08:11:51 -05:00
finally
2021-12-07 10:20:36 -05:00
{
2022-01-05 08:11:51 -05:00
Profiler.EndSample();
2021-12-07 10:20:36 -05:00
}
2022-01-15 11:25:12 -05:00
Profiler.BeginSample("TriInspector.RunValidation()");
try
{
if (_inspector.ValidationRequired)
{
_inspector.ValidationRequired = false;
_inspector.RunValidation();
}
}
finally
{
Profiler.EndSample();
}
2021-12-07 10:20:36 -05:00
2022-01-10 02:39:14 -05:00
EditorStack.Push(this);
2022-01-05 08:11:51 -05:00
Profiler.BeginSample("TriInspector.DoLayout()");
try
2021-12-07 10:20:36 -05:00
{
2022-01-05 08:11:51 -05:00
_inspector.DoLayout();
2021-12-07 10:20:36 -05:00
}
2022-01-05 08:11:51 -05:00
finally
2021-12-07 10:20:36 -05:00
{
2022-01-05 08:11:51 -05:00
Profiler.EndSample();
2022-01-10 02:39:14 -05:00
EditorStack.Pop();
2021-12-07 10:20:36 -05:00
}
2022-01-15 11:25:12 -05:00
if (serializedObject.ApplyModifiedProperties())
{
_inspector.RequestValidation();
}
2022-01-08 12:45:11 -05:00
if (_inspector.RepaintRequired)
{
_inspector.RepaintRequired = false;
Repaint();
}
2021-12-07 10:20:36 -05:00
}
2022-01-10 02:39:14 -05:00
2022-01-10 03:12:44 -05:00
internal static bool IsEditorForObjectPushed(Object targetObject)
2022-01-10 02:39:14 -05:00
{
foreach (var editor in EditorStack)
{
foreach (var editorTarget in editor.targets)
{
if (editorTarget == targetObject)
{
return true;
}
}
}
return false;
}
2021-12-07 10:20:36 -05:00
}
}