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.
\$\begingroup\$
\$\endgroup\$
4
2 Answers
\$\begingroup\$
\$\endgroup\$
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)
\$\begingroup\$
\$\endgroup\$
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).
RectTransform.sizeDelta = new Vector2(calculatedWidth, RectRansform.sizeDelta.y)
? \$\endgroup\$