module-inspector/Editor/Drawers/DrawerNaming.cs

42 lines
1.5 KiB
C#

using Module.Inspector.Editor.Utilities;
using UnityEditor;
using UnityEngine;
namespace Module.Inspector.Editor
{
[CustomPropertyDrawer(typeof(Naming))]
internal sealed class DrawerNaming : DrawerPropertyDrawer
{
public override bool Draw(Rect position, DrawerPropertyAttribute attribute, SerializedProperty property, GUIContent label, EditorPropertyUtility.Result result)
{
if (property.propertyType != SerializedPropertyType.String)
return false;
var att = (Naming)attribute;
EditorGUI.BeginProperty(position, label, property);
{
const float WIDTH = 80;
var rect0 = new Rect(position.x, position.y, position.width - WIDTH, position.height);
var rect1 = new Rect(rect0.xMax, position.y, WIDTH, position.height);
EditorGUI.PropertyField(rect0, property);
if (GUI.Button(rect1, "Go"))
{
string str = EditorNamingUtility.ConvertTo(att.type, property.stringValue);
property.stringValue = str;
property.serializedObject.ApplyModifiedProperties();
}
}
EditorGUI.EndProperty();
return true;
}
public override string GetErrorMessage(SerializedProperty property)
{
return "Only supports strings";
}
}
}