- Home /
Find UI Image position in Screen Space
In my user interface (based on UUI) I use a script to detect mobile touch input events, then compute the distance from the center of an image that represents a button. I don't know if this is the best way, but it allows me to have the advantages of touch (ie: keep pressed, no delay between first and second click, and so on). In addition I can have a touch-detect area that can be slightly larger than the button itself, and this allows me to have clean interface and no user input errors.
Said that, it used to work everything smoothy, with this simple code (very, very reduced):
// ...
pos = button.transform.position;
// ...
distance = Vector2.Distance(touch.position, pos);
if (distance < touchSize)
// ...
With canvas set in Overlay mode. I'm not aware of the reason why now it does not work anymore. It may be the Unity update, or some change in canvas, camera or alignment, anyway now pos is simply, and always (0, 0). It returns (0, 0) for different buttons in different positions. If I change the position of a parent, it returns the position of the parent. Why???
I tried these possible codes:
pos = button.transform.position;
// this used to work and now returns (0,0)
//
pos = button.transform.localPosition;
// this returns a number that depends on alignment: for example it can contain negative numbers if the button is aligned to the center and positioned on the left. Should I calculate the screen position by myself?
//
pos = button.GetComponent<RectTransform>().position;
// this again returns (0,0) !! same as transform.position
//
pos = button.GetComponent<RectTransform>().localPosition;
// and this again returns a relative value !! same as transform.localPosition
//
pos = Camera.main.WorldToScreenPoint(button.transform.position);
// this returns some number that is the same also if I move the button, as soon as button.transform.position returns zero.
//
pos = RectTransformUtility.WorldToScreenPoint(null, button.transform.position);
// this returns always (0,0). As a matter of fact, this method seems to return the same that I get using the component RectTransform.
//
pos = Camera.main.WorldToScreenPoint(button.GetComponent<RectTransform>().localPosition);
// this returns some values that are completely wrong at a first investigation, as if the camera wasn't aligned with the canvas (that is Overlay). I didn't check more, but this could be correct in portrait mode, but I use landscape...
//
pos = new Vector2(Screen.width / 2 + button.GetComponent<RectTransform>().localPosition.x, button.GetComponent<RectTransform>().localPosition.y);
// This is how it currently works. I had to calculate manually the position on the screen, considering that I know that the button is centered horizontally and at the bottom. This is **by far** a bad option because as soon as I should change anything on the formatting of the scene, this would stop working.
//
I'm really getting crazy on this. It seems that it should be so obvious and simple: I simply want to know the coordinates of the center of a UI Image in Screen Space. I don't have anymore ideas!! Does some one have any idea on how to do it?
Thank you.
Answer by Diablo404 · Nov 24, 2015 at 07:05 PM
Maybe you're looking for GetComponent().anchoredPosition
http://docs.unity3d.com/ScriptReference/RectTransform-anchoredPosition.html