This section will introduce you to the Unity development environment, the fundamentals of programming, and the C# language from the ground up. By the end of this section, you'll know your way around Unity and have the foundational knowledge necessary to start creating a simple game.
Pop culture has taught us that computer programmers are usually outsiders, lone wolves, or geeky hackers who possess extraordinary mental gifts for algorithmic thought, little social IQ, and the odd anarchic bent. While this is definitely not the case, in reality, there is something to the idea that learning to code fundamentally changes the way you look at the world. The good news is that your naturally curious mind will quickly adapt to this new way of thinking, and maybe even come to enjoy it.
You already use analytical skills in your everyday life that translate to programming—you're just missing the right language and syntax to map those life skills into code. You know your age, right? That's a variable. When you cross the street, I presume you look left, right, then left again like the rest of us. That's evaluating different conditions or what we call control flow. When you look at a can of pop, you instinctively identify that it has properties, such as shape, weight, and contents. That's a class object. You get the idea.
We're going to begin our adventure into C# programming by covering the following topics:
- Basic prerequisites
- Starting out with Unity
- Using C# with Unity
- Working with the Visual Studio editor
- Accessing documentation and resources
Some basic prerequisites
Sometimes it's easier to start with what a thing isn't, rather than what it is. The main goal of this book isn't to learn the vast ins and outs of Unity or all of game development. By necessity, we'll cover these topics at a basic level, and in more detail in Chapter 6, Getting Your Hands Dirty with Unity, but their purpose is to provide a fun, accessible way for us to learn the C# programming language from the ground up.
Since this book is aimed at complete beginners to programming, if you have no previous experience with either C# or Unity, you're in the right place! If you've had some experience with the Unity editor but not with programming, guess what? This is still the place to be. Even if you've dabbled in a bit of C# mixed with Unity, but want to explore some more intermediate or advanced topics, the later chapters can provide you with what you're looking for.
If you're an experienced programmer in other languages, feel free to skip the beginner theory and dive right into the parts you're interested in.
Starting out with Unity 2019
Head over to https://store.unity.com/ and you'll see a few options. Don't get overwhelmed—you can get Unity completely free by selecting the personal option on the right. The other paid options offer more advanced functionality and services, but you can check these out on your own down the road:
After selecting the personal plan, you'll end up on the download screen; accept the terms and conditions and hit Download Installer for Mac OS X:
If you're using a Windows machine, select the Choose Windows link located underneath the installer button, accept the terms and conditions, and you're good to go!
You can also download and manage different versions of Unity through the Unity Hub application, which can be also be downloaded from this screen.
When the download is complete, open up the package (by double-clicking it) and follow the installation instructions. When you get the green light, go ahead and fire up Unity!
All of the examples and screenshots for this book were created and captured using Unity 2018.3.8f1 and verified with version 2019.2.0a7. If you're using a newer version, things might look slightly different in the editor, but following along won't be a problem.
Creating a new project
The first thing you'll see whenUnityfinishes thinking aboutopeningis the dashboard. If you have a Unity account, go ahead andSign in. If not, you can either create one or hitSkipat the bottom of the screen.
Now, let's set up a new project by selecting the New tab at the top right and setting the following fields:
- Project Name: I'll be calling mine Hero Born
- Location:Wherever you'd like the project to be saved
- Template: Make sure this is set to 3Dand hit Create Project:
Navigating the editor
When the new project finishes initializing, you'll see the glorious Unity editor! I've marked the important tabs (or panels, if you prefer) in the following screenshot, which we can break down as follows:
- The Toolbaris the topmost part of the Unity editor. From here, you can manipulate objects (far-left button group), and play and pause the game (center buttons). The rightmost button group contains Unity Services, layer masks, ...