I have a Quaternion variable that I declared as public so I could edit it through the inspector. But since I don’t know how quaternions work, I would like to us Euler angles. Is there a way to have the editor show the values in Euler rather than quaternion?
I would recommend creating a custom PropertyAttribute and PropertyDrawer.
For example, given a PropertyAttribute like this:
using UnityEngine;
public class EulerAnglesAttribute : PropertyAttribute {}
You could create a PropertyDrawer like this:
using UnityEditor;
using UnityEngine;
[CustomPropertyDrawer(typeof(EulerAnglesAttribute))]
public class EulerAnglesDrawer : PropertyDrawer {
public override float GetPropertyHeight(SerializedProperty property, GUIContent label) {
return EditorGUIUtility.singleLineHeight * (EditorGUIUtility.wideMode ? 1f : 2f);
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
Vector3 euler = property.quaternionValue.eulerAngles;
EditorGUI.BeginProperty(position, label, property);
EditorGUI.BeginChangeCheck();
euler = EditorGUI.Vector3Field(position, label, euler);
if (EditorGUI.EndChangeCheck())
property.quaternionValue = Quaternion.Euler(euler);
EditorGUI.EndProperty();
}
}
You would then decorate your field like this:
using UnityEngine;
public class MyComponent : MonoBehaviour {
[SerializeField, EulerAngles]
Quaternion m_Orientation;
}