Euler angles in inspector
Is there a way to see (and change) Euler angles for gameobjects in the Unity inspector instead of quaternion notation? I want X Y Z instead of X Y Z W. I would think Unity would give this option. It's too difficult to figure out what the angle is in X Y Z W. And, if I do a conversion using a tool and then try to set XYZW in the inspector, other ones change before I can set all 4. So, the only way I can find to set a gameobject's rotation to an EXACT angle is though code.
Answer by YoucefB · Oct 11, 2017 at 10:47 PM
After some tests here is how i did it: (using propertyDrawer)
QuaternionToEuler.Cs (inside a folder with the name 'Editor')
using UnityEngine;
using UnityEditor;
[CustomPropertyDrawer(typeof(Quaternion))]
public class QuaternionToEulerPropertyDrawer: PropertyDrawer
{
Vector3 euler;
bool initialized = false;
public override void OnGUI (Rect position, SerializedProperty property, GUIContent label)
{
if (!initialized) {
euler = property.quaternionValue.eulerAngles; // store the current eulerAngles
initialized = true;
}
label.text += " (Quaternion)"; // can be removed, just to know which are Quaternions
euler = EditorGUI.Vector3Field (position,label,euler); // display the field.
property.quaternionValue = Quaternion.Euler(euler); // convert the eulerAngles back to Quaternion value.
}
}
Note: this script will replace any Quaternion field in any Script and display a vector3 field instead of it.
This didn't work. There were no errors in your script but, it didn't change the inspector at all. I still just see X Y Z W for GameObject rotations. Please see screenshot. And, it's definitely in a working Editor c# file.
Ah this wasn't meant to override the Transform's component in the inspector but a Quaternion type variable.
There might be an Other Editor script in your Project that is causing this, do you have any? if yes search for one that has this attribute : [CustomEditor(typeof(Transform))] .
Definitely not any script that I added. If there is some rogue script (and I mean ROGUE), it must have ridden along with some asset I imported into my project from the Asset Store very early on. I never thought of that! I'm going to have to check all of my scripts in Editor folders and see.
Answer by Bunny83 · Oct 11, 2017 at 06:40 PM
I don't quite understand the question. The Transform inspector does show euler angles unless you switched the inspector into debug mode.
The inspector already is in normal mode (please see my screenshot to someone else's post below). When I switched to "debug" (to see what happens), I saw even more fields appear.
Answer by Birdman1011395 · Oct 11, 2017 at 08:05 PM
Why, specifically, do you need a Quaternion if you'd prefer to work with Euler angles? A simpler option would be to expose a Vector3 to the Inspector and convert it into a Quaternion during serialization or via your Start or Awake methods.
I didn't find any built in attributes for this, but you could do this on your own as well. You'll need to add an attribute to your main script folder (something like EulerToQuaternionAttribute) and then create a new property drawer in your editor scripts. The ProperyDrawer page covers how to do this very well. The second example explains how to use custom attributes. I'd recommend taking a shot at this, because understanding editor scripts is a useful skill to have.
I'm sorry if I didn't phrase the question right. But I don't want quaternions at all. I wish they never existed. I want euler angles in code and in the inspector. I want to be able to just enter in 90, 0, 0 (for example) in three blanks in the inspector labeled X Y Z. I don't have that though. Everyone seems to be puzzled. Am I missing something really simple?
Yes, you are missing something very simple: Unity is built using quaternions, not euler angles. The manual goes over this a little and explains the issues with using euler angles. The Transform inspector displays euler angles for ease of reading but still uses the Quaternion struct.