Its quite weird that UI Toolkit does not have a Quaternion field and instead uses a Vector4 field.
Since there is no direct cast from Vector4 to Quaternion this requires special handling, instead of using generic functions for all data fields.
For such a commonly used type there must be an appropriate UI field option.
I am struggling about this.
I mean if you want euler angles you can just use a Vector3 field. It would be pretty straight forward to make a custom visual element that handles this in a reusable manner.
@spiney199 Yep. That was what i did at first thought and made it.
@Kekito
So basically, inside the component which is the Quaternion will be in, just create a Vector3 “hint” value for Quaternion.
Use #if UNITY_EDITOR #endif to make it only work for visualization if you want to but not necessary of course.
With Registercallback<ChangeEvent> or TrackProperty, you will be able to track the property value changes. Even tho, you can get UXML Element from C# with VisualElement.Q(“ID”);
After that, inside the callback of registercallback, you will save the vector3 with property.ApplyModifiedProperties(); and set the rotation value by converting the Vector3 to Quaternion by Quaternion.Euler(The new value);
I had the same need, hope my code can be usefull for next ones
// related code in CreatePropertyGUI
// rotation is a Quaternion SerializedProperty
bool isQuaternionLocked = false;
Vector3Field rotationField = new();
if (rotation.quaternionValue != null) rotationField.value = rotation.quaternionValue.eulerAngles;
rotationField.TrackPropertyValue(rotation, (newProp) => // Track Quaternion to Vector3
{
if (isQuaternionLocked) return;
isQuaternionLocked = true;
rotationField.value = newProp.quaternionValue.eulerAngles;
isQuaternionLocked = false;
});
rotationField.RegisterValueChangedCallback((@event) => // Track Vector3 to Quaternion
{
if (isQuaternionLocked) return;
isQuaternionLocked = true;
rotation.quaternionValue = Quaternion.Euler(@event.newValue);
rotation.serializedObject.ApplyModifiedProperties();
isQuaternionLocked = false;
});
Hi, you can make an official feature request using the UI roadmap page.
Hope this helps!