How to access RectTransform's left, right, top, bottom positions via code?

53.1k Views Asked by At

I have a UI Canvas with render mode world space set. For all UI elements that belong to this canvas, I am seeing 'left', 'right', 'top' and 'bottom' variables in the RectTransform component in the editor. Any ways to access these variables via code?

5

There are 5 best solutions below

0
On BEST ANSWER

Those would be

RectTransform rectTransform;

/*Left*/ rectTransform.offsetMin.x;
/*Right*/ rectTransform.offsetMax.x;
/*Top*/ rectTransform.offsetMax.y;
/*Bottom*/ rectTransform.offsetMin.y;
1
On
RectTransform rt = GetComponent<RectTransform>();
float left   =  rt.offsetMin.x;
float right  = -rt.offsetMax.x;
float top    = -rt.offsetMax.y;
float bottom =  rt.offsetMin.y;

TL;DR

From the values displayed in the Inspector, my analysis is that Left, Right, Top and Bottom are positive if the corresponding bounds are in the rectangle shaped by the anchors of the RectTransform.

offsetMin.x as Left and offsetMin.y as Bottom always fulfill this assessment however offsetMax.x as Right and offsetMax.y as Top does not.

I simply took the opposite values of offsetMax to make it compliant (basic space modification).

0
On

You can also easier set all values in two lines. Just get offsetMin which is Vector2 and set two parameters (first is Left and second is bottom). Then do the same with offsetMax (first is right and second is top). Remember that offsetMax is reversed so if You want to set Right = 20 then You need to set value -20 in script.

The code bellow sets values:

Left = 10,

Bottom = 20,

Right = 30,

Top = 40

GameObject.GetComponent<RectTransform> ().offsetMin = new Vector2 (10,20);
GameObject.GetComponent<RectTransform> ().offsetMax = new Vector2 (-30,-40);

I hope it will help someone else ;)

2
On

The above two answers are kinda on the right track, I have been procrastinating on my project due to this but found out while programming in bed. The offsetMin and offsetMax are what you need but they aren't everything, you need to include the anchors from the rect transform:

public RectTransform recTrans;

// Use this for initialization
void Start () {
    Vector2 min = recTrans.anchorMin;
    min.x *= Screen.width;
    min.y *= Screen.height;

    min += recTrans.offsetMin;

    Vector2 max = recTrans.anchorMax;
    max.x *= Screen.width;
    max.y *= Screen.height;

    max += recTrans.offsetMax;

    Debug.Log(min + " " + max);
}

If you plug this into a dummy class it should give you the correct readings regardless of what anchors you are using.

The way it works should be obvious but a small explanation wouldn't hurt.

The offsets are referring to the difference in position the min and max are from the center point of the rect transform, adding them to the anchor bounds gives the correct rect transform's min and max. Though the anchor min and max are normalised so you'll need to scale them back by multiplying by the screen size.

0
On

I need to adjust those properties very often so I've added a shorter and more convenient way to set them.

I wrote an extension for RectTransform where you can set these properties very simple:

public static class Extensions
{
    public static void SetPadding(this RectTransform rect, float horizontal, float vertical) {
        rect.offsetMax = new Vector2(-horizontal, -vertical);
        rect.offsetMin = new Vector2(horizontal, vertical);
    }

    public static void SetPadding(this RectTransform rect, float left, float top, float right, float bottom)
    {
        rect.offsetMax = new Vector2(-right, -top);
        rect.offsetMin = new Vector2(left, bottom);
    }
}

To use the extension, you simply call the methods like this:

var rect = YourGameObject.GetComponent<RectTransform>()

// with named parameters
rect.SetPadding(horizontal: 100, vertical: 40);

// without named parameters (shorter)
rect.SetPadding(100, 40);

// explicit, with named parameters
buyButton.SetPadding(left: 100, top: 40, right: 100, bottom: 40);

// explicit, without named parameters (shorter)
buyButton.SetPadding(100, 40, 100, 40);