- Home /
Access "Left", "Right", "Top" and "Bottom" of RectTransform via script
Hi guys. I have a quick question about RectTransforms.
I need to access the "Left" property in a UI Component FROM SCRIPT so I can shift it along a bit in certain situations. What's the easiest/simplest way to do this? Is it a value that is inferred from other stuff, like the sizeDelta and the position, and if so, what is the best way to manipulate those to get that apparent change?
Thanks, Harry
Answer by Eldoir · Mar 10, 2019 at 02:15 PM
Hi, I know this is an old thread but I landed here in 2019 so I guess this is still valuable. I just created a little script to answer your question. Create a new script called RectTransformExtensions (or whatever you want), and put that in there:
using UnityEngine;
public static class RectTransformExtensions
{
public static void SetLeft(this RectTransform rt, float left)
{
rt.offsetMin = new Vector2(left, rt.offsetMin.y);
}
public static void SetRight(this RectTransform rt, float right)
{
rt.offsetMax = new Vector2(-right, rt.offsetMax.y);
}
public static void SetTop(this RectTransform rt, float top)
{
rt.offsetMax = new Vector2(rt.offsetMax.x, -top);
}
public static void SetBottom(this RectTransform rt, float bottom)
{
rt.offsetMin = new Vector2(rt.offsetMin.x, bottom);
}
}
Now you can call these methods like this :
myRectTransform.SetLeft(100);
Hope this helps!
$$anonymous$$ore utility scripts for the common folder! Thanks!
Could this be implemented to DoTween? This works perfectly but I would like to add some delay animation to it. Do you think it's possible to tweak this around? Thanks!
Answer by Janibasha · Aug 02, 2017 at 12:19 PM
Actually, we cannot access the top, left, right, bottom individually as I know. so "Left", "Right", "Top" and "Bottom" of RectTransform properties so we have offsetMin and offsetMax which return a vector as follows
PanelSpacerRectTransform.offsetMin = new Vector2(0, 0); // new Vector2(left, bottom); PanelSpacerRectTransform.offsetMax = new Vector2(-360, -0); // new Vector2(-right, -top);
Answer by Ay-mak · Apr 07, 2019 at 12:23 PM
The easiest and cleanest solution is to simply use RectTransform.SetInsetAndSizeFromParentEdge. See this page for more information : https://docs.unity3d.com/462/Documentation/ScriptReference/RectTransform.SetInsetAndSizeFromParentEdge.html
If you want your object to fill the entire space of the parent, just do this:
RectTransform rectTransform = gameObjectButton.GetComponent<RectTransform>();
rectTransform .SetInsetAndSizeFromParentEdge(RectTransform.Edge.Bottom, 0, 0);
rectTransform .SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 0, 0);
rectTransform .SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, 0, 0);
rectTransform .SetInsetAndSizeFromParentEdge(RectTransform.Edge.Right, 0, 0);
Well, what it actually did in my case is collapsing the control. To be honest, this method is a complete mystery. I've exa$$anonymous$$ed the code and I can not suggest a single case when I'd use it.
Unity is a good engine, but their docs suck. With years the "entry threshold" gets higher and higher. One day, they will simply stop getting new adopters. It will be a sad day for the open/free software community.
Answer by digzou · Feb 07, 2015 at 08:40 AM
Bump. Hope it gives you some idea.
http://forum.unity3d.com/threads/setting-top-and-bottom-on-a-recttransform.265415/