- Home /
UI issue: Getting screen coordinates from a World Space object
I've been writing scripts for UI objects using a canvas set to Screen space - Overlay. It's pretty simple to get the side/corner coordinates from all of the (rect)transforms. However, I'm attempting to modify the scripts to also work with canvases set to World Space.
The graphic raycast I'm using seems to work fine. However, I am not sure how to get the screen coordinates for the (rect)transforms.
I've read some about "Camera.WorldToScreenPoint," however, I don't know how that would tell me where the UI object's edges are located without me already knowing.
Is there a way for the graphic raycast to tell me how far inside of the object's borders it is?
Using Screen space - Overlay, I just need to get the position and size of the object, then it's simple math. But for an object that could be any distance from the camera... or not even on the camera at all... well, that's where I'm lost.
Thank you!
Your question is confusing. Do you want the distance to the side of an object, distance to the camera or screen coordinates?
I apologize. What I'm ultimately looking for is how to get the screen coordinates of a given edge of a UI object such as a panel if the canvas in set to use World Space.
I usually use a canvas set to Screen Space - Overlay. Because it automatically fills the screen, it is relatively simple to get the screen coordinates for an object's edges. I merely need to get the width/height of the object in question, divide it in half, then add/subtract that value to the object's position coordinates. When I compare that number with the canvas' coordinates, I then have the exact location of a given edge.
But for a canvas that does not fill the screen perfectly, and can dynamically change scale and position (compared to the screen), I don't know where to even begin to get a reference point to compare the UI object to.
I suppose one main problem is that, according to the object's RectTransform, the height and width never actually change no matter how far away from the camera it gets. So I can't use the RectTransform info to base coordinates on as I do with the canvas set to Screen Space - Overlay.
Ah, and I think I may have just made everything more confusing. Again, I apologize. I think 6am isn't a good time for me to try and explain things, haha! Thank you, though.
Ps. I'm wanting to use the coordinates so I can define exactly where my cursor can interact with the UI object. $$anonymous$$y raycast allows me to interact with the object if my cursor is ANYWHERE over the object. But if I wanted interaction to take place ONLY if the cursor were, for examples, 25 pixels inside the object's edges, I don't know how to get an edge reference.
Answer by hexagonius · Mar 05, 2015 at 08:16 PM
I don't know about the UI, but what I know is: if the canvas has a world space position and you get the width into units you can calculate a bordervector. transform.position (of the UI) minus transform.right * (width / 2), would give you a point on the left edge of the canvas. In world space. You could THEN Camera.main.WorldToScreenSpace(leftEdgePoint) to get the pixel coordinates of that point on screen. Then it's just a small step to the pixel difference of the cursor.
Your answer
Follow this Question
Related Questions
Raycast from 2D canvas object to world space 1 Answer
UI element in 3D space, reacts to click despite raycast target being infront 0 Answers
Can't interact with buttons in a UI Canvas in World Space Render Mode. 0 Answers
Different between IPointerClickHandler.OnPointerClick and MonoBehaviour.OnMouseDown 1 Answer
Most direct way to create OnCursorStay "event" in UI. 1 Answer