user5677145
user5677145

Reputation:

Background of MainCamera (Unity C#)

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

Upvotes: 20

Views: 91928

Answers (5)

Zeeshan Qaswar
Zeeshan Qaswar

Reputation: 1

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

Upvotes: 0

Aravinth
Aravinth

Reputation: 113

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.

Upvotes: 0

Markiian Benovskyi
Markiian Benovskyi

Reputation: 2161

** Edit 2020

Unity now has a simple way to do this, in Unity 2018.4+.

Follow these few steps to add texture as a camera background:

  1. Create a new Canvas that would hold your image.
  2. 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.
  3. Right-click on the newly created canvas and create an Image inside of it.
  4. On the newly created image on Image component:
    • Set Source Image to your desired background sprite.
  5. 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.

Now you should have a tecture as camera background!


** old Unity

You can make it using a Skybox, or you can use a GUI Texture.

Here's how I made it some time before:

  1. Create a new camera (GameObject -> Create Other -> Camera), and name it "Background Camera".

  2. Create a new GUI Texture (GameObject -> Create Other -> GUI Texture), and name it "Background Image".

  3. Click the "Layer" dropdown menu in the Background Image's inspector pane, and select "Add Layer".

  4. In the next free "User Layer" slot, create a new layer name called "Background Image". This will be directly under the layer named "Terrain" if you haven't yet added any others.

  5. Select your Background Image in the hierarchy, and give it the desired texture, and set the x, y, width and height under "Pixel Inset" so that it fills the screen appropriately.

  6. Near the top of the inspector window, Use the layer dropdown menu to assign your "Background Image" layer that you defined earlier to this gameobject.

Now select your Background Camera in the hierarchy, and adjust these settings in the inspector:

  1. Un-Check Flare Layer and Audio Listener (but leave GUILayer enabled)

  2. Set Clear Flags to Solid Color

  3. Set Depth to -1

  4. Set Culling Mask, first to "Nothing", and then to "Background Image"

Now Select you other (main) camera, and in the inspector:

  1. Set its Clear Flags to "Depth Only"

  2. Click its culling mask setting, and un-check "Background Image". This should result in the culling mask displaying as "Mixed ..."

Voila, this should give you your GUI Texture rendered by your background camera, behind everything else rendered by your main camera. And for any other additional cameras (eg, other camera angles) that you want to use, just repeat the last two steps on them.

Upvotes: 67

sansa
sansa

Reputation: 904

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!

Upvotes: 28

Chris
Chris

Reputation: 41

Unity comes with a skybox functionality which you could use to create a background. It may take some reshaping but you should be able to reach the desired effect with the tutorial here.

Upvotes: 2

Related Questions