Learning C# by Developing Games with Unity - Seventh 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.

Key Features

  • Develop a strong foundation of programming concepts and the C# language
  • Become confident with Unity fundamentals and features in line with Unity 2023
  • Build a playable game prototype in Unity—a working first-person shooter game prototype

Book Description

It's the ability to write custom C# scripts for behaviors and game mechanics that really takes Unity the extra mile. That's where this book can help you as a new programmer!

Harrison Ferrone, in this seventh edition of the bestselling series will take you through the building blocks of programming and the C# language from scratch while building a fun and playable game prototype in Unity.

This book will teach you the fundamentals of OOPs, basic concepts of C#, and Unity engine with lots of code samples, exercises and tips to go beyond the book with your work.

You will write C# scripts for simple game mechanics, perform procedural programming, and add complexity to your games by introducing intelligent enemies and damage-dealing projectiles. You will explore the fundamentals of Unity game development, including game design, lighting basics, player movement, camera controls, collisions, and more with every passing chapter.

Note: The screenshots in the book display the Unity editor in full-screen mode for a comprehensive view. Users can easily reference color versions of images by downloading them from the GitHub repository or the graphics bundle linked in the book.

What you will learn

  • Understanding programming fundamentals by breaking them down into their basic parts
  • Comprehensive explanations with sample codes of object-oriented programming and how it applies to C#
  • Follow simple steps and examples to create and implement C# scripts in Unity
  • Divide your code into pluggable building blocks using interfaces, abstract classes, and class extensions
  • Grasp the basics of a game design document and then move on to blocking out your level geometry, adding lighting and a simple object animation
  • Create basic game mechanics such as player controllers and shooting projectiles using C#
  • 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

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 2023
      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
      3. Unity application lifecycle
        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. Multidimensional arrays
        3. 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-pager
    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. Specialized Collection Types and LINQ
    1. Introducing stacks
      1. Popping and peeking
      2. Common methods
    2. Working with queues
      1. Adding, removing, and peeking
    3. Using HashSets
      1. Performing operations
    4. Intermediate collections roundup
    5. Querying data with LINQ
      1. LINQ basics
      2. Lambda expressions
      3. Chaining queries
      4. Transforming data into new types
      5. Simplifying with optional syntax
    6. Summary
    7. 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 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 classes
      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 - Seventh Edition
  • Author(s): Harrison Ferrone
  • Release date: November 2022
  • Publisher(s): Packt Publishing
  • ISBN: 9781837636877