Tri-Inspector/Editor.Extras/Drawers/ObjectReferenceDrawer.cs

56 lines
1.7 KiB
C#
Raw Normal View History

2022-01-07 12:21:36 -05:00
using TriInspector;
using TriInspector.Drawers;
using UnityEditor;
using UnityEngine;
[assembly: RegisterTriValueDrawer(typeof(ObjectReferenceDrawer<>), TriDrawerOrder.Fallback)]
namespace TriInspector.Drawers
{
public class ObjectReferenceDrawer<T> : TriValueDrawer<T>
where T : Object
{
public override TriElement CreateElement(TriValue<T> value, TriElement next)
{
if (value.Property.TryGetSerializedProperty(out _))
{
return next;
}
return new ObjectReferenceDrawerElement(value);
}
private class ObjectReferenceDrawerElement : TriElement
{
2022-01-21 23:36:15 -05:00
private TriValue<T> _propertyValue;
2022-01-07 12:21:36 -05:00
public ObjectReferenceDrawerElement(TriValue<T> propertyValue)
{
_propertyValue = propertyValue;
}
public override float GetHeight(float width)
{
return EditorGUIUtility.singleLineHeight;
}
public override void OnGUI(Rect position)
{
var value = _propertyValue.Value;
EditorGUI.BeginChangeCheck();
var allowSceneObjects = _propertyValue.Property.PropertyTree.TargetObjects[0] is var targetObject &&
targetObject != null && !EditorUtility.IsPersistent(targetObject);
value = (T) EditorGUI.ObjectField(position, _propertyValue.Property.DisplayNameContent, value,
_propertyValue.Property.FieldType, allowSceneObjects);
if (EditorGUI.EndChangeCheck())
{
_propertyValue.Value = value;
}
}
}
}
}