0% found this document useful (0 votes)
5 views

Lecture 11

This lecture covers the basics of 2D game development in Unity, including the use of sprites, sprite renderers, and creating scenes. It explains how to set up the Unity editor for 2D mode, manage sprite textures, and utilize layers for organization and collision detection. Additionally, it introduces animation techniques and provides resources for creating a simple 2D game project.

Uploaded by

Abdullah Azmat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lecture 11

This lecture covers the basics of 2D game development in Unity, including the use of sprites, sprite renderers, and creating scenes. It explains how to set up the Unity editor for 2D mode, manage sprite textures, and utilize layers for organization and collision detection. Additionally, it introduces animation techniques and provides resources for creating a simple 2D game project.

Uploaded by

Abdullah Azmat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Lecture No 11

 Unity Project
◦ Introduction to basic Graphical components (objects)
◦ 2D / 3D graphics
 2D Game Development in Unity
◦ Sprites and Sprite Renderer
◦ Creating a basic scene
◦ Animations
◦ Layers
 When creating a project in Unity, it's possible to set up the editor for
2D Mode:

 In this mode, Unity automatically sets some parameters for 2D (such


as textures and sprites).
 The Scene View also has a toggle that switches the view between 2D
and 3D.

 When the 2D view is set, the depth coordinate is ignored, making 2D


game development much easier. The camera and the view are locked
in an orthographic view:
Orthographic Perspective
 Graphic objects in 2D are known as Sprites.
 Sprites are essentially just standard textures but there are
special techniques for combining and managing sprite
textures for efficiency and convenience during development.
 Unity provides a Sprite Editor for manipulating sprites. Of
special use is to edit an image to separate it into several
components. For instance, to keep the arms, legs and body of
a character as separate elements within one image.
 Sprites are rendered with a Sprite Renderer component rather
than the Mesh Renderer used with 3D objects.
 There are for controls at the top left corner to manipulate sprites in
the scene:
◦ Drag (Hand icon, shortcut Q): To move around the scene.
◦ Translate (Shortcut W): Changes position of a sprite.
◦ Rotate (Shortcut E): Rotates a sprite.
◦ Scale (Shortcut R): Scales a sprite.

 The last button (Shortcut T) allows to make all these changes on


Scene View as shown.
 When an image is imported as a sprite, the Texture Type must be
set to Sprite (2D and UI).

 The Sprite Mode can take two values:


◦ Single: The image is composed of 1 only element.
◦ Multiple: The image contains multiple elements in the same file.

 Pixels to Units denes the number of pixels from the sprite that will
correspond to 1 unit in world space.
 Pivot determines the position of the pivot in the sprite. The pivot is the point
in the image where the sprite's local coordinate system originates.
 It is possible to organize your game objects in layers. Layers are
used:
◦ by cameras to render only a part of the screen.
◦ by raycasting to selectively ignore colliders.
◦ by Lights to illuminate the scene.
◦ to determine collisions (Layer-based Collision).
◦ to determine the order in which sprites are rendered.
 To assign a game object to a layer, simply select the layer at the top
of the Inspector View, with the game object selected:
 Unity provides some layers by default, but you can create new
layers in Edit > Project Settings > Tags and Layers.
 It is possible to specify, for both camera and light
components, which layers will be affected by these (Culling
Mask selector). By default, all layers are rendered by the
camera and illuminated by the lights.
 Layer-based collision detection: It is also possible to specify which
game objects can collide with which by enabling/disabling collisions
between their layers.

 In the Physics 2D Manager, Edit > Project Settings > Physics2D.


 The Sprite Renderer component is in charge of drawing the sprite on
the screen.

◦ Sprite: The Sprite object to render.


◦ Color: Vertex color of the rendered mesh.
◦ Material: Material used to render the sprite.
◦ Sorting Layer: The layer used to dene this sprites overlay priority during rendering.
◦ Order in Layer: The overlay priority of this sprite within its layer. Lower numbers are
rendered first and subsequent numbers overlay those below.
 9-slicing is a 2D technique used to scale images individually in order to
prevent stretching when the image is displayed at different sizes and with
different aspect ratios.
 It involves splitting the image into nine portions, so that when sprite is re-
sized, the different portions stretch in different ways to keep the Sprite in
proportion.
 9-slice scaling allows scaling image without any quality loss.
When set to SpriteDrawMode.Sliced, the B, D,
E, F, H section will be stretched to fit the
dimension

When set to SpriteDrawMode.Tiled, the B, D,


E, F, H section will be repeated to fit the
dimension
https://docs.unity3d.com/Manual/9SliceSprites.html
 A dynamic medium in which images or objects are manipulated to
appear as moving objects.
 In Unity to animate a GameObject, all these items will be
automatically created, attached and set-up.
 To animate a GameObject, the object or objects need an Animator
Component attached.
 To create a new Animation Clip for the selected GameObject, make
sure the Animation Window is visible.
 The Texture Type should be set to Sprite.
 Set the Sprite Mode to Multiple, if image has several
elements.
 Using Sprite Editor, slice the spritesheet into individual
sprites.
 The Slice menu gives options for separating the elements of the
image.
◦ Automatic - the editor will attempt to guess the boundaries of sprite
elements by transparency and crop each sprite to a minimum size saving
space
◦ Grid by Cell Size - Requires that you know the size for each sprite.
The Pixel Size values determine the height and width of each sprite.
◦ Column & Row - determines the number of columns and rows used for
slicing.
 The Method menu lets you choose how to deal with existing
selections in the window.
◦ Delete existing - simply replaces whatever is already selected
◦ Smart - attempt to create new rectangles while retaining or adjusting
existing ones.
◦ Safe - adds new rectangles without changing anything already in place.
 Drag Sliced Sprites onto scene window, Unity will prompt to save the
animation.
 At the same time, Unity will create a Animator Controller and a ‘GameObject’
in the scene.
 The Animator Component provides a connection from your GameObject to
the AnimationController so that as things change in your game, the
AnimationController can adapt and provide those changes to the
GameObject.
 During this Exercise you will create a simple 2D game where you’ll be able to
put into practice: The Basics of 2D games, creating a scene, placing game
objects such as sprites, creating layers etc.
 Follow the steps to Set Up The Playing Field:
https://unity3d.com/learn/tutorials/s/2d-ufo-tutorial
 Your scene should look like this or you can also set up any scene of your
own choice.
 The 2D assets or sprites can be downloaded from unity store available free.
 Your scene should be arranged in layers.
 There should be an animation in the scene.
e.g. animating character, coins…

You might also like