Search Unity

Visual Element Scaling Pivot Position

Discussion in 'UI Toolkit' started by Pelican_7, Sep 18, 2019.

  1. Pelican_7

    Pelican_7

    Joined:
    Nov 25, 2014
    Posts:
    190
    Hi,

    I'm animating a Visual Element hierarchy on screen by adjusting its transform.scale property via the Scheduler API. This causes the visual element to scale around its origin position in the top left corner. How can I adjust the pivot position around which the visual element will scale? This is in order to, for example, scale the element around its rect's center. In Unity UI this can be achieved by adjusting the RectTransform's pivot position.

    Thanks,
    -andy.
     
  2. mcoted3d

    mcoted3d

    Unity Technologies

    Joined:
    Feb 3, 2016
    Posts:
    1,003
    You cannot change the pivot at this time, but we have plans to revise this in the future. So in the meantime, the only way to change the pivot of a transform is to add an intermediate VisualElement.

    For example, add your VisualElement to an empty parent, change the position of the element so that it is centered relative to its parent, then scale the parent.
     
    Last edited: Sep 20, 2019
    Pelican_7 likes this.
  3. Pelican_7

    Pelican_7

    Joined:
    Nov 25, 2014
    Posts:
    190
    Ahh, ok great. Thanks a lot for the solution @mcoted3d.
     
  4. Nexer8

    Nexer8

    Joined:
    Dec 10, 2017
    Posts:
    271
    Any updates on this? The empty parent solution only works when you work with pixel values.
     
  5. mcoted3d

    mcoted3d

    Unity Technologies

    Joined:
    Feb 3, 2016
    Posts:
    1,003
    Can you share more details about what kind of VisualElement configuration causes issues for you?
     
  6. Nexer8

    Nexer8

    Joined:
    Dec 10, 2017
    Posts:
    271
    For example if you want an element to be 50% the width and 50% of the height of its container, but the container is an empty that you use as pivot, then 50% of 0 is going to be 0. Being able to adjust the origin would make us not need the empty, and the problem would be fixed. This also allows for buttons to change size on hover without complications because it’s position is it’s center. In general it just makes it easier to handle positioning rotation and scaling of VisualElements without having to do all sorts of trickery.
     
    MousePods likes this.
  7. mcoted3d

    mcoted3d

    Unity Technologies

    Joined:
    Feb 3, 2016
    Posts:
    1,003
    100% agree. I can't tell exactly when this feature will be implemented, but it is quite high on our todo list. In the meantime, you'll have to use the parent hack, unfortunately.
     
    Nexer8, Midiphony-panda and MousePods like this.
  8. Nexer8

    Nexer8

    Joined:
    Dec 10, 2017
    Posts:
    271
    Great to hear! Also, is there a productboard similar to the one for the scriptable render pipelines where we can see what is being worked on, what has been implemented and what is under consideration?
     
    YuuHao likes this.
  9. mcoted3d

    mcoted3d

    Unity Technologies

    Joined:
    Feb 3, 2016
    Posts:
    1,003
    We don't have a public roadmap, but I'll pass that feedback forward. Thanks!
     
  10. Midiphony-panda

    Midiphony-panda

    Joined:
    Feb 10, 2020
    Posts:
    243
    Any chance this will make the next package release ? :D

    This would be soooo convenient for animating Visual Elements...
     
  11. Midiphony-panda

    Midiphony-panda

    Joined:
    Feb 10, 2020
    Posts:
    243
    Bump (my last one I promise, I understand that you already have enough pressure from every side :D)

    Currently investigating a basic custom animation tool, and it's unfortunate that we cannot (yet :p) modify the pivot.

    AnimationWithDirectSOEditingHD.gif
    AnimationWithEditor.gif
     
  12. MousePods

    MousePods

    Joined:
    Jul 19, 2012
    Posts:
    811
    Midiphony-panda likes this.
  13. Midiphony-panda

    Midiphony-panda

    Joined:
    Feb 10, 2020
    Posts:
    243
    Crossing fingers to have something at least in C# side before 2021.2 in the package

    The parent hack workaround won't be appreciated by my colleagues (increasing again risk of us dropping UITK)
     
  14. SimonDufour

    SimonDufour

    Unity Technologies

    Joined:
    Jun 30, 2020
    Posts:
    575
    It should come to 2021.2 alpha over the next months, along with a few other improvements, if everything goes according to the plan. :)
     
  15. Midiphony-panda

    Midiphony-panda

    Joined:
    Feb 10, 2020
    Posts:
    243
    Thanks for the visibility :)
     
  16. Chuck_S

    Chuck_S

    Joined:
    Dec 11, 2018
    Posts:
    15
    @Midiphony-panda - I make a new class anytime I want to make some extension methods, so I've got the following class:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UIElements;
    3. public static class VisualElementExtensionMethods
    4. {
    5.     public static void Rotate(this VisualElement item, float angleDegrees)
    6.     {
    7.         // Get the x/y location of the center note - these are constant unless the parent rescales; i.e., they don't change with rotation.
    8.         float x0 = item.contentRect.center.x;
    9.         float y0 = item.contentRect.center.y;
    10.  
    11.         // Convert Cartesian to Polar
    12.         float r = Mathf.Sqrt(x0 * x0 + y0 * y0);
    13.         float theta0 = Mathf.Atan2(y0, x0);
    14.  
    15.         // Calculate the location of the center of the VisualElement after rotating
    16.         // Note: The rotation you want is *in addition* to the "default" polar angle from origin to the center
    17.         float x = r * Mathf.Cos(theta0 + (Mathf.Deg2Rad * angleDegrees));
    18.         float y = r * Mathf.Sin(theta0 + (Mathf.Deg2Rad * angleDegrees));
    19.  
    20.         // Actually do the requested rotation
    21.         item.transform.rotation = Quaternion.Euler(0f, 0f, angleDegrees);
    22.  
    23.         // Finally, rotation happens about the upper-left corner of the VisualElement, so you need to shift the position
    24.         // to get the rotated center to be coincident with the un-rotated center.
    25.         float xDelta = x0 - x;
    26.         float yDelta = y0 - y;
    27.         item.transform.position = new Vector3(xDelta, yDelta, 0f);
    28.     }
    29. }
    Now I can do stuff like myVisualElement.Rotate(90) and it'll rotate about its center. Hope this helps! Had the same problem so I figured I'd post my answer here for everyone to use. You were asking about it specifically so I figured I'd tag you in the response.
     
    Nexer8, Midiphony-panda and MousePods like this.
  17. Midiphony-panda

    Midiphony-panda

    Joined:
    Feb 10, 2020
    Posts:
    243
    Thank you for the snippet and the idea :)
     
  18. VisionEEG

    VisionEEG

    Joined:
    Aug 29, 2018
    Posts:
    28
    Is this pivot feature available in 2021.2.1f1? We have a complex hierarchy of various elements and scaling is centered to an unknown position. Sometimes, elements goes out of the screen while scaling, we need to control the position of the root. We apply the scale this way:
    Code (CSharp):
    1. UIDocument.rootVisualElement.transform.scale
     
  19. SimonDufour

    SimonDufour

    Unity Technologies

    Joined:
    Jun 30, 2020
    Posts:
    575
    Catsoft-Studios likes this.
  20. SimonDufour

    SimonDufour

    Unity Technologies

    Joined:
    Jun 30, 2020
    Posts:
    575
    You can also set the value in the UI bulder/USS instead of code if it always needs to scale around the same position.
     
  21. VisionEEG

    VisionEEG

    Joined:
    Aug 29, 2018
    Posts:
    28
    It works, thanks!