Day 2 | An Important Video + Sprites + Pixel Art

July 8, 2022

I fell asleep last night watching that video. Go to the “Help” menu and find the Unity Functions Library (what he’s calling the “API” or the “scripting reference”) by searching for “scripting reference.” Here’s a link: https://docs.unity3d.com/ScriptReference/index.html

Timestamp 5:14 — How does scripting work? Scripting is all about logic, not math. It’s all logic to drive code that’s prebuilt into Unity. That code that’s prebuilt into Unity is the Unity Game Engine, and it’s accessed through something that’s called the API (or the application programming interface). You can find the API in the scripting reference. Scripts are written in C#, a special language that Unity can understand. This is how we give Unity our instructions.

Timestamp 17:42 — He says that “this is what drives the Unity game engine.” So very important stuff. Basically these are all of the pre-made variables, functions, and classes that can be simply called into existence without the headache of really understanding how those things were coded. This makes things quick, but I can imagine, frustrating at times not knowing how things are working under the hood.

If there’s something that you want to learn more about, like a function that you’ve called into existence in your script for example, you can highlight it and then push CMD + ‘ on Mac or CTRL + ‘ on Windows to immediately search all about what it can do.

Timestamp 21:00 - The difference between “public” and “private” variables. “Private” variables can only be accessed within this particular script within this particular class—basically meaning that no one can change it other than you inside the script. If a variable is set to “Public” it can be changed by a user in the game.

Timestamp 22:04 - What are types? For instance “public Light myLight;” if written in a script, has three parts:

  1. The signifier telling if the variable is public or private.

  2. The type of variable that it is.

  3. The name of the variable that you give it.

The type is important because it tells C# how to look at the variable. It tells it properties it should look for. For instance, if the variable is an int (integer) then it knows that it’s looking at a number. If the type is a Light, however, C# knows that it can’t do certain things like addition or subtraction to the Light because it’s an object.

NOTE: You can’t start a variable name with a number. Always start your variables with a lower case letter. Functions, on the other hand, start with an uppercase letter.

Timestamp 25:28 - What are functions? Scripts use functions to DO THINGS to variables. These are the functions that run automatically in Unity:

  1. Awake()

  2. Start()

  3. Update()

  4. FixedUpdate()

  5. LateUpdate()

Awake() — This is called in the very first frame of the game. If a game object is inactive, then Awake() will not be called until you wake it up.

Start() — Kind of the same as Awake()?

Update() — Called once per frame.

FixedUpdate() — This is called when you want to do physics work. This is a way to manage how much is updated so that your computer doesn’t blow up trying to update literally everything ever frame. The FixedUpdate() is done at a regular pace. It is called by the physics engine at a regular time, not based on the speed of rendering, but on a specific time tick that can be set.

LateUpdate() — Very similar to Update(). This is called at the end of the frame instead of the beginning. This is good for things like cameras. Let’s say that we have a character and is moved by another character by being bumped into. If we moved our camera at the same time that we moved our character, then the camera wouldn’t be in the right position at the end of the frame. You would, instead, need to have the camera in LateUpdate() so that it follows the action perfectly. This is a second loop to have. You can find more about this in the “Execution Order” in Unity Documentation.

Timestamp 30:57 — How to write a function. There are two ways to call functions:

  1. We can call Unity’s functions from their functions library, also called an API (like how JavaScript uses the jQuery functions library or API),

  2. or we can create our own functions and call them into existence within our script.

When we write a functions, functions start with a return type at the beginning of the function.

Example:

void MyFunction() {

}

In this example, this is our own function that we created ourselves. So instead of a pre-made Unity function like Update() which is called into existence automatically, we have to call our function MyFunction() into existence within our script after we write the code for it. It’s different in that regard. We either call it from another script or from within another script that is running from one of the functions that Unity calls. So it needs to be called from Awake() or Update() or it can be called from another script, for example like if a button is pressed and that script is called to enact that function.

Functions can do calculations and then return a value instead of just returning “void” like in the above example. This is what the parenthesis are for.

Example:

void Awake () {

AddTwo(9, 2);     <— 9 and 2 are called “Arguments.”

Debug.Log(myVar);  <—Sends the answer to the log.

}

int AddTwo(int var1, int var2) {     <—called “parameters”

int returnValue = var1 + var2;

return returnValue;

}

Timestamp 38:28 — What is a Class? A Class is a collection of variables and functions.

NOTE: The class name must match the file name of your C# script for it to work.

To be attached to a script, it has to derive from another class called “:MonoBehaviour” which is automatically put there when you write your script.

Timestamp 40:26 — You have to serialize your custom Classes. If you create a custom class that you want to hold a little data in, you have to ask it to “serialize” it. This means that it will be converted into simple data that Unity can look at in the inspector. Only then will your custom class appear in the Unity Inspector.

Example:

[System.Serializable] <—Allows class to appear in Inspector

public class DataClass {

public int myInt;

public float myFloat;

}

I need to look more into this concept later.

Timstamps 41:52 — Q&A

——————

Links to more videos about C# coding in Unity. I haven’t watched any of these to see if they’re useful yet, but I hit a treasure trove in the search engine today Thank God:

  1. “Unity Tutorial for Beginners - C# Coding” https://www.youtube.com/watch?v=7K2SMZQRKnw

  2. “HOW TO ACCESS DATA FROM ANOTHER SCRIPT 🎮 | Get Data From Other Scripts In Unity | Unity Tutorial”

https://www.youtube.com/watch?v=Y7pp2gzCzUI

  1. “Connecting Unity Clients to a Dedicated Server | C# Networking Tutorial - Part 1”

https://www.youtube.com/watch?v=uh8XaC0Y5MA

  1. “How to Create a 2D Card Game in Unity - Part 1 (Setup and Basic Game Architecture)”: https://youtu.be/0-dUB52eEMk

From Kevin Kaymak’s channel: https://www.youtube.com/user/AndroidRetro/featured

  1. “[Unity]Custom Unity Networking 2k19” https://www.youtube.com/watch?v=HjF8Umwd5HE

  2. “2020 Unity Networking Solution: INITIALIZING NETWORK / SENDING DATA“ https://www.youtube.com/watch?v=vOBSAFmL610

  3. “[C#]2D MMORPG Tutorial - EP1: DRAW GRAPHICS” https://youtu.be/7En-NOXwPQw

*Brackeys channel: https://www.youtube.com/c/Brackeys

———————

For more advanced programmers but new to the Unity Game Engine, Unity links to this page: https://unity.com/how-to/programming-unity

Sprites

I’m trying to figure out how to work with sprites in Unity. I’m pretty sure that’s what allows for 2D animation and different motions for characters in a game. It turns out that there’s a sprite editor in Unity according to this link: https://learn.unity.com/tutorial/introduction-to-sprite-editor-and-sheets#5ffbc6d4edbc2a0db6cc3184

It’s saying that you can import one big file of different pictures that you’re wanting to be animated, instead of having a bunch of individual pictures imported on different files. Good to know, but I’m still not sure how to have that setup in photoshop to create the sprite file. What size or format does it need to be?

I found a YouTube series that will help:

  1. “HOW TO DRAW PIXEL ART GAME CHARACTERS IN PS - TUTORIAL”: https://youtu.be/qzvYu48kw5Q

  2. “HOW TO ANIMATE PIXEL ART GAME CHARACTERS IN PS - TUTORIAL”: https://youtu.be/mnJb5iwYAmg

  3. ”HOW TO MAKE SPRITE SHEETS FOR YOUR UNITY GAME - TUTORIAL” https://youtu.be/ou8VkQB2sos

  4. “HOW TO CREATE 2D GAME ANIMATIONS - PS & UNITY TUTORIAL”: https://youtu.be/2o5HerzDohU

This seems to teach exactly what I’m wanting to know. How to create a sprite sheet in photoshop and then import it into Unity.

NOTE:

If your pixels are bleeding when you move them around, make sure that Anti-Alias is clicked off.

Previous
Previous

Day 3 | Importing Sprites to Unity

Next
Next

Day 1 | I’m Making an Indie Game