2
\$\begingroup\$

I think it is done with the Anchors of the Rect Transform. I have tried all the possible combinations of the following default anchors:

two default anchors

Unfortunately, when I resize the button in the Scene, the text is always stretched:

text stretched with image button

\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

You're misunderstanding how RectTransform works. The basic Transform properties (position, rotation, scale) affect all 2D/GUI children regardless of what settings they have, just like they do with regular GameObjects. Transform scaling thus completely ignores whatever anchors you have set on the children.

RectTransform anchors determine how the object changes in reaction to the parent being modified, allowing you to fine-tune this relationship. A Middle-Left anchor makes the child ignore changes to the top, bottom, and right sides of its parent, but will move with the left edge as if you had moved the entire parent. The various Stretch anchors allow slaving to two or more sides at once--so a Middle-Stretch anchor would leave the child unaffected by changes to the parent's left and right sides, but stretching along the top and bottom to match its parent.

So this means you almost never want to fiddle with the parent's scale (except for perhaps animations). Instead only change the parent's Width/Height/Left/Right/Top/Bottom parameters, and the child's anchors will do the rest.

(Shameless plug: I wrote a script here that helps make RectTransform manipulations easier in code.)

\$\endgroup\$
6
  • \$\begingroup\$ I have tried to set the text child gameObject as middle center too. That's one of the possible combinations. Good to know that I only have to care about the child, not the parent. If I stretch the parent, the child is stretched too. \$\endgroup\$
    – chelder
    Sep 29, 2015 at 16:04
  • \$\begingroup\$ So, to clarify: you have the Text object anchor set to middle center, and after modifying the Button width etc the text still stretches? \$\endgroup\$
    – ketura
    Sep 29, 2015 at 16:35
  • \$\begingroup\$ I have just found out a "solution". If we redimension the button in the Scene view, the scale of the button changes together with its child text no matter of the anchors. The solution is to modify the width in the inspector (instead the scale). Silly problem, silly solution :/ \$\endgroup\$
    – chelder
    Sep 29, 2015 at 17:47
  • 1
    \$\begingroup\$ Ah, so you're unfamiliar with uGUI's RectTransform, then. The basic Transform properties (position, rotation, scale) affect ALL children, just like they do with regular GameObjects; this does not change just because you're working in the GUI. RectTransform is its own beast; you almost never want to scale, you want to set the width/height so it fills the part of the screen you want, then anchor it, and you're good to go. So what you have discovered isn't a "silly trick", it's the way it's supposed to be used. \$\endgroup\$
    – ketura
    Sep 29, 2015 at 18:00
  • \$\begingroup\$ Feel free to write an answer ketura. It will be more fair than if I do it. I have edited my question to fit better with your answer (Unfortunately, when I resize the button in the Scene, the text is always stretched:) \$\endgroup\$
    – chelder
    Sep 29, 2015 at 18:49
0
\$\begingroup\$

You can try this

button.GetComponent<RectTransform>().sizeDelta = new Vector2(newWidth, newHeight);

Or just change the Widthand Height of the buttons RectTransform component.

\$\endgroup\$
1
  • \$\begingroup\$ RIght now, the parent is scaled to (0.4375,1,1). So with some maths I can set the child to: 1-0.4375+1=1.5625 -> (1.5625,1,1). But I will have to do the calculations everytime I want to modify the parent. I'm wondering if there is an "automatic" way to do it. \$\endgroup\$
    – chelder
    Sep 29, 2015 at 16:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .