- Home /
UI element RectTransform WorldToScreenPoint issue
How do I get the screen point of a RectTransform?
I've tried this ->
camera.WorldToScreenPoint(transform.position);
the "transform" belongs to the object that has a RectTransform. The supplied point is always a bit off, the offset amount depends on the screen size, the larger the screen size, the more of an offset there is. The canvas is set to World Space.
Thanks for any help.
Answer by Eba1337 · Sep 29, 2017 at 08:47 PM
Have you tried GetComponent RectTransform? Many position options!
Answer by SpiderJones · Sep 29, 2017 at 09:42 PM
Thank you for your reply. I am using this ->
camera.WorldToScreenPoint(gameObject.GetComponent<RectTransform>().position);
And still the screen position is off.
There was something like GetComponent RectTransform.anchoredPosition which returned the position on screen no need to use the WorldToScreenPoint, also If it's attached to canvas same.localPosition returns the position with the parent position calculated but I don't know if any of these solves your problem?
GetComponent RectTransform.anchoredPosition is not returning a screen position, it is returning a world space location, where 0 is in the middle of screen.
This also is not working ->
camera.WorldToScreenPoint(gameObject.GetComponent<RectTransform>().anchoredPosition);
Are you sure middle of the canvas is not zero? I'm not sure, not on a pc right now.
Your answer
Follow this Question
Related Questions
Find UI Image position in Screen Space 1 Answer
Aligning UI Elements in Code 0 Answers
Snap UI Text inside canvas 1 Answer
Unity UI Position 2 Answers
UI element moves down when another UI elements gets too close? 0 Answers