1.9.0: Added support for attributes for drawing handles in the scene

This commit is contained in:
Anders Ejlersen 2024-02-11 22:02:10 +01:00
parent 81906d49dd
commit 5ce34bde70
48 changed files with 1354 additions and 8 deletions

View file

@ -0,0 +1,83 @@
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace Module.Inspector.Editor
{
[InitializeOnLoad]
internal static class HandleDrawerEditor
{
private static readonly List<Element> ELEMENTS = new List<Element>();
static HandleDrawerEditor()
{
SceneView.beforeSceneGui -= SceneGUI;
SceneView.beforeSceneGui += SceneGUI;
Selection.selectionChanged -= OnSelectionChanged;
Selection.selectionChanged += OnSelectionChanged;
}
private static void SceneGUI(SceneView view)
{
for (int i = ELEMENTS.Count - 1; i >= 0; i--)
{
Element e = ELEMENTS[i];
bool valid = e.target != null;
if (e.target != null)
{
var serializedObject = new SerializedObject(e.target);
SerializedProperty serializedProperty = serializedObject.FindProperty(e.propertyPath);
valid = serializedProperty != null && e.drawer.IsValidFor(serializedProperty);
if (valid)
{
EditorGUI.BeginChangeCheck();
e.drawer.Draw(e.attribute, serializedProperty);
if (EditorGUI.EndChangeCheck())
serializedObject.ApplyModifiedProperties();
}
serializedObject.Dispose();
}
if (!valid)
ELEMENTS.RemoveAt(i);
}
}
private static void OnSelectionChanged()
{
ELEMENTS.Clear();
}
public static void Add(HandleDrawerPropertyAttribute attribute, SerializedProperty property, HandlePropertyDrawer drawer)
{
ELEMENTS.Add(new Element(attribute, property, drawer));
}
public static void Clear()
{
ELEMENTS.Clear();
}
private sealed class Element
{
public readonly HandleDrawerPropertyAttribute attribute;
public readonly HandlePropertyDrawer drawer;
public readonly Object target;
public readonly string propertyPath;
public Element(HandleDrawerPropertyAttribute attribute, SerializedProperty property, HandlePropertyDrawer drawer)
{
this.attribute = attribute;
this.drawer = drawer;
target = property.serializedObject.targetObject;
propertyPath = property.propertyPath;
}
}
}
}