Setting top and bottom on a RectTransform

Hi,

I’d like to set the top and bottom on a rectatransform belonging to a prefab I’m instantiating…

So…

I instantiate the object and set the rect i.e.

rectTransform.rect.set(top,left,width,height);

However the instantiated objects top is not the value I’ve set!

Any ideas?

2 Likes

You have to use:

rectTransform.offsetMin = new Vector2(rectTransform.offsetMin.x, bottom);
rectTransform.offsetMax = new Vector2(rectTransform.offsetMax.x, top);

IIRC RectTransform.rect returns a struct copy of rectTransform’s, and you are setting the values on that copy only. Tim C. made a post on it one or two days ago.

Also, I know that while offsetMax.x corresponds to the “right” property, “right” is actually displayed negatively, so you have to set it to opposite what you might expect (i.e.: a right value of 5 becomes maxOffset.x = -5), so I have a feeling it might be the same for top.

Edit: Confirmed (and corrected) by Runevision below. Edited this post to state true information only.

39 Likes

Almost. It’s actually top that is the opposite of offsetMax.y. So in both cases, it’s offsetMax that’s inversed while offsetMin is not.

6 Likes

Ah, that makes sense! It seems I mixed up top and bottom entirely in that post. Fixed now, thanks for your input!

Senshi you have no clue how helpful you’ve been. I’ve been searching for 4 hours for this thread XD

3 Likes

Hi, are you able to give a code snippet example of this? I have been trying and unable to change the top and bottom of my buttons to position on screen.

Thanks

Hi, the above code snipper should still work. Keep in mind that there are both a pixel offset position (offsetMin/ Max) and an anchor position relative to the parent size (anchorMin/ Max). Perhaps you accidentally set both (to cancel eachther our) or just the wrong one?

Hi, after further searching I go a reply pointing me towards vertical layouts and that has fixed it all for me, thanks :wink:

Thank you! code is working!

You can also create a struct for top, left, right, bottom and then a function to convert the offset rect.

Thank you so much, this was very helpful!

After looking to do this as well I think I’ve found the proper way:

3 Likes

Seriously, how sh*t is it that you cannot use newButton.GetComponent().anchoredPosition.Set() ??? I also have been searching for hours until I found this thread

If that method is a noop, then please remove it. How can anyone know to use anchoredPos = new Vector() instead of .Set()?

1 Like

It’s a limitation in the C# language. It’s the same for every property of struct type.

Since structs are always passed by copy and not as reference, you get a copy of the struct, then you change the copy using the Set method, but the value inside the transform is not affected by this. It’s super annoying but not anything we can solve and still be C# language compatible.

1 Like

So you could fix it, it just wouldn’t be C# anymore, right?

Here’s your resolution.

1 Like

Sure. It works in javascript (unityscript).

(Actually I’m not completely sure if calling .Set will work in javascript, but you can definitely do transform.position.x = 5; - another thing you can’t do in C# for the same reason.)

But for C# we are committed to staying compatible with the official C# language, so fixing it there is not something we want to do, despite the annoyance.

1 Like

Smart move. Keep C# completely on it’s own. It helps everyone in the long run. Better yet, remove UnityScript and just force everyone to use C#. Problem solved :slight_smile:

1 Like

// [ left - bottom ]
slideArea.gameObject.GetComponent().offsetMin = new Vector2(10f, 10f);
// [ right - top ]
slideArea.gameObject.GetComponent().offsetMax = new Vector2(-10f, -10f);

This give you :
Left = 10
Top = 10
Right = 10
Bottom = 10

10 Likes

gross…

Just make a struct.

1 Like