Attribute : AngleDisplayAs - Displays either a radian or euler float as the opposite
This commit is contained in:
parent
6e3bd4df70
commit
df1d2f6df1
51
Editor/Drawers/AngleDisplayAsAttributeDrawer.cs
Normal file
51
Editor/Drawers/AngleDisplayAsAttributeDrawer.cs
Normal file
|
|
@ -0,0 +1,51 @@
|
||||||
|
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";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
3
Editor/Drawers/AngleDisplayAsAttributeDrawer.cs.meta
Normal file
3
Editor/Drawers/AngleDisplayAsAttributeDrawer.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 1cff7f97798e4a7081c733911b2420ea
|
||||||
|
timeCreated: 1767540741
|
||||||
|
|
@ -78,6 +78,8 @@ List of all available pre-drawer attributes:
|
||||||
|
|
||||||
List of all available drawer attributes:
|
List of all available drawer attributes:
|
||||||
|
|
||||||
|
* `AngleDisplayAs`
|
||||||
|
* Displays either a radian or euler float as the opposite
|
||||||
* `EnumFlag`
|
* `EnumFlag`
|
||||||
* Adds popup with enum type provided and allows for selecting values as flags (Requirement: Enum must have flag values)
|
* Adds popup with enum type provided and allows for selecting values as flags (Requirement: Enum must have flag values)
|
||||||
* `FilePath`
|
* `FilePath`
|
||||||
|
|
|
||||||
16
Runtime/Drawers/AngleDisplayAsAttribute.cs
Normal file
16
Runtime/Drawers/AngleDisplayAsAttribute.cs
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
using System;
|
||||||
|
using UnityEngine.Scripting;
|
||||||
|
|
||||||
|
namespace Module.Inspector
|
||||||
|
{
|
||||||
|
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = true)]
|
||||||
|
public sealed class AngleDisplayAsAttribute : DrawerPropertyAttribute
|
||||||
|
{
|
||||||
|
public readonly EAngle type;
|
||||||
|
|
||||||
|
public AngleDisplayAsAttribute(EAngle type)
|
||||||
|
{
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
3
Runtime/Drawers/AngleDisplayAsAttribute.cs.meta
Normal file
3
Runtime/Drawers/AngleDisplayAsAttribute.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 888ba1cbaa514c0fb72484e2f2d00ef3
|
||||||
|
timeCreated: 1767540551
|
||||||
10
Runtime/Enums/EAngle.cs
Normal file
10
Runtime/Enums/EAngle.cs
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Module.Inspector
|
||||||
|
{
|
||||||
|
public enum EAngle
|
||||||
|
{
|
||||||
|
Euler,
|
||||||
|
Radian
|
||||||
|
}
|
||||||
|
}
|
||||||
3
Runtime/Enums/EAngle.cs.meta
Normal file
3
Runtime/Enums/EAngle.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 1812098cc5214e5c9b21c53535ee4a57
|
||||||
|
timeCreated: 1767540585
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "com.module.inspector",
|
"name": "com.module.inspector",
|
||||||
"version": "1.11.2",
|
"version": "1.11.3",
|
||||||
"displayName": "Module.Inspector",
|
"displayName": "Module.Inspector",
|
||||||
"description": "Custom inspector with various useful property drawers",
|
"description": "Custom inspector with various useful property drawers",
|
||||||
"unity": "2019.2",
|
"unity": "2019.2",
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue