Search code examples
unity-game-enginecanvascamera

Unity - How can the main camera show the entire canvas while being smaller than it?


I am relatively new to Unity and have been following a Udemy course for 2D game programming. One thing that stuck into my mind is that we have created a giant canvas compared to the camera, yet it shows the entire canvas when I start the game. What is the point of camera location/size if it is going to render the entire canvas? To be more specific, what is the relation between camera and canvas?

I researched and learned that we could change the Render Mode of canvas to fit the same size as the camera, thus eliminating the problem. Still didn't really understand why and how they both seem to work.

Here is a picture to demonstrate what I mean: Camera is at the bottom-left of the canvas


Solution

  • Your canvas is set to screen space overlay, which means it renders on top of everything that the camera/s render onto the screen. So it has no relation with the camera and it's size will depend on game view or display resolution.

    Screen space camera will make it so that it will render on top of everything else on a specific camera and it'll resize with the camera.

    And world space will put the canvas in the world like any other GameObject.

    https://docs.unity3d.com/Packages/[email protected]/manual/UICanvas.html