- Home /
Use Euler angles for Quaternion variable in the Inspector
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?
Answer by Adam-Mechtley · Feb 16, 2017 at 08:13 AM
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;
}
Hi, this however is not like the transform inspector as it snaps the euler to 0-359, it doesnt allow negative eulers.. I tried creating variables for the attribute, but it affects all of the attributes (for example, inside a list all of them would use the same vars, as expected..)
So really, how to make it work as the transform inspector? If the only way is creating a custom quaternion struct thats pretty lame..
You can't just "make it" work like the transform inspector as the transform holds an internal cached eulerangles value for the inspector. Usually the easiest solution is to just use a Vector3 value ins$$anonymous$$d of a quaternion and just convert the vector3 into a quaternion when you want to use it.
public Vector3 rotationEuler;
public Quaternion rotation
{
get { return Quaternion.Euler(rotationEuler); }
}
$$anonymous$$eep in $$anonymous$$d that a Quaternion is a completely different way to represent a rotation compared to eulerangles. Euler angles are 3 seperate rotations which are executed one after another while a Quaternion directly represents a rotation as a direction vector and and additional "rotation value". Quaternions aren't meant to be easy "editable" by a user. It's just a way better approach to do rotations.
If you want to know more about Quaternions i suggest you have a look at the Numberphile video on Quaternions
Your answer
Follow this Question
Related Questions
MouseLookPlus Script- can't access certain variables in the inspector view 1 Answer
Is it possible to show Static Variables in the Inspector? 10 Answers
Javascript and C#, different behaviour in inspector ? 1 Answer
How to expose variables in c# according to selected booleans 1 Answer
How can i assign prefab to variable without drag & drop from project to inspector. 2 Answers