How to Change from Canvas Space to World Space?

Oops, I said the title wrong. Actually, how do I change the world space to canvas space?

I have the coordinates of the canvas space, but whenever the code sets a game object to that canvas space, it sends it to the coordinates but in WORLD SPACE.

How do I tell transform.position to use Canvas Space?!

(And BTW: I’ve already tried changing the Canvas Mode to Screen Space - it makes it even worse by moving the gameobject off the screen).

Camera.WorldToScreenPoint and Camera.ScreenToWorldPoint

Could you explain to me how to use this. I’ve tried doing it, but I keep failing and it keeps giving me errors :frowning:

Thanks!

could you explain your setup a little first?

What mode is your UI canvas in? what are you trying to do? (and don’t just say “I’m trying to convert from canvas space to world space” :stuck_out_tongue: )

The answers very much depends on what mode your canvas is in. If you are using overlay mode you can use the 2 methods already mentioned. If it is in camra mode or workspace mode it is a little more complicated.

Here’s exactly what is happening : I have a Canvas which contains my Game UI (Pause Button, Scores, etc.) and also contains my “Buttons” Panel. This Buttons Panel has 64 buttons, and each button has a list of coordinates. For each level, they go to a different position by looking at their own list of coordinates and going to the corresponding x and y value. These coordinates are WORLD positions, NOT SCREEN positions.

Because they’re world positions, the Canvas must be set to World Space for the positions to be read correctly so that they actually go to the right place. If it’s set to Screen Space - Overlay, it goes to a different place (because the 0,0 for Screen Space is different from the 0,0 for World Space).

However, my Game UI needs Screen Space so that it can adjust its position on the Canvas according to the phone’s screen-size. If it uses World Space, it won’t be able to adjust it’s position at all.

So, 2 things under one canvas need Screen Space and World Space. Which is impossible, since it can only be one.

Now, one thing I was thinking of was making 2 canvases. One for Screen Space (for the UI and one for World Space (for the Button panel). But this is the problem: The Screen Space canvas is ALWAYS one top and blocks the World Space canvas from view, because the Screen Space canvas is, well, the screen! So because of THIS, the button panel would never even be seen.

So, there is no easy solution. However, I thought: Hmmmm… what if I made it so that the Buttons could take the corresponding x and y values from the list, and then convert them into Screen Space coordinates, so that it could use a Screen Space. Then, this would mean that there would only one canvas needed: a Screen Space canvas.
However, to do this, I would need to convert the current x and y World Space coordinates that I have into Screen Space coordinates. So what commands would I need to do this?

P.S: I can’t actually CHANGE the list of coordinates I have into Screen Space because I put the coordinates in manually into a public variable. I get those coordinates from the X-Position and Y-Position values of the “Rect Transform” component. And the positions always show up as World Space coordinates for me (unless there’s an option to change that). Do you know of any way to change it?

Thanks! Hope that made sense - just try to imagine it in your head! :slight_smile:

1 Like