ShoulO
ShoulO

Reputation: 468

How to stretch resize Unity UI element on swipe in C#

...

How stretch Unity UI with mouse or finger "swipe"?

I tried using onDrag to adjust the Left parameter of RectTransform: ... But it shows error that transform.GetComponent<RectTransform>().rect; is read-only and I cannot edit it.

Also I tried to

    Vector2 v2 = GetComponent<RectTransform>().sizeDelta;
    v2.x = Input.mousePosition.x;
    GetComponent<RectTransform>().sizeDelta = v2;

Which resized it all and I need just left edge to move, and right edge to remain in place:

enter image description here

What is good way to achieve it? Thnx

Upvotes: 0

Views: 1427

Answers (1)

You need to re-position the pivot.

Notice how your UI element is getting larger from its center (that is: both the left and right edges are moving as you add width). This is because the pivot for the object is in the center (which it is, (0.5, 0.5)). In order to make it scale outward only to the left you need to adjust the pivot to the right edge, (1, 0.5)

enter image description here

Upvotes: 1

Related Questions