Search Unity

Question VisualElement . transform docs / usage clarification

Discussion in 'UI Toolkit' started by spakment, Nov 30, 2022.

  1. spakment

    spakment

    Joined:
    Dec 19, 2017
    Posts:
    96
    Hi, I was following the example drag and drop from the docs here
    https://docs.unity3d.com/Manual/UIE-create-drag-and-drop-ui.html

    There is this line where the element is repositioned

    Code (CSharp):
    1. target.transform.position = new Vector2(
    2.                 Mathf.Clamp(targetStartPosition.x + pointerDelta.x, 0, target.panel.visualTree.worldBound.width),
    3.                 Mathf.Clamp(targetStartPosition.y + pointerDelta.y, 0, target.panel.visualTree.worldBound.height));
    hmm, reading that I thought there's some magic 2d transform.position, so I checked the docs, "Transform ... Returns a transform object for this VisualElement", so with my Unity head on I assume transform.position is well like all transform.position(s) I've ever used.

    I don't think my assumption / logic is correct.

    Could you someone at Unity confirm this is actually accessing the transform styling eg, translate?

    PS. If transform.position is actually just doing an offset then I think the clamping in the example shown above isn't right; clamping an offset to zero and the visualTree worldBounds, doesn't clamp it'ds position sensibly. Also is it to late to rename transform.position to transform.translate?
     
  2. SimonDufour

    SimonDufour

    Unity Technologies

    Joined:
    Jun 30, 2020
    Posts:
    578
    As you can see in the code for visualElement in the c# reference, they have been writing to the style for a while now.

    yes visualElement.transform.position is mapped to the translate style. The transform has been arround for much longer, but it will be deprecated at some point in favor of visualElement.style.translate as they are both the same.

    Code (CSharp):
    1. Vector3 ITransform.position
    2.         {
    3.             get
    4.             {
    5.                 return resolvedStyle.translate;
    6.             }
    7.             set
    8.             {
    9.                 style.translate = new Translate(value.x, value.y, value.z);
    10.             }
    11.         }
    12.  
    13.         Quaternion ITransform.rotation
    14.         {
    15.             get
    16.             {
    17.                 return resolvedStyle.rotate.ToQuaternion();
    18.             }
    19.             set
    20.             {
    21.                 value.ToAngleAxis(out float angle, out Vector3 axis);
    22.                 style.rotate = new Rotate(angle, axis);
    23.             }
    24.         }
    25.  
    26.         Vector3 ITransform.scale
    27.         {
    28.             get
    29.             {
    30.                 return resolvedStyle.scale.value;
    31.             }
    32.             set
    33.             {
    34.                 // This is the older API where Z-scaling doesn't make sense, forcing the value to Vector2.
    35.                 style.scale = new Scale((Vector2)value);
    36.             }
     
    spakment likes this.
  3. spakment

    spakment

    Joined:
    Dec 19, 2017
    Posts:
    96
    @SimonDufour Ah, great thanks for the clarification.

    I think it'd be fine if the docs at https://docs.unity3d.com/ScriptReference/UIElements.VisualElement-transform.html said that its mapped to the translate style "The transform object implements changes to the VisualElement object" is a little bit cryptic in this case though.

    Is there someone I should @ for the https://docs.unity3d.com/Manual/UIE-create-drag-and-drop-ui.html docs page?
    as that example is really missleading and caused me a lot of confusion about how it should be used.
     
  4. SimonDufour

    SimonDufour

    Unity Technologies

    Joined:
    Jun 30, 2020
    Posts:
    578
    The "report a problem on this page" directly open up a ticket with the doc tool. It probably won't be prioritized so it could take a moment for the page to be updated. The more "problem reported" the sonner someone will change this.
     
    spakment likes this.
  5. spakment

    spakment

    Joined:
    Dec 19, 2017
    Posts:
    96
    Awesome, reported, thanks