- Home /
How to get accurate x/y for Rectangle Transform of Panel under canvas?
Edit: sorry if it's not clear. I'm trying to get accurate x/y coordinates for the Rectangle Transform of a Panel which is a child of a canvas object, and I want to do it form a script attached to the Panel. The end-goal is to get a UI item I'm positioning in the upper left corner of the panel.
Here's what I've tried, with the script attached to the Panel. (Obviously I didn't try them like this all at once, but one by one)
RectTransform transform = gameObject.GetComponent<RectTransform>();
Vector3 usefulPosition = Camera.main.WorldToViewportPoint(transform.anchoredPosition);
usefulPosition = Camera.main.WorldToViewportPoint(transform.position);
usefulPosition = Camera.main.WorldToViewportPoint(transform.localPosition);
usefulPosition = Camera.main.WorldToScreenPoint(transform.anchoredPosition);
usefulPosition = Camera.main.WorldToScreenPoint(transform.position);
usefulPosition = Camera.main.WorldToScreenPoint(transform.localPosition);
usefulPosition = Camera.main.ScreenToWorldPoint(transform.anchoredPosition);
usefulPosition = Camera.main.ScreenToWorldPoint(transform.position);
usefulPosition = Camera.main.ScreenToWorldPoint(transform.localPosition);
usefulPosition = Camera.main.ScreenToViewportPoint(transform.anchoredPosition);
usefulPosition = Camera.main.ScreenToViewportPoint(transform.position);
usefulPosition = Camera.main.ScreenToViewportPoint(transform.localPosition);
usefulPosition = Camera.main.ViewportToScreenPoint(transform.anchoredPosition);
usefulPosition = Camera.main.ViewportToScreenPoint(transform.position);
usefulPosition = Camera.main.ViewportToScreenPoint(transform.localPosition);
usefulPosition = Camera.main.ViewportToWorldPoint(transform.anchoredPosition);
usefulPosition = Camera.main.ViewportToWorldPoint(transform.position);
usefulPosition = Camera.main.ViewportToWorldPoint(transform.localPosition);
However, none of these are enough on their own to get the item I'm positioning in the upper left corner of the panel.
Can someone please help me out? Am I missing some kind of multiply by transform type thing? I've done a lot of googling on this, but apparently I suck at Google.
Answer by Eudaimonium · Sep 03, 2015 at 07:52 PM
You can use Rectangle transform utility class.
RectTransformUtility.RectTransformUtility.ScreenPointToWorldPointInRectangle(rectTransform, rectTransform.position, Camera.main, out usefulPosition);
^Disclaimer, browser typing, I think there is ".position" property of RectTransform class, it should be it's position on the canvas. Note that the function doesn't return the value, but it rather puts it into Reference of a Vector3 for the final argument (hence out keyword). It's a bit stupid, IMO. Can't use it in-line for anything.
Note: Rename your RectTransform reference to something else, JUST in case, because "transform" by default already fetches the gameObject's attached Transform component.
Answer by Positive7 · Sep 03, 2015 at 07:55 PM
Try this It will move Child Objects to :
GetComponent<RectTransform>().localPosition = new Vector3(0.0f,0.0f,0.0f);
GetComponent<RectTransform>().anchorMin = new Vector2(0.0f,1.0f);
GetComponent<RectTransform>().anchorMax = new Vector2(0.0f,1.0f);
GetComponent<RectTransform>().pivot = new Vector2(0.0f,1.0f);
Your answer
Follow this Question
Related Questions
Canvas, Scroll Rect, Panels, Panes? 0 Answers
Divide UI in sections 0 Answers
Unity UI - Fading Canvas/Panel 7 Answers
Animation not playing on game object activated by script 3 Answers
How to set the UI Text position to be relative to his "grandfather" prefab 0 Answers