Videogames Development – Unity – 2D Basics – How to create a Scene
Unity is a fully integrated development engine that provides rich out-of-the-box functionality to create games and other interactive 3D and 2D content.
NEW PROJECT
1. MAIN TOP MENU> File> New Project> Setup defaults for: 2D> ‘Create’, Unity will restart in 2D mode.
or
1. MAIN TOP MENU> Edit> Project Settings> Editor
2. Inspector> Editor Settings> Default Behavior Mode> 2D (behaviour = comportamento)
NOTICE: the new project has been saved in C:\Users\a\Documents\New Unity Project
VIEWPORT
MAIN TOP MENU> Window> Layouts> Default
Scene> SCENE TOOLBAR> activare/deactivate ‘2D’ button
CAMERA (GAME AREA)
In the viewport click over the Camera icon> Inspector> Camera> Viewport Rect: X=0 Y=0 to center the game area, W=16 H=9 to setup 16:9 aspect ratio
GAME PREVIEW
Scene> SCENE TOOLBAR> ‘Game’ label> ‘Free Aspect’ rollout> 16:9
Now camera view and game preview have same aspect ratio.
Others Setups:
Free Aspect
5:4
4:3
3:2
16:10
16:9
Standalone (1024×768) setup this:
Edit> Project Settings> Player> Inspector> Settings for PC, MAc LinuX Standalone
Uncheck ‘Default is Full Screen’ and setup ‘Default Screen Width’ and ‘Default Screen Height’
LAYERS
You can use layers to organize your project.
1. TOP RIGHT> ‘Layers’ drop down menu> Edit Layers…> Sorting Layers> ‘+’ button> name the layer ‘Main-Character’
2. TOP RIGHT> ‘Layers’ drop down menu> Sorting Layers> ‘+’ button> name the layer ‘Background’
3. DRAG AND DROP a layer in the stack to change layer order, the top layer in the stack is the foreground, the bottom layer is the background.
It is the opposite of Photoshop layer stack
IMAGES IMPORT
1a. MAIN TOP MENU> Assets> Import New Asset> piggy.png
Assets are multimedia files imported to make a videogame. They can be images, music, 3d objects etc…
1b. Copy from Windows the multimedia files inside ‘Asset’ Folder; to see the ‘Asset’ folder location over Asset folder RMB> Show in Explorer
You can see all Assets in the project in the window ‘Project’> ‘Assets’ folder
It is a good way organize Assets in different folders: Sound – Textures – 3D Models – Scripts etc…
Here you can search with name, or using small icons you can select to show/hide:
– AnimationClip
– AudioClip
– Font
– GUISkin
– Material
– Mesh
– Model
– PhysicalMaterial
– Prefab
– Scene
– Script
– Shader
– Texture
‘Assets Window’> bottom slider to resize
2. Hierarchy> select ‘piggy’> Inspector> Sprite Renderer> Sorting Layer> Main-Character
Now ‘piggy’ is inside the layer ‘Main-Character’
3. TOP RIGHT> ‘Layers’ drop down menu> Sorting Layers> use the ‘padlock’ icon on the left to lock/unlock the layer to prevent accidental selections in viewport.
TAGS
It is useful assign tag to an object, it will be easier to find an object or call it with scripts.
Example: we can tag all enemy with an ‘Enemy’ tag, even they have different size, mesh or skills.
Inspector> Tag
In a script you can find object with a particular tag, see the code:
var enemy = GameObject.FindWithTag ("Your Tag Name");
IMAGES IN GAME
1. Project> Assets> drag and drop the pig iside the viewport
2. Inspector> Sprite Renderer, setup ‘Sorting Layer’ (the layer where the sprite is stored), ‘Order in layer’ (the order in the layer, example: 7 is over 8 and 8 is over 9 etc…)
3. Inspector> Transform> setup Position, Rotation, Scale or use the handles in the viewport
RESET VALUES
Inspector> Transform> Little Gear Icon> Reset
LOCK INSPECTOR
Inspector> on the right the lock icon> the current object will not change even you select another object
GAME OBJECTS and COMPONENTS
Inside Unity every object in the scene is a GameObject, a GameObject is the sum of all Components (the stack of scripts, physic, modifiers you can see in the Inspector window).
Add Components: Select an Object> Inspector> ‘Add Component’ button or Select an Object>MAIN TOP MENU> Component
Modify Components: Select an Object> Inspector> in the Component Slot click in the small gear icon to Reset / Remove / Move Up-Down / Copy
PREFAB – Prefabricated Objects
NOTICE: Only Prefab can be cloned and instatiated
Create a Prefab:
1. If you do not have a prefab folder create it Project> Create> Folder> give it the name ‘Prefabs’
2. Hierarchy> DRAG AND DROP the GameObject inside ‘Prefabs’ folder in Projects> Assets> Prefabs
3. Hierarchy> Delete the old Gameobject from the scene
4. Project> Assets> Prefab> DRAG AND DROP the new Prefab in the scene
Instantiate a Prefab:
1. Project> Assets> Prefab> DRAG AND DROP the new Prefab in the scene
2. Project> Assets> Prefab> DRAG AND DROP another Prefab in the scene
3a. Project> Assets> Prefab> CLICK the original Prefab and from Inspector> change Scale value> all instanced Prefabs in the scene will change
OR
3b. Select in the viewport a Prefab> Inspector> change scale value> Inspector ‘Apply’, to apply the change to all Prefabs in the scene.
Break Prefab Chain:
Select in the viewport a Prefab> MAIN TOP MENU> GameObject> Break Prefab Instance> now the Prefab is property independent
BACKGROUND
You can use the same techniques to load a background image, and store it in ‘Background’ layer
SAVE THE SCENE
MAIN TOP MENU> Save Scene As…> type a name for your Scene
NOTICE:
Unity will put the Scene inside Project> Assets
If you do not save the Scene your composition work will be lost.
SAVE THE PROJECT
MAIN TOP MENU> File> Save project
NOTICE:
Unity Project in the main container, it contents all Assets (Scenes, images, sfx, code etc…)