Editor for Quaternion - weird problem

Basically all I want to do is make an editor for a rotation, exactly like the one Unity uses for transform.rotation (that is, with x, y, z only, not w). I can get EditorGUILayout.Vector3Field to work fine for normal Vector3 fields, but for some reason I am having a strange problem when I try to convert a Quaternion to a Vector3, create a Vector3Field for it, and then convert the returned value back to a Quaternion.

The problem is that when I go to edit a number in this Vector3Field, the value for that number seems to wiggle around for a second or two before returning back to zero. I have no explanation for this. Here is the relevant line from my editor:

scriptTarget.startAngle = toQuaternion(EditorGUILayout.Vector3Field("Default Angle", toVector3(scriptTarget.startAngle)));

… and the two utility methods used:

public static Vector3 toVector3(Quaternion q) {
	return new Vector3(q.x, q.y, q.z);
}

public static Quaternion toQuaternion(Vector3 v) {
	return Quaternion.Euler(v.x, v.y, v.z);
}

You use the Quaternion wrong :wink: The 4 components of a quaternion has nothing to do with eulerAngles. It is actually some kind of complex number which represents a normalized direction vector and a rotation around that vector. If you don’t actually know how a quaternion works, you should never read or write any of the components of a quaternion.

A quaternion can be converted into eulerAngles. However if you want to do the same as the Transform inspector you should store your euler angles as Vector3 and convert it to a quaternion when you need it. The TransformInspector of Unity uses transform.localEulerAngles more or less directly. However inside the editor it seems that the eulerAngles property holds a cached version in memory. At edit time you can set the angles to any number you like even greater than 360. At runtime it behaves differently because the angles get calculates from the quaternion and therefore will always be within the 360° range.