You are on page 1of 26

Unity Cheat sheet

As of 4/20/2020

Bookmarks
Tutorials
Brackeys - https://www.youtube.com/user/Brackeys

 Unity tutorials to allow anyone - no matter their budget - to create top-quality games.

Unity Learn - https://unity.com/learn

 Develop your skills and your career with resources to learn at your own pace, or with Unity experts, at every
stage of learning.

Unity Manual - https://docs.unity3d.com/Manual/UnityManual.html

 Everything about Unity’s latest release

Free Assets
Unity Asset Store - https://assetstore.unity.com/

Open Game Art - https://opengameart.org/

Kenney assets - https://www.kenney.nl/assets

Why Unity?
1. Gaming industry is $9.6 Billion in investments as of 2019. Unity is used for popular games like Pokemon Go,
Hearthstone, Call of Duty Mobile
2. Popular movies are made with Unity like Lion King, Ready Player One, Jungle Book
3. Not just games but also applications software for cars, VR, and apps
4. Easy to use, HUGE friendly dev community with tutorials and answers to all sorts of questions
5. Build on multiple platforms like mobile to show off to friends, family, or interviewers
6. Free!

Overview
1. Starting a project
2. Navigating Unity UI
3. Creating a game
4. Building Project

Starting a Unity Project


1. Open Unity HUB
2. Click New
3. Change Project Name, Save Location, and choose a template (2D, 3D, etc.), then select Create
Navigating Unity UI
Tool bar

 Hand (Q or hold right click) – drag or pan the view of the scene
 The following affects the selected Game Object’s Transform Component
o Move (W) – move selected game object
o Rotate (E) – change rotation of selected game object
o Scale (R) – change the size selected Game Object (origin is center of Game Object)
o Rect (T) – change size (based on edges)
o Move, Rotate, or Scale (Y) – multi purpose (you can move or rotate selected game object)
 Play – Play current game scene. Clicking play while game scene is playing stops play mode
 Pause – Pause game scene that is playing
Creating a Game
Add Game objects to scene
Drag assets from Project tab to Scene.

You can change the color of sprites from the Inspector tab by clicking the Color then selecting the desired color on the
pop-up window (Click the X of the pop-up window to close it).

Parent and Child game objects


You can nest game objects with one another. From the hierarchy, you can drag one game object and drop it on another
game object to make the dragged and dropped game object a child of the other game object.

Think of nested game objects as combined as one so that transform actions (scale, move, rotate, etc.) on the parent
game object will affect the children. You can create a prefab (or blueprint) of this parent game object with children.

Add Scripts to Game objects


Scripts give you customized functionality to your game objects that can impact the game.

Create a script by right clicking on Project Tab under your desired scripts folder, then select Create, then select C# Script.
Select the Game Object in the Hierarchy Tab (or by clicking on the game object in the scene) to show the Game object’s
details on the Inspector Tab, then you can add this script to a Game Object in 2 ways.

1. On inspector tab, select Add Component, type the name of the script, then select the script, OR
2. drag the new C# Script under one of the other components of the selected Game object under the Inspector tab

Edit Scripts
Double Click on a script to add code into it so that any Game Object with the attached script has its functionality.

See a list of useful scripts below at Useful Code/Scripts (Alphabetical)

Create Prefabs
Drag a Game Object from the Hierarchy Tab into Project Tab (preferably Prefab folder) to create a prefab.

Now you can create clones of this new Game Object just by dragging it to the scene.

The prefab editor allows you to make changes later on a prefab and the changes will apply to every instance of the
prefab.

Add Audio
You can add background music and sound effects to your game.

Add BGM (Background Music)


Select a Game Object like Main Camera from the hierarchy and drag Music file from Project tab to the Inspector tab, to
add an Audio Source component. Make sure that Loop and Play On Awake check boxes are checked in Inspector tab.

ADD SFX (Sound Effects)


Select a Game Object that should play the sound effect from the hierarchy and drag Sound file from Project tab to the
Inspector tab, to add an Audio Source component. Make sure that Loop and Play On Awake check boxes are unchecked
in Inspector tab.

Add a script to play the sound.

public AudioSource sound; // On the editor, drag the game object with the sound source into this
public variable

// Start is called before the first frame update


void Start()
{
sound = GetComponent<AudioSource>();
}

public void PlaySound()


{
sound.Play();
}

Add UI
UI elements like Images, Buttons, text, and various interactable elements can be added to the game, they will be under a
Canvas Game Object.

Create UI
To create a UI element, right click on Hierarchy tab, select UI, then select desired UI to create – Text, Button, Image, etc.
If a Canvas Game object already exists, the new UI element will be created as a child of the canvas, otherwise a new
Canvas game object will automatically be created.

Position UI
It is best practice to use anchors to position UI elements to cater to different display resolutions

1. Select the UI element


2. Click on the box with the cross:

a.
3. then hold Alt + Shift and select with Mouse 1 desired location of UI

Useful UI elements
The following are useful UI elements you can create and use:

Text
You can use a Text UI to create a title.

1. Right click on Hierarchy, select UI, then Text. You can change the parameters (font, color, size, alignment, etc.) of
this new text game object in its Text (Script) component via the Inspector.
a. NOTE: If the font is too big, the text will disappear. Adjust the size of the text box using the Rect tool
(shortcut T) on the Scene view
2. Position the text UI using the anchors (see above).

Buttons
You can create UI buttons for different things like Start or Exit Game button

1. Right click on Hierarchy, select UI, then Button.


2. Add a script to any GameObject in the Scene
3. Under the Button (Script) component, there is an On Click () section that starts out as List is Empty. Click the +
sign to add effects of button click.
4. Drag the GameObject with a script attached to it to the newly created input field which shows None (Object)

a.
5. Click on No Function, then select the script name on the list, then select the function you want called when the
button is clicked.

Building a Project
To build your project:

1. Select File on menu tab, then Build Settings.


2. Click Add Open Scene button to add current scene or drag scene from Project tab to Scenes In Build.
3. Choose the Platform you want to build – PC, Android, or WebGL (web browser). If you change platform to build
then you need to click Switch Platform.
4. The 1st scene on the start of the game (e.g. Title screen or intro) should be on top of the list of Scenes in Build.
5. When all of your desired scenes are under Scenes In Build, Click Build and Run.

Useful Code/Scripts (Alphabetical)


Debug.log
To display debug messages on console
Debug.Log("Debug message here");

Destroy
To destroy game object, Add the following script to the Game Object
Destroy(gameObject, 0.1f); // Destroy game object after 0.1 time

Events
Events are specialized delegates useful to alert other classes that something has happened. They work similarly to public
multiclass delegates.

Delegate Event
Use this to create delegate events

Tutorial link - https://www.gamasutra.com/blogs/VivekTank/20180703/321126/How_to_use_C_Events_in_Unity.php

1. Create an Event Manager

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EventManager : MonoBehaviour


{
public delegate void onMyDelegate(); //Declare a Delegate
public static event onMyDelegate onMyEvent; //Create an Event

public static void EventFunction()


{
if (onMyEvent != null)
{
onMyEvent(); //Invoke an Event
}
}

// The following can be on a separate script (you can call it EventHandler.cs)


// Note that different scripts can define different conditions for an event to trigger
void Update()
{
if (Input.GetKeyDown(KeyCode.P))
{
EventManager.EventFunction(); // Function call to invoke an Event
}

}
}

2. Create a script with functions that subscribes and unsubscribes to an event

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScoreEvent : MonoBehaviour


{
// Function Subscribes to an event
private void OnEnable()
{
EventManager.onMyEvent += ScoreFunction;
}

// Function Unsubscribes to an event


private void OnDisable()
{
EventManager.onMyEvent -= ScoreFunction;
}

// Function definition that will subscribe/unsubscribe to event


public void ScoreFunction()
{
Debug.Log("Score");
}
}

Score system using Event


The following is an example application of Unity Events on a score system

1. Create an Event Manager

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EventManager : MonoBehaviour


{
public delegate void onMyDelegate(); //Declare a Delegate
public static event onMyDelegate onMyEvent; //Create an Event

public static void EventFunction()


{
if (onMyEvent != null)
{
onMyEvent(); //Invoke an Event
}
}
}

2. Create an Event Handler


a. This is what triggers the event. In this example, the conditions are – object with the script is touched or
mouse 1 clicked
b. IMPORTANT: There should only be one event handler

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// This is the Event Handler


public class ScoreEvent : MonoBehaviour
{
void Update()
{
if ((Input.touchCount > 0) || Input.GetMouseButtonDown(0))
{
Vector3 wp = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if (GetComponent<Collider2D>().OverlapPoint(wp))
{
// Event handler has the condition for event above and the following line
EventManager.EventFunction();

Destroy(gameObject, 0.0f); // Destroy game object after 0.1 time


}
}
}
}

3. Create scripts that subscribe to the event and also triggers when the event occurs
a. In this example add score function is called when the event occurs.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ScoreEventHandler : MonoBehaviour


{
public Text ScoreText;
public int ScoreVariable = 0;

public AudioSource sound; // On the editor, drag the game object with the sound source into this
public variable

// Start is called before the first frame update


void Start()
{
ScoreText.text = ScoreVariable.ToString();

sound = GetComponent<AudioSource>();
}

public void AddScore()


{
sound.Play();

ScoreVariable = ScoreVariable + 10;


ScoreText.text = ScoreVariable.ToString();
}

// Function Subscribes to an event


private void OnEnable()
{
EventManager.onMyEvent += AddScore;
}

// Function Unsubscribes to an event


private void OnDisable()
{
EventManager.onMyEvent -= AddScore;
}

Instantiate
Use the following script to create a Game Object during run time.

public Transform prefab;

Instantiate (prefab, transform.position, transform.rotation);

Instantiate objects in map


Creating objects from prefabs

// Instantiates 10 copies of prefab each 2 units apart from each other

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour


{
public Transform prefab;
void Start()
{
for (int i = 0; i < 10; i++)
{
Instantiate(prefab, new Vector3(i * 2.0F, 0, 0), Quaternion.identity);
}
}
}

Instantiate projectiles
Shooting projectiles using script

Syntax:
public static Object Instantiate(Object original);
public static Object Instantiate(Object original, Transform parent);
public static Object Instantiate(Object original, Transform parent, bool instantiateInWorldSpace);

public static Object Instantiate(Object original, Vector3 position, Quaternion rotation);


public static Object Instantiate(Object original, Vector3 position, Quaternion rotation, Transform parent);

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {


public Rigidbody projectile;
void Update() {
if (Input.GetButtonDown("Fire1")) {
Rigidbody clone;
clone = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
clone.velocity = transform.TransformDirection(Vector3.forward * 10);
}
}
}

Scene Change
Tags: change map change Level Change Scene load scene load level

IMPORTANT NOTE: The next scene should be added on Bu

Use script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine;

public class sceneChanger : MonoBehaviour {

public string nextScene = "nextSceneName";

public void changeScene()


{
SceneManager.LoadScene(nextScene);
}
}

Touch
Touch controls script

Add the following script to a Game Object with a 2D Collider component:


void Update()
{
if ((Input.touchCount > 0)|| Input.GetMouseButtonDown(0))
{
Vector3 wp = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if (GetComponent<Collider2D>().OverlapPoint(wp))
{
Destroy(gameObject, 0.1f); // Destroy game object after 0.1 time
}
}
}

Terms
Editor – this is the Unity interface where you can create your game

Game Object – any object in the scene that has a transform and can have components

Hierarchy – this is the list of game objects that is in the game scene. Selecting a game object in the hierarchy will display
its components on the Inspector tab

Prefab – it is a blueprint of a Game Object to make copies of game objects easier.

Scene – a game scene is where Game Objects are placed

UI – User Interface
Unity Cheat sheet

As of 2/9/2020

Bookmarks
Tutorials
Brackeys - https://www.youtube.com/user/Brackeys

· Unity tutorials to allow anyone - no matter their budget - to create top-quality games.

Unity Learn - https://unity.com/learn

· Develop your skills and your career with resources to learn at your own pace, or with Unity experts, at
every stage of learning.

Unity Manual - https://docs.unity3d.com/Manual/UnityManual.html

· Everything about Unity’s latest release

Free Assets
Unity Asset Store - https://assetstore.unity.com/

Open Game Art - https://opengameart.org/

Kenney assets - https://www.kenney.nl/assets

Why Unity?
1. Gaming industry is $9.6 Billion in investments as of 2019. Unity is used for popular games like Pokemon
Go, Hearthstone, Call of Duty Mobile

2. Popular movies are made with Unity like Lion King, Ready Player One, Jungle Book

3. Not just games but also applications software for cars, VR, and apps

4. Easy to use, HUGE friendly dev community with tutorials and answers to all sorts of questions

5. Build on multiple platforms like mobile to show off to friends, family, or interviewers

6. Free!

Overview
1. Starting a project

2. Navigating Unity UI

3. Creating a game

4. Building Project

Starting a Unity Project


1. Open Unity HUB

2. Click New

3. Change Project Name, Save Location, and choose a template (2D, 3D, etc.), then select Create

Navigating Unity UI
Tool bar

· Hand (Q or hold right click) – drag or pan the view of the scene

· The following affects the selected Game Object’s Transform Component

o Move (W) – move selected game object

o Rotate (E) – change rotation of selected game object

o Scale (R) – change the size selected Game Object (origin is center of Game Object)

o Rect (T) – change size (based on edges)

o Move, Rotate, or Scale (Y) – multi purpose (you can move or rotate selected game object)

· Play – Play current game scene. Clicking play while game scene is playing stops play mode

· Pause – Pause game scene that is playing

Creating a Game
Add Game objects to scene
Drag assets from Project tab to Scene.

You can change the color of sprites from the Inspector tab by clicking the Color then selecting the desired color
on the pop-up window (Click the X of the pop-up window to close it).
Parent and Child game objects

You can nest game objects with one another. From the hierarchy, you can drag one game object and drop it on
another game object to make the dragged and dropped game object a child of the other game object.

Think of nested game objects as combined as one so that transform actions (scale, move, rotate, etc.) on the
parent game object will affect the children. You can create a prefab (or blueprint) of this parent game object
with children.

Add Scripts to Game objects


Scripts give you customized functionality to your game objects that can impact the game.

Create a script by right clicking on Project Tab under your desired scripts folder, then select Create, then select
C# Script. Select the Game Object in the Hierarchy Tab (or by clicking on the game object in the scene) to
show the Game object’s details on the Inspector Tab, then you can add this script to a Game Object in 2 ways.

1. On inspector tab, select Add Component, type the name of the script, then select the script, OR

2. drag the new C# Script under one of the other components of the selected Game object under the
Inspector tab

Edit Scripts
Double Click on a script to add code into it so that any Game Object with the attached script has its
functionality.

See a list of useful scripts below at Useful Code/Scripts (Alphabetical)

Create Prefabs
Drag a Game Object from the Hierarchy Tab into Project Tab (preferably Prefab folder) to create a prefab.

Now you can create clones of this new Game Object just by dragging it to the scene.

The prefab editor allows you to make changes later on a prefab and the changes will apply to every instance of
the prefab.

Add Audio
You can add background music and sound effects to your game.

Add BGM (Background Music)

Select a Game Object like Main Camera from the hierarchy and drag Music file from Project tab to the
Inspector tab, to add an Audio Source component. Make sure that Loop and Play On Awake check boxes are
checked in Inspector tab.
ADD SFX (Sound Effects)

Select a Game Object that should play the sound effect from the hierarchy and drag Sound file from Project tab
to the Inspector tab, to add an Audio Source component. Make sure that Loop and Play On Awake check
boxes are unchecked in Inspector tab.

Add a script to play the sound.

public AudioSource sound; // On the editor, drag the game object with the sound source into this public variable

// Start is called before the first frame update

void Start()

sound = GetComponent<AudioSource>();

public void PlaySound()

sound.Play();

Add UI
UI elements like Images, Buttons, text, and various interactable elements can be added to the game, they will
be under a Canvas Game Object.

Create UI

To create a UI element, right click on Hierarchy tab, select UI, then select desired UI to create – Text, Button,
Image, etc. If a Canvas Game object already exists, the new UI element will be created as a child of the
canvas, otherwise a new Canvas game object will automatically be created.

Position UI

It is best practice to use anchors to position UI elements to cater to different display resolutions

1. Select the UI element

2. Click on the box with the cross:


a.

3. then hold Alt + Shift and select with Mouse 1 desired location of UI

Useful UI elements

The following are useful UI elements you can create and use:

Text

You can use a Text UI to create a title.

1. Right click on Hierarchy, select UI, then Text. You can change the parameters (font, color, size, alignment,
etc.) of this new text game object in its Text (Script) component via the Inspector.

a. NOTE: If the font is too big, the text will disappear. Adjust the size of the text box using the
Rect tool (shortcut T) on the Scene view

2. Position the text UI using the anchors (see above).

Buttons

You can create UI buttons for different things like Start or Exit Game button

1. Right click on Hierarchy, select UI, then Button.

2. Add a script to any GameObject in the Scene

3. Under the Button (Script) component, there is an On Click () section that starts out as List is Empty. Click
the + sign to add effects of button click.

4. Drag the GameObject with a script attached to it to the newly created input field which shows None
(Object)

a.

5. Click on No Function, then select the script name on the list, then select the function you want called when
the button is clicked.

Building a Project
To build your project:

1. Select File on menu tab, then Build Settings.

2. Click Add Open Scene button to add current scene or drag scene from Project tab to Scenes In Build.

3. Choose the Platform you want to build – PC, Android, or WebGL (web browser). If you change platform to
build then you need to click Switch Platform.
4. The 1st scene on the start of the game (e.g. Title screen or intro) should be on top of the list of Scenes in
Build.

5. When all of your desired scenes are under Scenes In Build, Click Build and Run.

Useful Code/Scripts (Alphabetical)


Debug.log
To display debug messages on console

Debug.Log("Debug message here");

Destroy
To destroy game object, Add the following script to the Game Object

Destroy(gameObject, 0.1f); // Destroy game object after 0.1 time

Events
Events are specialized delegates useful to alert other classes that something has happened. They work
similarly to public multiclass delegates.

Delegate Event

Use this to create delegate events

Tutorial link -
https://www.gamasutra.com/blogs/VivekTank/20180703/321126/How_to_use_C_Events_in_Unity.php

1. Create an Event Manager

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class EventManager : MonoBehaviour

{
public delegate void onMyDelegate(); //Declare a Delegate

public static event onMyDelegate onMyEvent; //Create an Event

public static void EventFunction()

if (onMyEvent != null)

onMyEvent(); //Invoke an Event

// The following can be on a separate script (you can call it EventHandler.cs)

// Note that different scripts can define different conditions for an event to trigger

void Update()

if (Input.GetKeyDown(KeyCode.P))

EventManager.EventFunction(); // Function call to invoke an Event

2. Create a script with functions that subscribes and unsubscribes to an event

using System.Collections;

using System.Collections.Generic;

using UnityEngine;
public class ScoreEvent : MonoBehaviour

// Function Subscribes to an event

private void OnEnable()

EventManager.onMyEvent += ScoreFunction;

// Function Unsubscribes to an event

private void OnDisable()

EventManager.onMyEvent -= ScoreFunction;

// Function definition that will subscribe/unsubscribe to event

public void ScoreFunction()

Debug.Log("Score");

Score system using Event

The following is an example application of Unity Events on a score system

1. Create an Event Manager

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class EventManager : MonoBehaviour


{

public delegate void onMyDelegate(); //Declare a Delegate

public static event onMyDelegate onMyEvent; //Create an Event

public static void EventFunction()

if (onMyEvent != null)

onMyEvent(); //Invoke an Event

2. Create an Event Handler

a. This is what triggers the event. In this example, the conditions are – object with the script is
touched or mouse 1 clicked

b. IMPORTANT: There should only be one event handler

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

// This is the Event Handler

public class ScoreEvent : MonoBehaviour

void Update()

if ((Input.touchCount > 0) || Input.GetMouseButtonDown(0))

Vector3 wp = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if (GetComponent<Collider2D>().OverlapPoint(wp))

// Event handler has the condition for event above and the following line

EventManager.EventFunction();

Destroy(gameObject, 0.0f); // Destroy game object after 0.1 time

3. Create scripts that subscribe to the event and also triggers when the event occurs

a. In this example add score function is called when the event occurs.

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.UI;

public class ScoreEventHandler : MonoBehaviour

public Text ScoreText;

public int ScoreVariable = 0;

public AudioSource sound; // On the editor, drag the game object with the sound source into this public variable

// Start is called before the first frame update

void Start()

ScoreText.text = ScoreVariable.ToString();
sound = GetComponent<AudioSource>();

public void AddScore()

sound.Play();

ScoreVariable = ScoreVariable + 10;

ScoreText.text = ScoreVariable.ToString();

// Function Subscribes to an event

private void OnEnable()

EventManager.onMyEvent += AddScore;

// Function Unsubscribes to an event

private void OnDisable()

EventManager.onMyEvent -= AddScore;

Instantiate
Use the following script to create a Game Object during run time.
public Transform prefab;

Instantiate (prefab, transform.position, transform.rotation);

Instantiate objects in map

Creating objects from prefabs

// Instantiates 10 copies of prefab each 2 units apart from each other

using UnityEngine;

using System.Collections;

public class ExampleClass : MonoBehaviour

public Transform prefab;

void Start()

for (int i = 0; i < 10; i++)

Instantiate(prefab, new Vector3(i * 2.0F, 0, 0), Quaternion.identity);

}
}

Instantiate projectiles

Shooting projectiles using script

Syntax:

public static Object Instantiate(Object original);

public static Object Instantiate(Object original, Transform parent);

public static Object Instantiate(Object original, Transform parent, bool instantiateInWorldSpace);

public static Object Instantiate(Object original, Vector3 position, Quaternion rotation);

public static Object Instantiate(Object original, Vector3 position, Quaternion rotation, Transform parent);

using UnityEngine;

using System.Collections;

public class ExampleClass : MonoBehaviour {

public Rigidbody projectile;

void Update() {

if (Input.GetButtonDown("Fire1")) {

Rigidbody clone;

clone = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;

clone.velocity = transform.TransformDirection(Vector3.forward * 10);


}

Scene Change
Tags: change map change Level Change Scene load scene load level

IMPORTANT NOTE: The next scene should be added on Bu

Use script:

using System.Collections;

using System.Collections.Generic;

using UnityEngine.SceneManagement;

using UnityEngine;

public class sceneChanger : MonoBehaviour {

public string nextScene = "nextSceneName";

public void changeScene()

SceneManager.LoadScene(nextScene);

Touch
Touch controls script

Add the following script to a Game Object with a 2D Collider component:

void Update()
{

if ((Input.touchCount > 0)|| Input.GetMouseButtonDown(0))

Vector3 wp = Camera.main.ScreenToWorldPoint(Input.mousePosition);

if (GetComponent<Collider2D>().OverlapPoint(wp))

Destroy(gameObject, 0.1f); // Destroy game object after 0.1 time

Terms
Editor – this is the Unity interface where you can create your game

Game Object – any object in the scene that has a transform and can have components

Hierarchy – this is the list of game objects that is in the game scene. Selecting a game object in the hierarchy
will display its components on the Inspector tab

Prefab – it is a blueprint of a Game Object to make copies of game objects easier.

Scene – a game scene is where Game Objects are placed

UI – User Interface

You might also like