banner



How To Create Background In Unity 3d

Using the empty project nosotros created in the previous office, nosotros will now larn how to add together a background and some fancy clouds.

Calculation a background

Your first background will be static. We will use the following image:

TGPA background

(Right click to save the image)

Import the image in the "sprites" folder. Simply re-create the file in it, or drag and drop it from the explorer.

Practise not worry most the import settings for now.

In Unity, create a new Sprite game object in the scene.

New sprite

What is a sprite?

In general, nosotros phone call "sprite" a 2D image displayed in a video game. Here, it's a Unity specific object made for 2D games.

Add together the texture to the sprite

We will now select the actual sprite to display. Make sure that "New Sprite" is selected and look at the inspector. Fix the Sprite property to the "background" image:

Select a sprite

(You have to click on the small round icon at the right of the input box to show the "Select Sprite" inspector)

Y'all can run into that some other sprites are there. Those are the default images used past uGUI (the Unity UI organization).

"My sprite doesn't show upward in the dialog?": first, brand sure you lot are in the "Assets" tab of the "Select Sprite" dialog.

Some readers accept reported that, in their projection, this dialog was empty. The reason is that for some Unity installations, even with a fresh new 2D project, images are imported as "Texture" and not "Sprite".

To fix this, you lot demand to select the image in the "Project" pane, and in the "Inspector", alter the "Texture Blazon" holding to "Sprite":

Sprite mode

Nosotros don't know why everybody doesn't seem to take the same behavior.

Well, nosotros take ready a simple sprite displaying a cloudy heaven background. Yous can think information technology was a flake complicated just for that. In fact, we could take dragged the sprite from the "Project" tab directly correct into the "Scene".

Let's reorganize the scene.

In the "Hierarchy" pane, select the New Sprite. Rename it in Background1 or something you volition easily remember.

And then move the object to where information technology belongs: Level -> Background. Modify its position to (0, 0, 0).

Background is set

A quick exercise: indistinguishable the background and identify information technology at (twenty, 0, 0). Information technology should fit perfectly with the offset part.

Tip: you tin can duplicate an objet with the cmd + D (Bone X) or ctrl + D (Windows) shortcuts.

Background2 in place

Sprite layers

The side by side statement will seem pretty obvious, but it has some consequences: we are displaying a 2d world.

This means that all images are at the same depth, ie. 0. And you lot (as well every bit the graphics engine) don't really know who's going to be displayed starting time.

Sprite layers let us to tell Unity what is in the front end and what is in the back.

In Unity, we can modify the "Z" of our elements, and this will allow us to have layers. This is actually what we were doing in this tutorial before the Unity 5 update.

Simply we thought that it was a expert idea to employ Sprite Layers.

On your "Sprite Renderer" component, you have a field named… "Sprite Layer", currently set to Default.

If you click on information technology, a short list will prove:

Sorting layer list

Allow's add some layers to fit our needs (use the "+" button):

Sorting layer add

Apply the Groundwork layer to our groundwork sprite:

Set sorting layer

Tip: the settings "Order in Layer" is a manner to limit sub-layers. Sprites with lower numbers are rendered earlier sprites with greater numbers.

Notation: the "Default" layer cannot exist removed, because this is the layer used by 3D elements. You tin have 3D objects in your second game. Particles are considered as 3D objects by Unity, so they will be rendered on this layer.

Adding background elements

Also known as props. These elements aren't used to ameliorate the gameplay simply to visually enhance the scene.

Here are some simple flight platform sprites:

Platform sprites

(Right click to salvage the paradigm)

As you tin can encounter, nosotros got 2 platforms in one file. This is a skilful way to learn how to ingather sprites with the Unity tools.

Getting two sprites from one image

  1. Import the paradigm in your "sprites" binder
  2. Select the "platforms" sprite and go to the inspector
  3. Change "Sprite Mode" to "Multiple"
  4. Click on "Sprite Editor"

Multiple sprites

In the new window ("Sprite Editor"), yous can describe rectangles around each platform to slice the texture into smaller parts:

Sprite Editor

Call the platforms "platform1" and "platform2".

Tip: the pinnacle-left push button "Slice" allows you to chop-chop and automatically make this slow task.

Unity will find the objects within the image and will slice them automatically. Yous tin specify the default pivot indicate, or set up a minimum size for a slice. For a simple prototype without artifacts, it's actually efficient. Withal, if you use this tool, exist careful and check the result to exist sure to get what yous desire.

Now, under the paradigm file, you should see the two sprites separately:

Sprite Editor result

Calculation them to the scene

We will proceed similar for the groundwork sprite: create a new Sprite and select the "platform1" sprite (or drag them one by 1 from the "Project" to the "Scene" tab). Repeat for "platform2".

Ready their "Sprite Layer" to "Platforms".

Place them in the Middleground object.

Two shiny new platforms

And… it's working! I'm nonetheless amazed how simple it is now (to be honest, it was a bit tricky without the 2D tools, involving quad and image tiling).

Prefabs

Salvage those platforms equally prefabs. Just drag'n'driblet them inside the "Prefabs" folder of the "Projection" pane from the "Hierarchy":

Prefabs

Past doing and so, you lot will create a Prefab based exactly on the original game object. Y'all tin notice that the game object that you take converted to a Prefab presents a new row of buttons just under its name:

Prefab connection

Annotation on the "Prefab" buttons: if you change the game object afterwards, you tin can "Use" its changes to the Prefab or "Revert" it to the Prefab properties (canceling whatsoever change you've made on the game object). The "Select" button motion your choice directly to the Prefab nugget in the "Project" view (information technology will be highlighted).

Creating prefabs with the platform objects will make them easier to reuse afterward. Simply drag the Prefab into the scene to add a re-create. Endeavor to add another platform that way.

Yous are now able to add more platforms, alter their positions, scales and planes.

You lot can put some in groundwork or foreground too. Remember that the "Background", "Middleground" and "Foreground" objects are just folders. And then you need to set up the right "Sprite Layer" (Platforms) and change the "Order in Layer".

Use -10 for far platforms, and increment this number as you lot attain the foreground. An example:

Add a platform in background

It'south not very fancy but in two chapters we will add a parallax scrolling and information technology volition suddenly bring the scene to life.

Camera and lights

Well. In the previous version of this tutorial (for Unity four.2), nosotros had a long and detailed explanation on how to set the camera and the lights for a second game.

The good news is that information technology's completely useless at present. You have nothing to do. Information technology just works™.

Aside: if you click on the Main Camera game object, y'all can see that information technology has a "Project" flag set up to "Orthographic". This is the setting that allows the camera to render a 2d game without taking the 3D into business relationship. Keep in mind that even if y'all are working with 2D objects, Unity is nonetheless using its 3D engine to return the scene. The gif above shows this well.

Adjacent step

You have but learned how to create a unproblematic static background and how to brandish it properly. Then, we accept taught you how to make uncomplicated sprites from an image.

In the next affiliate, we volition learn how to add a player and its enemies.

How To Create Background In Unity 3d,

Source: https://pixelnest.io/tutorials/2d-game-unity/background-and-camera/

Posted by: leonardgoidesseem.blogspot.com

0 Response to "How To Create Background In Unity 3d"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel