Search Unity

Feature Request UI Toolkit Quaternion field does not exist

Discussion in 'UI Toolkit' started by Kekito, Jul 1, 2022.

  1. Kekito

    Kekito

    Joined:
    Nov 24, 2016
    Posts:
    14
    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.
     
    FerdowsurAsif and Wilhelm_LAS like this.
  2. Wilhelm_LAS

    Wilhelm_LAS

    Joined:
    Aug 18, 2020
    Posts:
    55
    I am struggling about this.
     
  3. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,934
    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.
     
  4. Wilhelm_LAS

    Wilhelm_LAS

    Joined:
    Aug 18, 2020
    Posts:
    55
    @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<TheValueType>> 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);
     
    Last edited: Sep 6, 2023
  5. Pholith

    Pholith

    Joined:
    Oct 31, 2021
    Posts:
    4
    I had the same need, hope my code can be usefull for next ones

    Code (CSharp):
    1.  
    2. // related code in CreatePropertyGUI
    3. // rotation is a Quaternion SerializedProperty
    4. bool isQuaternionLocked = false;
    5. Vector3Field rotationField = new();
    6. if (rotation.quaternionValue != null) rotationField.value = rotation.quaternionValue.eulerAngles;
    7. rotationField.TrackPropertyValue(rotation, (newProp) => // Track Quaternion to Vector3
    8. {
    9.    if (isQuaternionLocked) return;
    10.    isQuaternionLocked = true;
    11.    rotationField.value = newProp.quaternionValue.eulerAngles;
    12.    isQuaternionLocked = false;
    13. });
    14. rotationField.RegisterValueChangedCallback((@event) => // Track Vector3 to Quaternion
    15. {
    16.    if (isQuaternionLocked) return;
    17.    isQuaternionLocked = true;
    18.    rotation.quaternionValue = Quaternion.Euler(@event.newValue);
    19.    rotation.serializedObject.ApplyModifiedProperties();
    20.    isQuaternionLocked = false;
    21. });
    22.  
     
    Last edited: Oct 5, 2023
    Wilhelm_LAS likes this.
  6. martinpa_unity

    martinpa_unity

    Unity Technologies

    Joined:
    Oct 18, 2017
    Posts:
    480
    Hi, you can make an official feature request using the UI roadmap page.
    Hope this helps!
     
    FerdowsurAsif likes this.