Learning C# by Developing Games With Unity 5.x - Second Edition - Sample Chapter
Learning C# by Developing Games With Unity 5.x - Second Edition - Sample Chapter
Second Edition
$ 44.99 US
28.99 UK
"Community
Experience
Distilled"
Sa
m
pl
C o m m u n i t y
E x p e r i e n c e
D i s t i l l e d
Learning C# by Developing
Games with Unity 5.x
Greg Lukosek
Second Edition
Learning C# by Developing
Games with Unity 5.x
ee
Second Edition
Develop your first interactive 2D platformer game by learning the
fundamentals of C#
Greg Lukosek
he was about 8 years old, his amazing parents bought him and his brother a
Commodore C64. That was when his love of programming started. He would spend
hours writing simple basic code, and when he couldn't write it on the computer
directly, he used a notepad.
Greg completed his mechanical engineering diploma at ZSTiO Meritum
Siemianowice Slaskie, Poland. He has learned all his programming skills through
determination and hard work at home.
Greg met the love of his life, Kasia, in 2003, which changed his life forever. They both
moved to London in search of adventure and decided to stay there.
He started work as a 3D artist and drifted away from programming for some years.
Deep inside, he still felt the urge to come back to game programming. During
his career as a 3D artist, he discovered Unity and adopted it for an interactive
visualizations project. At that very moment, he started programming again.
His love for programming overcomes his love for 3D graphics. Greg ditched his 3D
artist career and came back to writing code professionally. He is now doing what he
really wanted to do since he was 8 years olddeveloping games.
These days, Greg lives in a little town called Sandy in the UK with Kasia and
their son, Adam.
Preface
Hello, future game developers! If you are reading this book, you are probably
a curious person trying to learn more about a great game engineUnityand
specifically, programming in C#. This book will take you on a learning journey. We
will go through it together, beginning with the fundamentals of programming and
finishing with a functional 2D platform game.
Preface
Chapter 9, Starting Your First Game, helps us transform an idea into a real Unity
project.
Chapter 10, Writing GameManager, gets you acquainted with the basics of the
singleton approach and also helps you work through the gameplay loop.
Chapter 11, The Game Level, helps you learn how to create reusable pieces
of a level and also how to populate them to create the illusion of an endlessly
running game.
Chapter 12, The User Interface, explains how to construct and implement the user
interface in our game.
Chapter 13, Collectables What Next?, focuses on collectables and storing some data
between Unity sessions.
Defining a method
Calling a method
Variables are the first major building block of C# and methods are the second, so let's
dive into methods.
[ 55 ]
All of the executable code in a script is inside methods. The first purpose of a method
is to work with the member variables of the class. The member variables store data
that is needed for a component to give a GameObject its behavior. The whole reason
for writing a script is to make a GameObject do something interesting. A method is
the place where we make a behavior come to life.
The second purpose of a method is to create code blocks that will be used over and
over again. You don't want to be writing the same code over and over. Instead, you
place the code in a code block and give it a name so that you can call it whenever
needed.
Let's take a quick look at this example:
This is a perfect example of the function that does something useful. It might look
a bit strange to you as it takes two parameters. Don't worry about it too much as of
now; we will cover it in detail soon. All I want you to notice right now is that the
preceding method can take some data and do something useful with it. In this case,
it is adding two numbers and printing the result on the Unity console. Now, the
best part nowwe can call this method as many times as we want, passing different
parameters, without repeating the code every time we need it. If you feel confused,
don't worry. Just remember that a function can save you from repeating code over
and over again.
Methods can also return some data. We will cover this at a later stage in this chapter.
[ 56 ]
Chapter 4
Don't make them too simple. Suppose you name a method Wiggle. Sure, you know
what Wiggle means right now, but six months later, you'll look at that and say
"Wiggle? Wiggle what?" It takes only a moment more to be a little more precise and
write WiggleDogsTail. Now, when you see this method name, you'll know exactly
what it's going to do.
You can see that the name is actually three words squished together. Since method
names can have only one word, the first word begins with an uppercase, and then we
just capitalize the first letter of every additional word, for example, PascalCasing.
The type of information, or data, that a method will return to the place from
where it was called
NameOfTheMethod ( )
Looking at LearningScript once again, or any Unity-generated script, you can see
that the Start() method has the three minimum requirements for a method:
void Start ()
{
}
Our first requirement is the type of data that the method will return to the
place in the code that called this method. This method isn't returning any
value, so instead of specifying an actual type of data, the void keyword is
used. This informs Unity that nothing is being returned from the method.
The last requirement is the curly braces. They contain the code that defines
what the method is going to do.
[ 58 ]
Chapter 4
This example fulfills the bare minimum requirements for a method. However, as
you can see, there's no code in the code block, so when Start() is called by Unity,
it doesn't do anything at all. Yet it's a method. Normally, if we aren't going to use
a method by adding code to a skeleton method created by Unity, we can simply
remove them from our script. It's normally best to remove unused code after the
script has been written.
Here's what we know about this bare-minimum method definition as far as Unity is
concerned:
[ 59 ]
A script may need to add two numbers several times, but they probably won't
always be the same two numbers. We can have possibly hundreds of different
combinations of two numbers to add together. This means that we need to let the
method know which two numbers need to be added together at the moment when
we call the method. Let's write a code example to make sure you fully understand it:
[ 60 ]
Chapter 4
As expected! The console will print out the values. There's a special name for
information between the parentheses of a method definition, such as line 23the
code is called method parameters.
Okay, so you're setting parameters, or limits, on the type of data the method can use,
but what exactly is a parameter? Well, the first parameter is called firstNumber.
And what is firstNumber doing? It stores a value that will be used in the code block
on line 25. What do we call things that store data? That's right, variables! Variables
are used everywhere.
Remember that a variable is just a substitute name for the value
it actually stores.
As you can see on line 25 of the code block, those variables are being added and
stored in the result variable.
[ 62 ]
Chapter 4
As you can see, this method is very similar to the AddAndPrintTwoNumbers method
that we spoke of previously. The two main differences are highlighted.
A return type function will always begin with a description of the type of data that it's
returning. In this case, we will be returning the sum of two numbers, so our type is int
(an integer). In simple words, the AddTwoNumbers function is returning a number.
Example
You just learned how to write a return type method. Time to put it to use! Let's
write a new script and call it LearningReusableMethodsWithReturn:
[ 63 ]
What do we have here? You probably understand most of this code with no issues,
but it's good practice to go through it line by line. Lines 7 and 8 contain declarations
of the number1 and number2 integer variables. Lines 22 to 27 are exactly the same as
we used in the last example. They have the declaration of a method that takes two
parametersfirstNumber and secondNumberand it returns a value of the int type.
Lines 30 to 34 contain the declaration of method that simply prints the given int
value on the Unity console. Now is the most important part you need to remember.
Take a look at line 14:
int sumResult = AddTwoNumbers(number1, number2);
The left-hand side of this line is a simple declaration of an int variable called
sumResult. Simple! What I want to talk about is the right-hand sidethe assignment
of this variable. As you can see, what we are doing here is calling the AddTwoNumbers
method instead of simply giving the value to be stored in sumResult. It might look a
bit awkward. You would expect a value to be passed instead of another method call.
Let me explain how it works. The AddTwoNumbers method is a return type method.
It does return an int value in every place where you call itinstantly. In even
simpler words, AddTwoNumbers() is an integer, and a number value.
This concept might be a bit difficult to get your head around. If you still don't get
it, don't worry. All you need to remember right now is the fact that, whenever a
program calls a method that returns something, it is calling the method and inserting
the value that the method returns into the place where it made the call.
Remember I told you that, when you call a method, it's just a substitute for the code
block that will be executed. It's like taking all of the code in the method's code block
and placing it right where the method was called.
Summary
In this chapter, you learned more details about methods. We will start using methods
everywhere in this book. Feel free to come back to this chapter if you feel lost.
In the next chapter, we will introduce a little more complex ideas of handling, lists,
arrays, and dictionaries.
[ 64 ]
www.PacktPub.com
Stay Connected: