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

Those would be

RectTransform rectTransform;

/*Left*/ rectTransform.offsetMin.x;
/*Right*/ rectTransform.offsetMax.x;
/*Top*/ rectTransform.offsetMax.y;
/*Bottom*/ rectTransform.offsetMin.y;

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).


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.