Tri-Inspector/Editor/TriEditor.cs
2022-01-10 11:12:44 +03:00

94 lines
2.2 KiB
C#

using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.Profiling;
namespace TriInspector
{
public abstract class TriEditor : Editor
{
private static readonly Stack<Editor> EditorStack = new Stack<Editor>();
private TriPropertyTree _inspector;
private void OnEnable()
{
var mode = TriEditorMode.None;
var isInlineEditor = EditorStack.Count > 0;
if (isInlineEditor)
{
mode |= TriEditorMode.InlineEditor;
}
if (serializedObject.targetObject != null)
{
_inspector = TriPropertyTree.Create(serializedObject, mode);
}
}
private void OnDisable()
{
_inspector?.Destroy();
_inspector = null;
}
public override void OnInspectorGUI()
{
if (_inspector == null)
{
DrawDefaultInspector();
return;
}
serializedObject.UpdateIfRequiredOrScript();
Profiler.BeginSample("TriInspector.Update()");
try
{
_inspector.Update();
}
finally
{
Profiler.EndSample();
}
EditorStack.Push(this);
Profiler.BeginSample("TriInspector.DoLayout()");
try
{
_inspector.DoLayout();
}
finally
{
Profiler.EndSample();
EditorStack.Pop();
}
serializedObject.ApplyModifiedProperties();
if (_inspector.RepaintRequired)
{
_inspector.RepaintRequired = false;
Repaint();
}
}
internal static bool IsEditorForObjectPushed(Object targetObject)
{
foreach (var editor in EditorStack)
{
foreach (var editorTarget in editor.targets)
{
if (editorTarget == targetObject)
{
return true;
}
}
}
return false;
}
}
}