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

81 lines
2.6 KiB
C#
Raw Normal View History

2022-01-08 12:45:11 -05:00
using TriInspector;
using TriInspector.Drawers;
using TriInspector.Elements;
using TriInspector.Utilities;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;
2022-01-15 12:24:52 -05:00
[assembly: RegisterTriAttributeDrawer(typeof(InlineEditorDrawer), TriDrawerOrder.Drawer - 100,
2022-01-09 11:24:45 -05:00
ApplyOnArrayElement = true)]
2022-01-08 12:45:11 -05:00
namespace TriInspector.Drawers
{
public class InlineEditorDrawer : TriAttributeDrawer<InlineEditorAttribute>
{
public override string CanDraw(TriProperty property)
2022-01-08 12:45:11 -05:00
{
if (!typeof(Object).IsAssignableFrom(property.FieldType))
{
return "[InlineEditor] valid only on Object fields";
2022-01-08 12:45:11 -05:00
}
return null;
}
public override TriElement CreateElement(TriProperty property, TriElement next)
{
2022-01-10 03:12:44 -05:00
var element = new TriBoxGroupElement(new DeclareBoxGroupAttribute(""));
2022-01-08 12:45:11 -05:00
element.AddChild(new ObjectReferenceFoldoutDrawerElement(property));
2022-01-10 03:12:44 -05:00
element.AddChild(new InlineEditorElement(property));
2022-01-08 12:45:11 -05:00
return element;
}
private class ObjectReferenceFoldoutDrawerElement : TriElement
{
private readonly TriProperty _property;
public ObjectReferenceFoldoutDrawerElement(TriProperty property)
{
_property = property;
}
public override float GetHeight(float width)
{
return EditorGUIUtility.singleLineHeight;
}
public override void OnGUI(Rect position)
{
var prefixRect = new Rect(position)
{
height = EditorGUIUtility.singleLineHeight,
xMax = position.xMin + EditorGUIUtility.labelWidth,
};
var pickerRect = new Rect(position)
{
height = EditorGUIUtility.singleLineHeight,
xMin = prefixRect.xMax,
};
2022-01-10 02:39:14 -05:00
using (TriGuiHelper.PushIndentLevel())
{
TriEditorGUI.Foldout(prefixRect, _property);
}
2022-01-08 12:45:11 -05:00
EditorGUI.BeginChangeCheck();
2022-05-17 06:33:23 -04:00
var allowSceneObjects = _property.PropertyTree.TargetIsPersistent;
2022-01-08 12:45:11 -05:00
var value = (Object) _property.Value;
value = EditorGUI.ObjectField(pickerRect, GUIContent.none, value,
_property.FieldType, allowSceneObjects);
if (EditorGUI.EndChangeCheck())
{
_property.SetValue(value);
}
}
}
}
}