Skip to content

Instantly share code, notes, and snippets.

@unity-msoto
Last active May 8, 2023 22:27
Show Gist options
  • Save unity-msoto/4b1208ef2f9fff4c529d3ff6c4949e94 to your computer and use it in GitHub Desktop.
Save unity-msoto/4b1208ef2f9fff4c529d3ff6c4949e94 to your computer and use it in GitHub Desktop.
Unity Learning Notes

Unity Learning Notes - Course 1

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.

Custom Project Folder Structure

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

Camera Size

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)

Perfect Pixel Sprites

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 to None

Adding a script to a game object.

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.

Adding the Box Collider

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.

Remember...

When building your project for the first time:

  • 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.

Unity Learning Notes - Course 2

Understanding Game Objects

Objects inside an scene are called game objects, and they can contain several components. The components you add to your game object will define how it will work and what can you do with it.

When you drag an sprite image from the project window to the scene window, Unity will create a game object and add several components into it. Those are:

  • A Transform component
  • A Sprite Rendering component
  • A Sprites-Default (Material) component

To understand this you can manually add a sprite doing the following:

  • Go to the menu option GameObject -> Create Empty
    • You will see a new item in your hierarchy window called GameObject.
    • If you look at the inspector you will see this object contains only a Transform component.
    • You will also see the object name and can change it in the inspector (or on the hierarchy windows).
  • To add an sprite to your game object click on the Add Component button on the inspector and select Rendering -> Sprite Renderer. This will also add the Sprites-Default (Material) component.
  • Expand your sprite renderer component on the inspector and you'll see the Sprite property set to none.
  • You can select one by clicking on the picker button on the property, but you can also drag and drop a sprite from your sprite assets on your project window.
  • You now have the same game object composition that Unity does automatically for you when you drag and drop an sprite image into your scene.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment