Instant Access to Learning C by Developing Games with Unity 5 x Develop your first interactive 2D platformer game by learning the fundamentals of C Second Edition Greg Lukosek ebook Full Chapters
Instant Access to Learning C by Developing Games with Unity 5 x Develop your first interactive 2D platformer game by learning the fundamentals of C Second Edition Greg Lukosek ebook Full Chapters
com
https://textbookfull.com/product/developing-2d-games-with-unity-
independent-game-programming-with-c-1st-edition-jared-halpern/
textbookfull.com
https://textbookfull.com/product/data-analytics-and-management-in-
data-intensive-domains-leonid-kalinichenko/
textbookfull.com
Happy Never After why the happiness fairytale is driving
us mad and how I flipped the script Jill Stark
https://textbookfull.com/product/happy-never-after-why-the-happiness-
fairytale-is-driving-us-mad-and-how-i-flipped-the-script-jill-stark/
textbookfull.com
https://textbookfull.com/product/operation-linebacker-
ii-1972-the-b-52s-are-sent-to-hanoi-1st-edition-marshall-l-michel-iii/
textbookfull.com
https://textbookfull.com/product/daddys-business-partner-forbidden-
temptations-3-1st-edition-sofia-t-summers/
textbookfull.com
https://textbookfull.com/product/inverse-synthetic-aperture-radar-
imaging-with-matlab-algorithms-2nd-edition-caner-ozdemir/
textbookfull.com
https://textbookfull.com/product/the-elven-apostate-1st-edition-sara-
c-roethle/
textbookfull.com
Introductory Quantum Mechanics A Traditional Approach
Emphasizing Connections with Classical Physics 1st Edition
Paul R. Berman
https://textbookfull.com/product/introductory-quantum-mechanics-a-
traditional-approach-emphasizing-connections-with-classical-
physics-1st-edition-paul-r-berman/
textbookfull.com
Table of Contents
Learning C# by Developing Games with Unity 5.x Second Edition
Credits
About the Author
About the Reviewer
www.PacktPub.com
eBooks, discount offers, and more
Why subscribe?
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Downloading the color images of this book
Errata
Piracy
Questions
1. Discovering Your Hidden Scripting Skills and Getting Your
Environment Ready
Prerequisite knowledge to use this book
Dealing with scriptphobia
Downloading Unity
Obtaining a free license
Teaching behavior to GameObjects
Using Unity's documentation
Do I need to know all that?
C# documentation – where to find it? Do I need it at all?
The Unity community – asking others for help
Working with C# script files
Lots of files can create a mess
Why does my Project tab look different?
Creating a C# script file
Introducing the MonoDevelop code editor
Syncing C# files between MonoDevelop and Unity
Opening LearningScript in MonoDevelop
The namespace – highlighted in blue
The class definition – highlighted in green
Watching for possible gotchas while creating script files in Unity
Fixing synchronization if it isn't working properly
Adding our script to GameObject
Instance? What is it?
Summary
2. Introducing the Building Blocks for Unity Scripts
Understanding what a variable is and what it does
Naming a variable
A variable name is just a substitute for a value
Creating a variable and seeing how it works
Declaration
Assignment
Click on Play!
Changing variables
Watching for a possible gotcha when using public variables
What is a method?
Using the term "method" instead of "function"
Method names are substitutes, too
Introducing the class
Inheritance
The Start(), Update(), and Awake() methods and the execution
order
Components that communicate using dot syntax
What's with the dots?
Making decisions in code
Using the NOT operator to change the condition
Checking many conditions in an if statement
Using else if to make complex decisions
Making decisions based on user input
Paper and pencil are powerful tools
Summary
3. Getting into the Details of Variables
Writing C# statements properly
Understanding component properties in Unity's Inspector
Variables become component properties
Unity changes script and variable names slightly
Changing a property's value in the Inspector panel
Displaying public variables in the Inspector panel
Private variables
Naming your variables properly
Beginning variable names with lowercase
Using multiword variable names
Declaring a variable and its type
The most common built-in variable types
Assigning values while declaring a variable
Where you declare a variable is important
Variable scope – determining where a variable can be used
Summary
4. Getting into the Details of Methods
Using methods in a script
Naming methods properly
Beginning method names with an uppercase letter
Using multiword names for a method
Parentheses are part of the method's name
Defining a method the right way
The minimum requirements for defining a method
Understanding parentheses – why are they there?
Specifying a method's parameters
How many parameters can a method have?
Returning a value from a method
Returning the value
Example
Summary
5. Lists, Arrays, and Dictionaries
What is an array?
Declaring an array
Storing items in the List
Common operations with Lists
List<T> versus arrays
Retrieving the data from the Array or List<T>
Checking the size
ArrayList
Dictionaries
Accessing values
How do I know what's inside my Hashtable?
Summary
6. Loops
Introduction to loops
The foreach loop
The for loop
An example
The while loop
while versus for loops
Loops in statements
Modulo
Searching for data inside an array
Breaking the loop
Summary
7. Object, a Container with Variables and Methods
Working with objects is a class act
Few facts
Example
Instantiating an object
Bored yet?
Using methods with objects
Custom constructors
Overloading
Summary
8. Let's Make a Game! – From Idea to Development
Your first game – avoiding the trap of the never-ending concept
The idea
Game mechanics and core components
Breaking a complex idea into smaller parts
Jake on the mysterious planet – the feature list
Procedural level generation
An animated 2D character
Physics
Mouse and touch controls
Collectables and obstacles
Scoring
UI – the user interface
Target platform and resolution
Target screen resolution
Summary
9. Starting Your First Game
Setting up a new Unity project for our game
Backup
Keeping your project clean
Preparing the player prefab
Rigidbody2D
CircleCollider2D
PlayerController
User input
Jump
Animator
Running
Code
PlayerController.cs
Summary
10. Writing GameManager
Gameplay loops
Singleton class
Starting the game
Setting up input keys
Using triggers
Restarting the game
Setting up the player starting position
Code in this chapter
Summary
11. The Game Level
Generating levels versus designed levels
Creating a level chunk
Planning the LevelGenerator class
Writing LevelGenerator
Commenting on your code
Creating a copy of the level piece
Instantiating
Vector3
Testing LevelGenerator
Extending the level
The code used in this chapter
Summary
12. The User Interface
Introducting the Unity UI
Views
Constructing the view UI – how to keep things clean
Target screen resolution
Recognizing events
Buttons
A simple button
Image
The Button component
Interaction
The Button action
Hiding and showing the Canvas
Reference exceptions
GameView
Game Over
The code in this chapter
Summary
13. Collectables — What Next?
Collectables
The coin prefab
The Collectable class
High score and persisting data
The Update function and UI values
What next?
The code in this chapter
Summary
Index
Learning C# by Developing
Games with Unity 5.x Second
Edition
Learning C# by Developing
Games with Unity 5.x Second
Edition
Copyright © 2016 Packt Publishing
Every effort has been made in the preparation of this book to ensure
the accuracy of the information presented. However, the information
contained in this book is sold without warranty, either express or
implied. Neither the author, nor Packt Publishing, and its dealers and
distributors will be held liable for any damages caused or alleged to
be caused directly or indirectly by this book.
Livery Place
35 Livery Street
Visit https://textbookfull.com
now to explore a rich
collection of eBooks, textbook
and enjoy exciting offers!
Birmingham B3 2PB, UK.
ISBN 978-1-78528-759-6
www.packtpub.com
Credits
Author
Greg Lukosek
Terry Norton
Reviewer
Karl Henkel
Commissioning Editor
Ashwin Nair
Acquisition Editor
Vinay Argekar
Deepti Thore
Technical Editor
Mohita Vyas
Copy Editor
Vikrant Phadke
Project Coordinator
Shweta H Birwatkar
Proofreader
Safis Editing
Indexer
Mariammal Chettiyar
Graphics
Disha Haria
Production Coordinator
Nilesh Mohite
Cover Work
Nilesh Mohite
About the Author
Greg Lukosek was born and raised in the Upper Silesia region of
Poland. When 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 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.
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 old—developing games.
These days, Greg lives in a little town called Sandy in the UK with
Kasia and their son, Adam.
I want to thank my loving wife, Kasia, for all her love and
support. Without her, writing this book would be simply
impossible. I also want to thank my loving parents, Ela and
Marek, and brother, Artur, for always believing in me and giving
me exceptional support when I needed it.
https://www2.packtpub.com/books/subscription/packtlib
Why subscribe?
Fully searchable across every book published by Packt
Copy and paste, print, and bookmark content
On demand and accessible via a web browser
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
engine—Unity—and 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.
What this book covers
Chapter 1, Discovering Your Hidden Scripting Skills and Getting Your
Environment Ready, puts you at ease with writing scripts for Unity.
Chapter 4, Getting into the Details of Methods, helps you learn more
in detail about methods and how to use them to understand the
importance of code blocks and the variables used in them.
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.
https://unity3d.com/unity/system-requirements
Who this book is for
The book is targeted at beginner-level Unity developers with no prior
programming experience. If you are a Unity developer and wish to
create games by learning how to write C# scripts or code, then this
book is for you.
Conventions
In this book, you will find a number of text styles that distinguish
between different kinds of information. Here are some examples of
these styles and an explanation of their meaning.
using UnityEngine;
using System.Collections;
LevelGenerator.instance.AddPiece();
LevelGenerator.instance.RemoveOldestPiece();
}
New terms and important words are shown in bold. Words that
you see on the screen, for example, in menus or dialog boxes,
appear in the text like this: "When you are ready, click on Play in
Unity."
Note
Warnings or important notes appear in a box like this.
Tip
Tips and tricks appear like this.
Reader feedback
Feedback from our readers is always welcome. Let us know what
you think about this book—what you liked or disliked. Reader
feedback is important for us as it helps us develop titles that you will
really get the most out of.
If there is a topic that you have expertise in and you are interested
in either writing or contributing to a book, see our author guide at
www.packtpub.com/authors.
Random documents with unrelated
content Scribd suggests to you:
Project Gutenberg™ eBooks are often created from several printed
editions, all of which are confirmed as not protected by copyright in
the U.S. unless a copyright notice is included. Thus, we do not
necessarily keep eBooks in compliance with any particular paper
edition.
Most people start at our website which has the main PG search
facility: www.gutenberg.org.