module-inspector/Editor/Drawers/AngleDisplayAsAttributeDrawer.cs

51 lines
1.8 KiB
C#

using Module.Inspector.Editor.Utilities;
using UnityEditor;
using UnityEngine;
namespace Module.Inspector.Editor
{
[CustomPropertyDrawer(typeof(AngleDisplayAsAttribute))]
internal sealed class AngleDisplayAsAttributeDrawer : DrawerPropertyDrawer
{
public override bool Draw(Rect position, DrawerPropertyAttribute attribute, SerializedProperty property, GUIContent label, EditorPropertyUtility.Result result)
{
if (property.propertyType != SerializedPropertyType.Float)
return false;
var att = (AngleDisplayAsAttribute)attribute;
EditorGUI.BeginChangeCheck();
EditorGUI.BeginProperty(position, label, property);
{
if (att.type == EAngle.Euler)
{
float toEuler = Mathf.Rad2Deg * property.floatValue;
float temp = EditorGUI.FloatField(position, label, toEuler) * Mathf.Deg2Rad;
if (!Mathf.Approximately(temp, property.floatValue))
property.floatValue = temp;
}
else
{
float toRadian = Mathf.Deg2Rad * property.floatValue;
float temp = EditorGUI.FloatField(position, label, toRadian) * Mathf.Rad2Deg;
if (!Mathf.Approximately(temp, property.floatValue))
property.floatValue = temp;
}
}
EditorGUI.EndProperty();
bool changed = EditorGUI.EndChangeCheck();
if (changed)
property.serializedObject.ApplyModifiedProperties();
return true;
}
public override string GetErrorMessage(SerializedProperty property)
{
return "Only supports floats";
}
}
}