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

54 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)]
2022-01-07 12:21:36 -05:00
namespace TriInspector.Drawers
{
public class ObjectReferenceDrawer : TriValueDrawer<Object>
2022-01-07 12:21:36 -05:00
{
public override TriElement CreateElement(TriValue<Object> value, TriElement next)
2022-01-07 12:21:36 -05:00
{
2022-12-07 02:38:25 -05:00
if (value.Property.IsRootProperty || value.Property.TryGetSerializedProperty(out _))
2022-06-01 03:35:33 -04:00
{
return next;
}
2022-01-07 12:21:36 -05:00
return new ObjectReferenceDrawerElement(value);
}
private class ObjectReferenceDrawerElement : TriElement
{
private TriValue<Object> _propertyValue;
2022-06-10 08:33:05 -04:00
private readonly bool _allowSceneObjects;
2022-01-07 12:21:36 -05:00
public ObjectReferenceDrawerElement(TriValue<Object> propertyValue)
2022-01-07 12:21:36 -05:00
{
_propertyValue = propertyValue;
2022-12-07 02:38:25 -05:00
_allowSceneObjects = propertyValue.Property.PropertyTree.TargetIsPersistent == false;
2022-01-07 12:21:36 -05:00
}
public override float GetHeight(float width)
{
return EditorGUIUtility.singleLineHeight;
}
public override void OnGUI(Rect position)
{
2022-12-07 02:38:25 -05:00
var value = _propertyValue.SmartValue;
2022-01-07 12:21:36 -05:00
2022-06-10 08:33:05 -04:00
EditorGUI.BeginChangeCheck();
2022-01-07 12:21:36 -05:00
2022-06-10 08:33:05 -04:00
value = EditorGUI.ObjectField(position, _propertyValue.Property.DisplayNameContent, value,
_propertyValue.Property.FieldType, _allowSceneObjects);
2022-01-07 12:21:36 -05:00
if (EditorGUI.EndChangeCheck())
{
_propertyValue.SetValue(value);
2022-01-07 12:21:36 -05:00
}
}
}
}
}