Monthly Tutorials for Beginners
Welcome and thanks for joining me! You are here because you are probably really interested in learning how to code and likely want to become a Game Developer. I've created this course for the *very* beginner, so we'll be taking things slowly. Every programmer starts with learning to write "Hello World", so think of this as a slightly advanced version of "Hello World".
In this tutorial I will cover:
I'm not going to go over installing Unity, since it is so easy, but if you haven't already, download and install the free version of Unity 2017.2 Personal Edition. When prompted make sure you keep the boxes checked for Visual Studio Community, since that is the IDE (integrated development environment) that we will be using. Come back when it's finished. It's about a 10GB file, so it may take a while to download and install.
Now that you're done with that, go ahead and open Unity. Here we are going to create our first project, a very simple interactive book. We'll take baby steps in this tutorial, so that in the next tutorial you will understand the basics to create your first game.
Creating the Project
Creating the Project
Create a 2d Project, name it what ever you see fit and file it in a logical place on your computer.
Here I am naming my project: 'My Comic Book' and saving it to my documents, in a folder I created for all of my tutorials.
Next let's go over the need-to-know pieces that make up the Unity Editor Interface.
Each of these allow you to transform game objects in your scene and transform your scene. The 'hand' moves the scene around. The "4-way arrow" allows you to move objects on the x, y, z axis. The rotate arrows allow you to rotate your object. The next one changes the scale and the last "box" one changes the height and width of your game objects.
There's a lot more to this, but this will get you started. Let's jump over to Visual Studio and prepare that now.
Visual Studio Community Set-Up
Let's go to Visual Studio (which should have been downloaded and installed with Unity) and make sure you have the right preferences on.
First, we want to see line numbers in our code, so let's make sure those are toggled on.
Next, we want to be able to collapse our code when we set up sections, so let's toggle that as well.
Finally, we always want our code to look very readable and spaced correctly, so let's set up a hot key to format our code in the case it gets messy. This will allow us to highlight blocks of code press CMD ']' (or what ever you prefer and will remember) and it will automatically format our code with the correct spacing.
Now that we have a basic understanding of our dev environment and it is all set up, let's write our first script. This script will handle our first page of our interactive book, so we will need a reference to a text object, a public function that we will attach to our button and a way to change to the next scene/page. I've commented it so that you can read it line by line and understand what everything does. Keep in mind the comments, and try to be this diligent with commenting your code. If you are truly trying to learn to code, then do yourself a favor and write out each line, rather than copying and pasting the chunks. Seriously. Don't.
Let's take a look at the first script.
Now that you've written (and hopefully you did write and not just copy and paste) your first script, let's switch back to Unity and let it compile. You'll notice that as soon as you switch back to Unity you'll have a little loading spinner in the bottom, right corner. Wait until it goes away, then move on. Every time you make a change in the script, Unity will re-compile your code.
Creating Scene Objects
We have a few public items from our script that we need to connect, but first we need to add the game objects to the scene so that we can do so.
First, let's add a Canvas.
The Canvas will hold our Text object that will display our string and it will hold our Button that will cause the string or scene to change. It is good practice to rename each object as you create it, so that you know exactly what it's purpose is at first glance. Sometimes you will end up with multiple Text/Image/Canvas objects, so rather than having to click each one and see what components are attached, you will know just by reading the name. In a future tutorial, we will also see how we can access a game object in a script simply by finding it based on the name. In the Hierarchy go to: Create->UI->Canvas.
Now let's add a plain Game Object to the scene (not as a child to the Canvas) and rename it "SceneManager", then attach our script to it by either dragging and dropping or by adding a 'Component' to it. Right click any where in the Hierarchy and create a new game object like so:
Now you can see the public Text in the "SceneManager", so let's literally drag and drop our Text object to it and drop it in the field.
Next we need to connect our button to the public function. In your button object, scroll down to the Button Script, click the '+', drag our Scene Manager to the empty field. This will now allow us to select our Script (that was attached to our Scene Manager) in the drop down list. Find the script and then find the 'ChangeText' public method that we created and select it.
We need to reposition our button so that the button is transparent and takes up the entire screen.
Let's also modify the text box so that it doesn't cut off any text (truncate) and instead overflows the box so that we see all of our text. Take a moment also to play around with the color, fonts and sizing.
Now that we have a scene set up and ready, we need to save it. You wouldn't want to loose all this work, would you? Following the same steps that we did to create our Scripts Folder, create a folder named 'Scenes'. Then go to File->SaveSceneAs and find this new folder and select it, then name the scene what ever you want. As soon as you do, you'll see your scene in your Project view, under 'Scenes' folder.
As soon as you save, hit the "Play" button in Unity that is in the toolbar that we discussed earlier. With a tap of the screen, the text changes, and after a few taps we notice we have an error in the 'Console.' The console is where we will see our logs, errors and warnings. Warnings will not interrupt your game in most cases, but when you hit an error, then the program can no longer run as expected. Let's remedy this error so that we can have a clean console.
We have a first scene, but we need to create a second scene to switch to when our If statement hits that condition. Go to your computer's file structure and find your scene, then duplicate it and name it "SecondScene". When you are done, go back to Unity and you'll see the new scene in your 'Scenes' folder in your Project view. Double click the scene and if prompted, save any changes you made.
Now we need to update our methods to swap between the scenes. First create a new script called, "ProjectEnums". Here we will create a public Enumerator that will hold the name of the two scenes as two different strings. This enumerator will contain the names of the Scenes that we create in our project. Enumerator lists are constants, so they never change, but you can always add to the list. Let's take a look at the our Project Enums below:
We can access these public enums from our Script now. Go back to your 'IntroManager' and add a new public variable "nextScene" as shown below. Also update the load scene statement to pass the 'nextScene' variable, but we need to pass it as a string. See below:
We need to also update the strings that we display in the 'ChangeText' method. To do this we will create a new public variable, but this time it will be a string array. An array is a type of data structure in C#. An array holds an ordered series of the specified data type. As the information is fed into the array, we can get access to that same item based on what position it is in the array. Let's add the variable and take a look:
We can update our 'ChangeText' function now to account for this new string array, rather than having so many if/else checks. We'll now use the amount of strings in our array, coupled with our original integer to display our story. Here's the updated script:
Now let's go to our very first scene in Unity, and update our two new variables like so:
Next, let's update the second scene to our new variables as well.
Finally, we need to add both scenes to our build settings, so that Unity knows which scenes it needs to build with. To do that, go to File->BuildSettings and drag both scenes in order to the "Scenes In Build" area.
Hit the 'Play' button and see what you created! You will now have an infinite loop of your book that you created.
Congratulations! You have the basics of what could be your first app! There are plenty more features you could add to this, but this is a great start! Feel free to ask questions or leave any comments. The next tutorial will be your very first game! If you want to stay updated, follow me on Facebook, Twitter or Instagram.