Freely edit quaternion from inspector debug menu or inspector in Unity editor

Viewed 569

I have a quaternion that I would like to set as a transform property on an object in the Unity editor. The debug mode in the inspector tab shows the current value of the quaternion and it is editable, however I can't set fields one by one without them being overwritten immediately. It looks like Unity tries to properize/normalize it after each edit. How can I disable this? Is there any way other way to just type in the value of a quaternion in the unity editor?

"Convert it to euler angles" is not an acceptable answer to this question.

1 Answers

As far as I know, that functionality can't be disabled.

However, if using the normal inspector is acceptable, a custom inspector can help you here.

First, a class to collect the local rotation. Includes a boolean to uncheck before you edit it, to stop the values from being updated/overwritten. Vector4 is used here to allow the convenient PropertyField to appear in the custom editor:

using UnityEngine;

public class Demo : MonoBehaviour
{
    public Vector4 rotation;
    public bool updateFromTransform = true;

    void Update()
    {
        // for testing purposes
        // transform.Rotate(0.3f * Vector3.up); 

        if (updateFromTransform)
        {
            rotation.x = transform.localRotation.x;
            rotation.y = transform.localRotation.y;
            rotation.z = transform.localRotation.z;
            rotation.w = transform.localRotation.w;
        }

    }
}

And a custom editor including a button to apply changes to the Vector4 to the local rotation:

using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(Demo))]
public class DemoEditor : Editor

    SerializedProperty rotation
    SerializedProperty updateFromTransform;
    private void OnEnable()
    {
        rotation = serializedObject.FindProperty("rotation");
        updateFromTransform = serializedObject.FindProperty("updateFromTransform");
    }

    public override void OnInspectorGUI()
    {
        Demo d = (Demo)target;
        serializedObject.Update();

        EditorGUILayout.PropertyField(updateFromTransform);
        EditorGUILayout.PropertyField(rotation);

        serializedObject.ApplyModifiedProperties();

        if (GUILayout.Button("Apply as local rotation"))
        {
            d.updateFromTransform = true;
            d.transform.localRotation = 
                    new Quaternion(d.rotation.x, d.rotation.y, d.rotation.z, d.rotation.w);
        }

    }
}
Related