How To Make 2D Background In Unity

Posted on

How To Make 2D Background In Unity. All we’ll need to do is set the scale of the background based on it’s sprite’s size compared to the size of the camera at runtime, and maintain the aspect ratio of the original image. As we’ll see in the code below, unity actually makes maintaining the aspect ratio really.

Unity 5] Tutorial: How To Create An Infinite Background (2D) - Youtube
Unity 5] Tutorial: How To Create An Infinite Background (2D) – Youtube from www.youtube.com

Unity can dynamically stretch and tile designated parts of a sprite to allow one sprite to serve as the border or background for ui elements of many sizes. 1) drag your image file into scene view to create a sprite 2) set the scale to (x960 y1280 z0) and the position somwhere in the background 3) use the scale tool to make it small again. Yeah, a background can be something as simple as a texture (jpg, etc).

Importing The Sprite Sheet Into Unity.

That’s why we’ve created a free unity asset called texturepacker importer that automates the process for you. Change texture type to sprite (2d and ui). Star) and assign it to that gui texture.

Like A Color Gradient From Left To Right:

Backgroundstyleproperties note avoid using the slice background style properties. Next, add six child 2d sprites. //these can be set in editor public gameobject sprite1;

All We’ll Need To Do Is Set The Scale Of The Background Based On It’s Sprite’s Size Compared To The Size Of The Camera At Runtime, And Maintain The Aspect Ratio Of The Original Image.

Putting the parallax scrolling together in unity we are now ready to put everything together. Create a new layer (example: How do you make a scrolling background in unity 2d?

You Should Create 2 Cameras.

Create a new 2d project in unity. Select the original texture (not the gameobject). Name the first one main camera, name the second one background camera.

//Put Following Code In Function That Changes Sprites //This Sets Both Of These To Be The Opposite Of Their Current State //Setactive() Will Set The Game Object Before It To The Active State.

Yeah, a background can be something as simple as a texture (jpg, etc). You would have to split the sprites manually if you just copy and paste the sprite sheet into unity. A better solution would be to write a little code to dynamically scale the background as needed.

Leave a Reply

Your email address will not be published.