Learning C# by Developing Games with Unity 2021 - Sixth Edition

Book description

Learn C# programming from scratch using Unity as a fun and accessible entry point with this updated edition of the bestselling series. Includes invitation to join the online Unity Game Development community to read the book alongside peers, Unity developers/C# programmers and Harrison Ferrone.

Purchase of the print or Kindle book includes a free eBook in the PDF format.

Key Features

  • Learn C# programming basics, terminology, and coding best practices
  • Become confident with Unity fundamentals and features in line with Unity 2021
  • Apply your C# knowledge in practice and build a working first-person shooter game prototype in Unity

Book Description

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 with a wide array of applications in various domains. This bestselling franchise presents a clear path for learning C# programming from the ground up through the world of Unity game development.

This sixth edition has been updated to introduce modern C# features with Unity 2021. A new chapter has also been added that covers reading and writing binary data from files, which will help you become proficient in handling errors and asynchronous operations.

The book acquaints you with the core concepts of programming in C#, including variables, classes, and object-oriented programming. You will explore the fundamentals of Unity game development, including game design, lighting basics, player movement, camera controls, and collisions. You will write C# scripts for simple game mechanics, perform procedural programming, and add complexity to your games by introducing smart enemies and damage-causing projectiles.

By the end of the book, you will have developed the skills to become proficient in C# programming and built a playable game prototype with the Unity game engine.

What you will learn

  • Follow simple steps and examples to create and implement C# scripts in Unity
  • Develop a 3D mindset to build games that come to life
  • Create basic game mechanics such as player controllers and shooting projectiles using C#
  • Divide your code into pluggable building blocks using interfaces, abstract classes, and class extensions
  • Become familiar with stacks, queues, exceptions, error handling, and other core C# concepts
  • Learn how to handle text, XML, and JSON data to save and load your game data
  • Explore the basics of 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 Unity and C# programming in a fun and engaging manner, this book is for you. You'll still be able to follow along if you don't have programming experience, but knowing the basics will help you get the most out of this book.

Table of contents

  1. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Get in touch
  2. Getting to Know Your Environment
    1. Technical requirements
    2. Getting started with Unity 2021
      1. Using macOS
      2. Creating a new project
      3. Navigating the editor
    3. Using C# with Unity
      1. Working with C# scripts
      2. Introducing the Visual Studio editor
        1. Opening a C# file
        2. Beware of naming mismatches
      3. Syncing C# files
    4. Exploring the documentation
      1. Accessing Unity's documentation
      2. Locating C# resources
    5. Summary
    6. Pop quiz – dealing with scripts
  3. The Building Blocks of Programming
    1. Defining variables
      1. Names are important
      2. Variables act as placeholders
    2. Understanding methods
      1. Methods drive actions
      2. Methods are placeholders too
    3. Introducing classes
      1. A common Unity class
      2. Classes are blueprints
      3. Communication among classes
    4. Working with comments
      1. Single-line comments
      2. Multi-line comments
      3. Adding comments
    5. Putting the building blocks together
      1. Scripts become components
      2. A helping hand from MonoBehaviour
        1. Hero's trial – MonoBehaviour in the Scripting API
    6. Summary
    7. Pop quiz – C# building blocks
  4. Diving into Variables, Types, and Methods
    1. Writing proper C#
    2. Debugging your code
    3. Understanding variables
      1. Declaring variables
        1. Type and value declarations
        2. Type-only declarations
      2. Using access modifiers
      3. Working with types
        1. Common built-in types
        2. Type conversions
        3. Inferred declarations
        4. Custom types
      4. Naming variables
      5. Understanding variable scope
    4. Introducing operators
      1. Arithmetic and assignments
    5. Defining methods
      1. Declaring methods
      2. Naming conventions
      3. Methods as logic detours
      4. Specifying parameters
      5. Specifying return values
      6. Using return values
        1. Hero's trial – methods as arguments
      7. Dissecting common Unity methods
        1. The Start method
        2. The Update method
    6. Summary
    7. Pop quiz – variables and methods
  5. Control Flow and Collection Types
    1. Selection statements
      1. The if-else statement
        1. Using the NOT operator
        2. Nesting statements
        3. Evaluating multiple conditions
      2. The switch statement
        1. Pattern matching
        2. Fall-through cases
      3. Pop quiz 1 – if, and, or but
    2. Collections at a glance
      1. Arrays
        1. Indexing and subscripts
        2. Range exceptions
      2. Lists
        1. Accessing and modifying lists
      3. Dictionaries
        1. Working with dictionary pairs
      4. Pop quiz 2 – all about collections
    3. Iteration statements
      1. for loops
      2. foreach loops
        1. Looping through key-value pairs
      3. while loops
      4. To infinity and beyond
    4. Summary
  6. Working with Classes, Structs, and OOP
    1. Introducing OOP
    2. Defining classes
      1. Instantiating class objects
      2. Adding class fields
      3. Using constructors
      4. Declaring class methods
    3. Declaring structs
    4. Understanding reference and value types
      1. Reference types
      2. Value types
    5. Integrating the object-oriented mindset
      1. Encapsulation
      2. Inheritance
        1. Base constructors
      3. Composition
      4. Polymorphism
    6. Applying OOP in Unity
      1. Objects are a class act
      2. Accessing components
        1. Accessing components in code
        2. Drag and drop
    7. Summary
    8. Pop quiz – all things OOP
  7. Getting Your Hands Dirty with Unity
    1. A game design primer
      1. Game design documents
      2. The Hero Born one-page
    2. Building a level
      1. Creating primitives
      2. Thinking in 3D
      3. Materials
      4. White-boxing
        1. Editor tools
        2. Hero's trial – putting up drywall
        3. Keeping the hierarchy clean
        4. Working with Prefabs
    3. Lighting basics
      1. Creating lights
      2. Light component properties
    4. Animating in Unity
      1. Creating animations in code
      2. Creating animations in the Unity Animation window
      3. Recording keyframes
      4. Curves and tangents
    5. Summary
    6. Pop quiz – basic Unity features
  8. Movement, Camera Controls, and Collisions
    1. Managing player movement
    2. Moving the player with the Transform component
      1. Understanding vectors
      2. Getting player input
      3. Moving the player
    3. Scripting camera behavior
    4. Working with the Unity physics system
      1. Rigidbody components in motion
      2. Colliders and collisions
        1. Picking up an item
      3. Using Collider triggers
        1. Creating an enemy
        2. Hero's trial – all the Prefabs!
      4. Physics roundup
    5. Summary
    6. Pop quiz – player controls and physics
  9. Scripting Game Mechanics
    1. Adding jumps
      1. Introducing enumerations
        1. Underlying types
      2. Working with layer masks
    2. Shooting projectiles
      1. Instantiating objects
      2. Adding the shooting mechanic
      3. Managing object build-up
    3. Creating a game manager
      1. Tracking player properties
      2. The get and set properties
      3. Updating item collection
    4. Creating a GUI
      1. Displaying player stats
      2. Win and loss conditions
      3. Pausing and restarting the game with using directives and namespaces
    5. Summary
    6. Pop quiz – working with mechanics
  10. Basic AI and Enemy Behavior
    1. Navigating 3D space in Unity
      1. Navigation components
      2. Setting up enemy agents
    2. Moving enemy agents
      1. Procedural programming
      2. Referencing the patrol locations
      3. Moving the enemy
    3. Enemy game mechanics
      1. Seek and destroy: changing the agent's destination
      2. Lowering player health
      3. Detecting bullet collisions
      4. Updating the game manager
    4. Refactoring and keeping it DRY
    5. Summary
    6. Pop quiz – AI and navigation
  11. Revisiting Types, Methods, and Classes
    1. Access modifiers
      1. Constant and read-only properties
      2. Using the static keyword
    2. Revisiting methods
      1. Overloading methods
      2. ref parameters
      3. out parameters
    3. Intermediate OOP
      1. Interfaces
      2. Abstract classes
      3. Class extensions
    4. Namespace conflicts and type aliasing
    5. Summary
    6. Pop quiz – leveling up
  12. Introducing Stacks, Queues, and HashSets
    1. Introducing stacks
      1. Popping and peeking
      2. Common methods
    2. Working with queues
      1. Adding, removing, and peeking
      2. Common methods
    3. Using HashSets
      1. Performing operations
    4. Intermediate collections roundup
    5. Summary
    6. Pop quiz – intermediate collections
  13. Saving, Loading, and Serializing Data
    1. Introducing data formats
      1. Breaking down XML
      2. Breaking down JSON
    2. Understanding the filesystem
      1. Working with asset paths
      2. Creating and deleting directories
      3. Creating, updating, and deleting files
    3. Working with streams
      1. Managing your Stream resources
      2. Using a StreamWriter and StreamReader
      3. Creating an XMLWriter
      4. Automatically closing streams
    4. Serializing data
      1. Serializing and deserializing XML
      2. Serializing and deserializing JSON
    5. Data roundup
    6. Summary
    7. Pop quiz – data management
  14. Exploring Generics, Delegates, and Beyond
    1. Introducing generics
      1. Generic objects
      2. Generic methods
      3. Constraint type parameters
      4. Adding generics to Unity objects
    2. Delegating actions
      1. Creating a debug delegate
      2. Delegates as parameter types
    3. Firing events
      1. Creating and invoking events
      2. Handling event subscriptions
      3. Cleaning up event subscriptions
    4. Handling exceptions
      1. Throwing exceptions
      2. Using try-catch
    5. Summary
    6. Pop quiz – intermediate C#
  15. The Journey Continues
    1. Diving deeper
    2. Remembering your object-oriented programming
    3. Design patterns primer
    4. Approaching Unity projects
    5. Unity features we didn't cover
    6. Next steps
      1. C# resources
      2. Unity resources
      3. Unity certifications
    7. Hero's trial – putting something out into the world
    8. Summary
  16. Pop Quiz Answers
  17. Other Books You May Enjoy
  18. Index

Product information

  • Title: Learning C# by Developing Games with Unity 2021 - Sixth Edition
  • Author(s): Harrison Ferrone
  • Release date: October 2021
  • Publisher(s): Packt Publishing
  • ISBN: 9781801813945