Book description
Key Features
- Understand C# programming basics, terminology, and coding best practices
- Put your knowledge of C# concepts into practice by building a fun and playable game
- Come away with a clear direction for taking your C# programming and Unity game development skills to the next level
Book Description
Over the years, the Learning C# by Developing Games with Unity series has established itself as a popular choice for getting up to speed with C#, a powerful and versatile programming language that can be applied in a wide array of application areas. This book presents a clear path for learning C# programming from the ground up without complex jargon or unclear programming logic, all while building a simple game with Unity.
This fifth edition has been updated to introduce modern C# features with the latest version of the Unity game engine, and a new chapter has been added on intermediate collection types. Starting with the basics of software programming and the C# language, you’ll learn the core concepts of programming in C#, including variables, classes, and object-oriented programming. Once you’ve got to grips with C# programming, you’ll enter the world of Unity game development and discover how you can create C# scripts for simple game mechanics. Throughout the book, you’ll gain hands-on experience with programming best practices to help you take your Unity and C# skills to the next level.
By the end of this book, you’ll be able to leverage the C# language to build your own real-world Unity game development projects.
What you will learn
- Discover easy-to-follow steps and examples for learning C# programming fundamentals
- Get to grips with creating and implementing scripts in Unity
- Create basic game mechanics such as player controllers and shooting projectiles using C#
- Understand the concepts of interfaces and abstract classes
- Leverage the power of the latest C# features to solve complex programming problems
- Become familiar with stacks, queues, exceptions, error handling, and other core C# concepts
- Explore the basics of artificial intelligence (AI) for games and implement them to control enemy behavior
Who this book is for
If you’re a developer, programmer, hobbyist, or anyone who wants to get started with C# programming in a fun and engaging manner, this book is for you. Prior experience in programming or Unity is not required.
Table of contents
- Title Page
- Copyright and Credits
- Dedication
- About Packt
- Contributors
- Preface
-
Getting to Know Your Environment
- Technical requirements
- Getting started with Unity 2020
- Using macOS
- Creating a new project
- Navigating the editor
- Using C# with Unity
- Working with C# scripts
- Introducing the Visual Studio editor
- Time for action – opening a C# file
- Beware of naming mismatches
- Syncing C# files
- Exploring the documentation
- Accessing Unity's documentation
- Time for action – opening the Reference Manual
- Time for action – using the Scripting Reference
- Locating C# resources
- Time for action – looking up a C# class
- Summary
- Pop quiz – dealing with scripts
-
The Building Blocks of Programming
- Defining variables
- Names are important
- Variables act as placeholders
- Time for action – creating a variable
- Time for action – changing a variable's value
- Understanding methods
- Methods drive actions
- Methods are placeholders too
- Time for action – creating a simple method
- Introducing classes
- A common Unity class
- Classes are blueprints
- Working with comments
- Practical backslashes
- Multi-line comments
- Time for action – adding comments
- Putting the building blocks together
- Scripts become components
- A helping hand from MonoBehavior
- Hero's trial – MonoBehavior in the Scripting API
- Communication among classes
- Summary
- Pop quiz – C# building blocks
-
Diving into Variables, Types, and Methods
- Writing proper C#
- Debugging your code
- Declaring variables
- Type and value declarations
- Type-only declarations
- Using access modifiers
- Choosing a security level
- Time for action – making a variable private
- Working with types
- Common built-in types
- Time for action – playing with different types
- Time for action – creating interpolated strings
- Type conversions
- Inferred declarations
- Custom types
- Types roundup
- Naming variables
- Best practices
- Understanding variable scope
- Introducing operators
- Arithmetic and assignments
- Time for action – executing incorrect type operations
- Defining methods
- Basic syntax
- Modifiers and parameters
- Time for action – defining a simple method
- Naming conventions
- Methods are logic detours
- Specifying parameters
- Time for action – adding method parameters
- Specifying return values
- Time for action – adding a return type
- Using return values
- Time for action – capturing return values
- Hero's trial – methods as arguments
- Dissecting common Unity methods
- The Start method
- The Update method
- Summary
- Pop quiz – variables and methods
-
Control Flow and Collection Types
- Selection statements
- The if-else statement
- Basic syntax
- Time for action – thieving prospects
- Using the NOT operator
- Nesting statements
- Evaluating multiple conditions
- Time for action – reaching the treasure
- The switch statement
- Basic syntax
- Pattern matching
- Time for action – choosing an action
- Fall-through cases
- Time for action – rolling the dice
- Pop quiz 1 – if, and, or but
- Collections at a glance
- Arrays
- Basic syntax
- Indexing and subscripts
- Range exceptions
- Lists
- Basic syntax
- Time for action – party members
- Common methods
- Dictionaries
- Basic syntax
- Time for action – setting up an inventory
- Working with dictionary pairs
- Pop quiz 2 – all about collections
- Iteration statements
- For loops
- Time for action – finding an element
- foreach loops
- Looping through key-value pairs
- Hero's trial – finding affordable items
- while loops
- Time for action – tracking player lives
- To infinity and beyond
- Summary
-
Working with Classes, Structs, and OOP
- Defining a class
- Basic syntax
- Time for action – creating a character class
- Instantiating class objects
- Time for action – creating a new character
- Adding class fields
- Time for action – fleshing out character details
- Using constructors
- Time for action – specifying starting properties
- Declaring class methods
- Time for action – printing out character data
- Declaring structs
- Basic syntax
- Time for action – creating a weapon struct
- Understanding reference and value types
- Reference types
- Time for action – creating a new hero
- Value types
- Time for action – copying weapons
- Integrating the object-oriented mindset
- Encapsulation
- Time for action – adding a reset
- Inheritance
- Base constructors
- Time for action – calling a base constructor
- Composition
- Polymorphism
- Time for action – functional variations
- OOP roundup
- Applying OOP in Unity
- Objects are a class act
- Accessing components
- Basic syntax
- Time for action – accessing the current transform component
- Time for action – finding components on different objects
- Drag and drop
- Time for action – assigning variables in Unity
- Summary
- Pop quiz – all things OOP
-
Getting Your Hands Dirty with Unity
- A game design primer
- Game design documents
- The Hero Born one-page
- Building a level
- Creating primitives
- Time for action – creating a ground plane
- Thinking in 3D
- Materials
- Time for action – changing the ground color
- White-boxing
- Editor tools
- Hero's trial – putting up drywall
- Keeping the hierarchy clean
- Time for action – using empty objects
- Working with prefabs
- Time for action – creating a turret
- Time for action – updating the prefab
- Time for action – finishing the level
- Hero's trial – creating a health pickup
- Lighting basics
- Creating lights
- Light component properties
- Animating in Unity
- Creating clips
- Time for action – creating a new clip
- Recording keyframes
- Time for action – spinning animation
- Curves and tangents
- Time for action – smoothing the spin
- The particle system
- Time for action – adding sparkle effects
- Summary
- Pop quiz – basic Unity features
-
Movement, Camera Controls, and Collisions
- Moving the player
- Player setup
- Time for action – creating the player capsule
- Understanding vectors
- Getting player input
- Time for action – player locomotion
- Adding a following Camera
- Time for action – scripting camera behavior
- Working with Unity physics
- Rigidbody components in motion
- Time for action – accessing the Rigidbody component
- Time for action – moving the Rigidbody component
- Colliders and collisions
- Time for action – picking up an item
- Using Collider triggers
- Time for action – creating an enemy
- Time for action – capturing trigger events
- Hero's trial – all the prefabs!
- Physics roundup
- Summary
- Pop quiz – player controls and physics
-
Scripting Game Mechanics
- Adding jumps
- Introducing enumerations
- Underlying types
- Time for action – pressing the spacebar to jump!
- Working with layer masks
- Time for action – setting object layers
- Time for action – one jump at a time
- Shooting projectiles
- Instantiating objects
- Time for action – creating a projectile prefab
- Time for action – adding the shooting mechanic
- Managing GameObject buildup
- Time for action – destroying bullets
- Creating a game manager
- Tracking player properties
- Time for action – creating a game manager
- The get and set properties
- Time for action – adding backing variables
- Time for action – updating item collection
- Adding player polish
- Graphical UI
- Time for action – adding UI elements
- Win and loss conditions
- Time for action – winning the game
- Using directives and namespaces
- Time for action – pausing and restarting
- Summary
- Pop quiz – working with mechanics
-
Basic AI and Enemy Behavior
- Navigating in Unity
- Navigation components
- Time for action – setting up the NavMesh
- Time for action – setting up enemy agents
- Moving enemy agents
- Procedural programming
- Time for action – referencing the patrol locations
- Time for action – moving the enemy
- Time for action – patrolling continuously between locations
- Enemy game mechanics
- Seek and destroy
- Time for action – changing the agent's destination
- Time for action – lowering player health
- Time for action – detecting bullet collisions
- Time for action – updating the game manager
- Refactoring and keeping it DRY
- Time for action – creating a restart method
- Hero's trial – refactoring win/lose logic
- Summary
- Pop quiz – AI and navigation
-
Revisiting Types, Methods, and Classes
- Access Modifier redux
- Constant and read-only properties
- Using the static keyword
- Time for action – creating a static class
- Methods redux
- Overloading methods
- Time for action – overloading the level restart
- Ref parameters
- Time for action – tracking player restarts
- Out parameters
- OOP redux
- Interfaces
- Time for action – creating a manager interface
- Time for action – adopting an interface
- Abstract classes
- Class extensions
- Time for action – extending the string class
- Time for action – using an extension method
- Namespace redux
- Type aliasing
- Summary
- Pop quiz – leveling up
-
Introducing Stacks, Queues, and HashSets
- Introducing stacks
- Basic syntax
- Time for action – storing collected items
- Popping and peeking
- Time for action – the last item collected
- Common methods
- Working with queues
- Basic syntax
- Adding, removing, and peeking
- Common methods
- Using HashSets
- Basic syntax
- Performing operations
- Summary
- Pop quiz – intermediate collections
-
Exploring Generics, Delegates, and Beyond
- Introducing generics
- Generic objects
- Time for action – creating a generic collection
- Generic methods
- Time for action – adding a generic item
- Constraining type parameters
- Time for action – limiting generic elements
- Delegating actions
- Basic syntax
- Time for action – creating a debug delegate
- Delegates as parameter types
- Time for action – using a delegate argument
- Firing events
- Basic syntax
- Time for action – creating an event
- Handling event subscriptions
- Time for action – subscribing to an event
- Handling exceptions
- Throwing exceptions
- Time for action – checking negative scene indexes
- Using try-catch
- Time for action – catching restart errors
- Design pattern primer
- Common game patterns
- Summary
- Pop quiz – intermediate C#
- The Journey Continues
-
Pop Quiz Answers
- Chapter 1 – Getting to Know Your Environment
- Pop quiz – dealing with scripts
- Chapter 2 – The Building Blocks of Programming
- Pop quiz – C# building blocks
- Chapter 3 – Diving into Variables, Types, and Methods
- Pop quiz #1 – variables and types
- Pop quiz #2 – understanding methods
- Chapter 4 – Using Collections and Controlling Your Code
- Pop quiz #1 – if, and, or but
- Pop quiz #2 – all about collections
- Chapter 5 – Working with Classes, Structs, and OOP
- Pop quiz – all things OOP
- Chapter 6 – Getting Your Hands Dirty with Unity
- Pop quiz – basic Unity features
- Chapter 7 – Movement, Camera Controls, and Collisions
- Pop quiz – player controls and physics
- Chapter 8 – Scripting Game Mechanics
- Pop quiz – working with mechanics
- Chapter 9 – Basic AI and Enemy Behavior
- Pop quiz – AI and navigation
- Chapter 10 – Revisiting Types, Methods, and Classes
- Pop quiz – leveling up
- Chapter 11 – Introducing Stacks, Queues, and HashSets
- Pop quiz – intermediate collections
- Chapter 12 – Exploring Generics, Delegates, and Beyond
- Pop quiz – intermediate C#
- Other Books You May Enjoy
Product information
- Title: Learning C# by Developing Games with Unity 2020 - Fifth Edition
- Author(s):
- Release date: August 2020
- Publisher(s): Packt Publishing
- ISBN: 9781800207806
You might also like
book
Learning C# by Developing Games with Unity 2021 - Sixth Edition
Learn C# programming from scratch using Unity as a fun and accessible entry point with this …
book
Learning C# by Developing Games with Unity 2019 - Fourth Edition
Understand the fundamentals of C# programming and get started with coding from ground up in an …
book
Developing 2D Games with Unity: Independent Game Programming with C#
Follow a walkthrough of the Unity Engine and learn important 2D-centric lessons in scripting, working with …
video
Unity C# Scripting — Complete C# for Unity Game Development
Unity is free, easy to learn, has excellent documentation, and is the game engine used to …