Background of MainCamera (Unity C#)

Viewed 76311

I writing simple runner game

I have Main Camera and have Image. I want to set this Image as Main Camera background.

Screen of my Main CameraMain Camera

5 Answers

that other answer seems so out of date. I thought I'd share something newer. Just simply:

  • Right click your Main Camera, select UI -> Image.
  • Select your image and on the little box in Rect Transform where it says "stretch" and "stretch", click on that box. Now a menu will appear with a bunch of more boxes. Hold ALT and select the box furthest **Down Right **. This is the box with the blue arrows pointing at all directions.

Now you have fit your image to your canvas. In addition to this, you will need to fit your canvas to your camera.

  • Select your canvas. Within the Canvas' inspector, look for a component called Canvas. Change the Render Mode to Screen Space - Camera. Now it will ask you for a camera at the Render Camera box. Simply drag your Main Camera to Render Camera and voila, you have now a screen filled background without having 2 cameras lmao :)

Edit: Do not forget to turn off Raycasting for both the image and the canvas!

STEPS:

  • Make a new Canvas.
  • In that canvas, on Canvas component set Render Mode to Screen Space - Camera.
  • Render Camera to your main camera (drag&drop),Plane Distance are the units, at which the background would render.
  • Right-click on the newly created canvas and create an Image inside of it.
  • Set Source Image(drag&drop) to your desired background sprite.
  • Stretch your image to the full screen size.
  • You can do this as follows: in the Rect Transform of that image, there is a gray square in it with some arrows, and lines (saying stretch on top and left side), click it, a window with multiple squares would open, hold down the Alt and click the most bottom-right square.

For Users using URP Camera, setting Background is easy.

In URP we have two types of cameras Base And Overlay. Its the first option of Camera Component called RENDER TYPE

  1. Set your MainCamera as Base Camera and culling mask to nothing.
  2. Create two more cameras as Overlays.
  3. 1 of them is going to render the whole game and the other one is going to render the background. Suppose I name them RenderGame & RenderBackground.
  4. For background create separate CANVAS with screenSpace - Camera. and put the Overlay camera which is going to render the background in it. and set its layer as "Background Layer"
  5. create and assign a separate layer for canvas containing background.
  6. For RenderGame camera set culling mask to everything and then deselect "Background Layer".
  7. For RenderBackground set culling mask to only "Background Layer".
  8. And last In mainCamera There is the option called Stack under which first put RenderBackground and then RenderGame. Order is Important

For help you can refer to Image

Related