These notes are from the Course 1 - Unit 8 Project from Introduction to Unity for 2D Video Games. They should help to re-create the project from scratch as they highlight the main points of the lesson.
Is a good practice to stablish your own folder structure and try to keep it as stantarized as possible for your different projects and types of games.
It's recommended to set this structure as soon as your project is started. Here's a basic custom structure for a 2D Game:
- Assets
- Animation
- Audio
- Materials
- Prefabs
- Scenes
- Scripts
- Sprites
In a project the camera size is how much of the scene the camera covers, in this first project the camera size was set to 0.5 (default was 5). This will make it so that the sprites look bigger in the screen. (TODO: enhance wording)
When importing a sprite assets, if you want them to look pixel perfect it is recommented:
- Select your sprite asset in your project window.
- The inspector will show the import settings of the asset (note that this is the asset settings and not settings for an specific game object).
- Go to the inspector and set Filter Mode to
Point (no filter)
and Compression toNone
Let's say you want to add a script asset to a sprite that's located on your scene. For this, you should:
-
Select your sprite object by clicking on it on the Hierarchy Window. This will select the sprite on the scene and show its properties on the Inspector Window.
-
Select the script asset from the Project Window and drag/drop the script into it on the inspector (which should be showing the properties of the sprite game object).
-
Any public variable that is defined on the script will appear on the inspector in order to be set at design time in case this is desired.
Here's the script LoadScene.cs
that was included on lesson Course 1 - Unit 8:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class LoadScene : MonoBehaviour
{
public string scene;
private void OnMouseDown()
{
SceneManager.LoadScene(scene);
}
}
Here the public variable scene
is used on the call to LoadScene()
in order to load a given scene by name. The scene variable will be accesible from the inspector. You can then specify the scene you want to load by writing the name on the inspector field.
Notice the call to LoadScene()
is located on a private method called OnMouseDown()
, this method should be called when the user clicks on the sprite. However in order for this to happen, the sprite object must be associated to a Box Collider
, which we'll talk about in the next section.
In order to capture the OnMouseDown()
event when the user clicks on a sprite, you need to add a Box Collider. The Box Collider defines the boundaries inside de sprite image that will trigger a collission event. It is square shaped and you can change the boundaries by changing this shape. (TODO: enchance this explanation after learning more about colliders).
Let's add a Box Collider to the sprite object:
- First make sure you have the sprite object you want to interact with selected on the
Hierarchy Window
. - On the inpector there's a button at the bottom that says
Load Component
; click on it. - Now, select the
Physics 2D
section of the component selection window, then select Box Collider.
- Go to File -> Build Settings, and then add your scenes if you use more than one.
- Once inside Build Settings, make sure you also click on Player Settings and set the appropiate information for the game and each platform.
- Go to Windows -> Package Manager, make sure you have "In Proyect" selected, then upgrade packages that can be upgraded, remove any package you don't really need.