I have a 2d game object used in the UI. It contains a Rect transform, canvas renderer and image
I can change the width in the editor in the Rect transform for example from 200 to 100 and the width in scene in the editor is half as long. Great!
I just can’t seem to get it to work in script.
The object is anchored to the left. min (0, 0.5) max (0, .5) and Pivot (0, .5) Scale is 1,1,1
I have tried a number of ways in script and it just doesn’t display properly. I have a percent I want to shrink the object by. Setting percentEarnedThisSeg to .5 for example doesn’t give me an object half the original size.
I can change the width value in the editor while the game is running and get exactly what I want. I just can’t seem to do it in script.
Here are some failed attempts:
// try 1
segment.transform.localScale = new Vector3(percentEarnedThisSeg, 1, 1);
// try 2
segment.rectTransform.sizeDelta *= new Vector2(percentEarnedThisSeg, 1);
// try 3
var orgX = segment.rectTransform.rect.x;
var sizeDeltaX = orgX * percentEarnedThisSeg;
segment.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, sizeDeltaX);