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

73 lines
2.2 KiB
C#
Raw Normal View History

2021-12-07 10:20:36 -05:00
using TriInspector;
using TriInspector.Drawers;
using UnityEditor;
using UnityEngine;
2022-01-06 12:11:27 -05:00
[assembly: RegisterTriValueDrawer(typeof(StringDrawer), TriDrawerOrder.Fallback)]
2021-12-07 10:20:36 -05:00
namespace TriInspector.Drawers
{
public class StringDrawer : TriValueDrawer<string>
{
public override TriElement CreateElement(TriValue<string> value, TriElement next)
{
if (value.Property.TryGetSerializedProperty(out var serializedProperty))
{
2022-01-30 10:36:38 -05:00
return new StringSerializedPropertyDrawerElement(value.Property, serializedProperty);
2021-12-07 10:20:36 -05:00
}
return new StringDrawerElement(value);
}
private class StringDrawerElement : TriElement
{
2022-01-21 23:36:15 -05:00
private TriValue<string> _propertyValue;
2021-12-07 10:20:36 -05:00
public StringDrawerElement(TriValue<string> propertyValue)
{
_propertyValue = propertyValue;
}
public override float GetHeight(float width)
{
return EditorGUIUtility.singleLineHeight;
}
public override void OnGUI(Rect position)
{
var value = _propertyValue.Value;
EditorGUI.BeginChangeCheck();
value = EditorGUI.TextField(position, _propertyValue.Property.DisplayNameContent, value);
if (EditorGUI.EndChangeCheck())
{
_propertyValue.Value = value;
}
}
}
private class StringSerializedPropertyDrawerElement : TriElement
{
2022-01-30 10:36:38 -05:00
private readonly TriProperty _property;
2021-12-07 10:20:36 -05:00
private readonly SerializedProperty _serializedProperty;
2022-01-30 10:36:38 -05:00
public StringSerializedPropertyDrawerElement(TriProperty property, SerializedProperty serializedProperty)
2021-12-07 10:20:36 -05:00
{
2022-01-30 10:36:38 -05:00
_property = property;
2021-12-07 10:20:36 -05:00
_serializedProperty = serializedProperty;
}
public override float GetHeight(float width)
{
return EditorGUIUtility.singleLineHeight;
}
public override void OnGUI(Rect position)
{
2022-01-30 10:36:38 -05:00
EditorGUI.PropertyField(position, _serializedProperty, _property.DisplayNameContent);
2021-12-07 10:20:36 -05:00
}
}
}
}