[Unity 4.6 Beta] Rect Transform Position (New UI System)

Hello Unity Users!
I have a question. How do I access the Position of a Rect Transform. I don’t mean the World space Coords, I mean the Pixel Coordinates. (The Coordinates on Screen Space).

I just cant find its Position (Its not localPosition or position)
I have already found the Size which is called sizeDelta but I cant find the position to it.

Thank you very much!

Solution found!

Its called anchoredPosition.

When you are under a canvas in “Screen Space - Camera” mode, then your position is measured in world coordinates. Its significant since you can’t easily compare it to things like Input.mousePosition Vector2.

What you want to do to get the position of the RectTransforms pivot on screen is to use the WorldToScreen() function of the camera that is rendering that UI element. like so.

Vector2 startPointPos = myCam.WorldToScreenPoint(startPoint.position);

Thank you so much, @mwk888. I never would have thought of touching any function dealing with World Space for elements of my Screen Space - Overlay Canvas.

Your function allowed me to complete my script that takes a secondary camera and automatically sets it’s Viewport Rect to match a “panel” object in my UI. I placed this script onto the Panel’s GameObject. It turns the panel into a “magic window”, so-to-speak.

Need to change where the secondary camera view displays during development, just move the panel!

I’m posting the script here, for anyone to use. It’s good for rear-view mirrors, mini-maps, etc…

using UnityEngine;
using System.Collections;

public class UICameraWindow : MonoBehaviour {

    public GameObject cameraObj;     // drag your secondary camera to this in the inspector
    private Camera camera;
    private RectTransform me;

    public bool inUse = false;

    private int sW = 0;
    private int sH = 0;


	// Use this for initialization
    void Start()
    {
        camera = cameraObj.GetComponent<Camera>();
        me = this.gameObject.GetComponent<RectTransform>();

        if (inUse)
            Init();
    }
	
	// Update is called once per frame
    void Update()
    {
        if (inUse)
        {
            if ((Screen.width != sW) || (Screen.height != sH))
                Init();
        }
    }


    void Init()
    {
        sW = Screen.width;
        sH = Screen.height;

        camera.pixelRect = GetScreenRect(me);
    }


    public void TurnOn(bool yes)
    {
        inUse = yes;

        if (inUse)
            Init();

        cameraObj.SetActive(yes);
    }


    public Rect GetScreenRect(RectTransform rectTransform)
    {
        Vector3[] corners = new Vector3[4];

        rectTransform.GetWorldCorners(corners);

        float xMin = float.PositiveInfinity;
        float xMax = float.NegativeInfinity;
        float yMin = float.PositiveInfinity;
        float yMax = float.NegativeInfinity;

        for (int i = 0; i < 4; i++)
        {
            // For Canvas mode Screen Space - Overlay there is no Camera; best solution I've found
            // is to use RectTransformUtility.WorldToScreenPoint) with a null camera.

            Vector3 screenCoord = RectTransformUtility.WorldToScreenPoint(null, corners*);*

if (screenCoord.x < xMin)
xMin = screenCoord.x;
if (screenCoord.x > xMax)
xMax = screenCoord.x;
if (screenCoord.y < yMin)
yMin = screenCoord.y;
if (screenCoord.y > yMax)
yMax = screenCoord.y;
}

Rect result = new Rect(xMin, yMin, xMax - xMin, yMax - yMin);

return result;
}
}

You can also easily access the rect of the UI Element if you get the RectTransform Component.
Remember to Import UnityEngine.UI !

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class TestClass : MonoBehaviour {

public Rect getScreenCoordinates(RectTransform rT){
		return rT.rect;
}

}

public void Inside(Vector3 Mouse_position, Camera Cam)
{
if (RectTransformUtility.RectangleContainsScreenPoint(slider.GetComponent(), Mouse_position, Cam))
{
Clicked = true;
}

}

This worked better for me, because I prefer to have the origo in Low Left corner:

Vector2 position = new Vector2(100, 100);

//Set the origo to zero
uiImage.rectTransform.anchoredPosition = Vector2.zero;
//Position will now be from screen low left corner
uiImage.rectTransform.position = position;

If the canvas is in Screen Space Overlay AND the element you want to position is attached to some UI child than is good to parent it to the canvas, set the position and back reparenting. Here an example, where the UI element is attached to a GameObjec, to be attached to the Update:

            Vector3 element = transform.position;
            Vector2 viewport = Camera.WorldToViewportPoint(element);

             Vector2 screenPosition = new Vector2
            (
                viewport.x * Screen.width,
                viewport.y *Screen.height
            );

// The tricky part:
            label.transform.parent = Canvas;
            label.anchoredPosition = screenPosition;
            label.transform.rotation = new Quaternion();
            label.transform.parent = transform;

Where Canvas is the RectTransorm of the Canvas and Camera is the Camera you are using

Thanks mwk888! Your script works great. It’s really useful.