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.
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.
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?
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()?
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.
(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.
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
// [ 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