1
\$\begingroup\$

The usual way to set the width or height for a RectTransform is this one as far as I know : RectTransform.sizeDelta = new Vector2(...); . I want to set the width of my object in an EditorScript, so if I would use the mentioned method, I would automatically lock my height value in the editor. But I want to freely adjust my height and at the same time let the width be calculated from my script. So my problem is, that RectTransform.sizeDelta = new Vector2(...); is locking both values in the Editor.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ Why not RectTransform.sizeDelta = new Vector2(calculatedWidth, RectRansform.sizeDelta.y)? \$\endgroup\$ Sep 6, 2016 at 1:32
  • \$\begingroup\$ That's all the question is about, your line of code would lock the height in my editor, but I still want to adjust it. \$\endgroup\$
    – TobiasW
    Sep 6, 2016 at 6:08
  • 1
    \$\begingroup\$ Tried it like this: pastebin.com/Rpeu3YmJ Don't see any height locking. \$\endgroup\$ Sep 6, 2016 at 12:08
  • \$\begingroup\$ Well shame on me for not trying your line of code... I did it like this : RectTransform.sizeDelta = new Vector2(calculatedWidth, RectRansform.rect.height); And that locked the height so I thought everything would do that... Thanks a lot! I would like to accept this as answer :) \$\endgroup\$
    – TobiasW
    Sep 7, 2016 at 5:52

2 Answers 2

3
\$\begingroup\$

You can read current height and set it again without changing. When you change height in the editor, it'll be changed between your code executions.

RectTransform.sizeDelta = new Vector2(calculatedWidth, RectRansform.sizeDelta.y)
\$\endgroup\$
2
\$\begingroup\$

This is a very common problem, actually. Unity provides SetSizeWithCurrentAnchors() for setting either width or height, ignoring the current anchors (which is almost certainly what you want).

\$\endgroup\$

You must log in to answer this question.

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