Learning C# by Developing Games with Unity 2020 - Fifth Edition

Book description

Get to grips with coding in C# and build simple 3D games with Unity from the ground up with this updated fifth edition of the bestselling guide

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.

Publisher resources

Download Example Code

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Learning C# by Developing Games with Unity 2020 Fifth Edition
  3. Dedication
  4. About Packt
    1. Why subscribe?
  5. Contributors
    1. About the author
    2. About the reviewers
    3. Packt is searching for authors like you
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Download the color images
    6. Conventions used
    7. Get in touch
    8. Reviews
  7. Getting to Know Your Environment
    1. Technical requirements
    2. Getting started with Unity 2020
    3. Using macOS 
    4. Creating a new project
    5. Navigating the editor
    6. Using C# with Unity 
    7. Working with C# scripts
    8. Introducing the Visual Studio editor
    9. Time for action – opening a C# file
    10. Beware of naming mismatches
    11. Syncing C# files
    12. Exploring the documentation
    13. Accessing Unity's documentation
    14. Time for action – opening the Reference Manual
    15. Time for action – using the Scripting Reference
    16. Locating C# resources
    17. Time for action – looking up a C# class
    18. Summary
    19. Pop quiz – dealing with scripts
  8. The Building Blocks of Programming
    1. Defining variables
    2. Names are important
    3. Variables act as placeholders
    4. Time for action – creating a variable
    5. Time for action – changing a variable's value
    6. Understanding methods
    7. Methods drive actions
    8. Methods are placeholders too
    9. Time for action – creating a simple method
    10. Introducing classes
    11. A common Unity class
    12. Classes are blueprints
    13. Working with comments
    14. Practical backslashes
    15. Multi-line comments
    16. Time for action – adding comments
    17. Putting the building blocks together
    18. Scripts become components
    19. A helping hand from MonoBehavior
    20. Hero's trial – MonoBehavior in the Scripting API
    21. Communication among classes
    22. Summary
    23. Pop quiz – C# building blocks
  9. Diving into Variables, Types, and Methods
    1. Writing proper C#
    2. Debugging your code
    3. Declaring variables
    4. Type and value declarations
    5. Type-only declarations
    6. Using access modifiers
    7. Choosing a security level
    8. Time for action – making a variable private
    9. Working with types
    10. Common built-in types
    11. Time for action – playing with different types
    12. Time for action – creating interpolated strings
    13. Type conversions
    14. Inferred declarations
    15. Custom types
    16. Types roundup
    17. Naming variables
    18. Best practices
    19. Understanding variable scope
    20. Introducing operators
    21. Arithmetic and assignments
    22. Time for action – executing incorrect type operations
    23. Defining methods
    24. Basic syntax
    25. Modifiers and parameters
    26. Time for action – defining a simple method
    27. Naming conventions
    28. Methods are logic detours
    29. Specifying parameters
    30. Time for action – adding method parameters
    31. Specifying return values
    32. Time for action – adding a return type
    33. Using return values
    34. Time for action – capturing return values 
    35. Hero's trial – methods as arguments
    36. Dissecting common Unity methods
    37. The Start method
    38. The Update method
    39. Summary
    40. Pop quiz – variables and methods
  10. Control Flow and Collection Types
    1. Selection statements
    2. The if-else statement
    3. Basic syntax
    4. Time for action – thieving prospects
    5. Using the NOT operator
    6. Nesting statements
    7. Evaluating multiple conditions
    8. Time for action – reaching the treasure
    9. The switch statement
    10. Basic syntax
    11. Pattern matching
    12. Time for action – choosing an action
    13. Fall-through cases
    14. Time for action – rolling the dice
    15. Pop quiz 1 – if, and, or but
    16. Collections at a glance
    17. Arrays
    18. Basic syntax
    19. Indexing and subscripts
    20. Range exceptions
    21. Lists
    22. Basic syntax
    23. Time for action – party members
    24. Common methods
    25. Dictionaries 
    26. Basic syntax
    27. Time for action – setting up an inventory
    28. Working with dictionary pairs
    29. Pop quiz 2 – all about collections
    30. Iteration statements
    31. For loops
    32. Time for action – finding an element
    33. foreach loops
    34. Looping through key-value pairs
    35. Hero's trial – finding affordable items
    36. while loops
    37. Time for action – tracking player lives
    38. To infinity and beyond
    39. Summary
  11. Working with Classes, Structs, and OOP
    1. Defining a class 
    2. Basic syntax
    3. Time for action – creating a character class
    4. Instantiating class objects
    5. Time for action – creating a new character
    6. Adding class fields
    7. Time for action – fleshing out character details
    8. Using constructors
    9. Time for action – specifying starting properties
    10. Declaring class methods
    11. Time for action – printing out character data
    12. Declaring structs
    13. Basic syntax
    14. Time for action – creating a weapon struct
    15. Understanding reference and value types
    16. Reference types
    17. Time for action – creating a new hero
    18. Value types
    19. Time for action – copying weapons
    20. Integrating the object-oriented mindset
    21. Encapsulation
    22. Time for action – adding a reset
    23. Inheritance
    24. Base constructors
    25. Time for action – calling a base constructor
    26. Composition
    27. Polymorphism
    28. Time for action – functional variations
    29. OOP roundup
    30. Applying OOP in Unity
    31. Objects are a class act
    32. Accessing components
    33. Basic syntax
    34. Time for action – accessing the current transform component
    35. Time for action – finding components on different objects
    36. Drag and drop
    37. Time for action – assigning variables in Unity
    38. Summary
    39. Pop quiz – all things OOP
  12. Getting Your Hands Dirty with Unity
    1. A game design primer
    2. Game design documents
    3. The Hero Born one-page
    4. Building a level
    5. Creating primitives
    6. Time for action – creating a ground plane
    7. Thinking in 3D
    8. Materials
    9. Time for action – changing the ground color
    10. White-boxing
    11. Editor tools
    12. Hero's trial – putting up drywall
    13. Keeping the hierarchy clean
    14. Time for action – using empty objects
    15. Working with prefabs
    16. Time for action – creating a turret
    17. Time for action – updating the prefab
    18. Time for action – finishing the level
    19. Hero's trial – creating a health pickup
    20. Lighting basics
    21. Creating lights
    22. Light component properties
    23. Animating in Unity
    24. Creating clips
    25. Time for action – creating a new clip
    26. Recording keyframes
    27. Time for action – spinning animation
    28. Curves and tangents
    29. Time for action – smoothing the spin
    30. The particle system
    31. Time for action – adding sparkle effects
    32. Summary
    33. Pop quiz – basic Unity features
  13. Movement, Camera Controls, and Collisions
    1. Moving the player
    2. Player setup
    3. Time for action – creating the player capsule
    4. Understanding vectors
    5. Getting player input
    6. Time for action – player locomotion
    7. Adding a following Camera
    8. Time for action – scripting camera behavior
    9. Working with Unity physics
    10. Rigidbody components in motion
    11. Time for action – accessing the Rigidbody component
    12. Time for action – moving the Rigidbody component
    13. Colliders and collisions
    14. Time for action – picking up an item
    15. Using Collider triggers
    16. Time for action – creating an enemy
    17. Time for action – capturing trigger events
    18. Hero's trial – all the prefabs!
    19. Physics roundup
    20. Summary
    21. Pop quiz – player controls and physics
  14. Scripting Game Mechanics
    1. Adding jumps
    2. Introducing enumerations 
    3. Underlying types
    4. Time for action – pressing the spacebar to jump!
    5. Working with layer masks
    6. Time for action – setting object layers
    7. Time for action – one jump at a time
    8. Shooting projectiles
    9. Instantiating objects
    10. Time for action – creating a projectile prefab
    11. Time for action – adding the shooting mechanic
    12. Managing GameObject buildup
    13. Time for action – destroying bullets
    14. Creating a game manager
    15. Tracking player properties
    16. Time for action – creating a game manager
    17. The get and set properties
    18. Time for action – adding backing variables
    19. Time for action – updating item collection
    20. Adding player polish
    21. Graphical UI
    22. Time for action – adding UI elements
    23. Win and loss conditions
    24. Time for action – winning the game 
    25. Using directives and namespaces
    26. Time for action – pausing and restarting 
    27. Summary
    28. Pop quiz – working with mechanics
  15. Basic AI and Enemy Behavior
    1. Navigating in Unity
    2. Navigation components
    3. Time for action – setting up the NavMesh
    4. Time for action – setting up enemy agents
    5. Moving enemy agents
    6. Procedural programming
    7. Time for action – referencing the patrol locations 
    8. Time for action – moving the enemy
    9. Time for action – patrolling continuously between locations
    10. Enemy game mechanics
    11. Seek and destroy
    12. Time for action – changing the agent's destination
    13. Time for action – lowering player health
    14. Time for action – detecting bullet collisions
    15. Time for action – updating the game manager
    16. Refactoring and keeping it DRY
    17. Time for action – creating a restart method
    18. Hero's trial – refactoring win/lose logic
    19. Summary
    20. Pop quiz – AI and navigation
  16. Revisiting Types, Methods, and Classes
    1. Access Modifier redux
    2. Constant and read-only properties
    3. Using the static keyword 
    4. Time for action – creating a static class
    5. Methods redux
    6. Overloading methods
    7. Time for action – overloading the level restart
    8. Ref parameters
    9. Time for action – tracking player restarts
    10. Out parameters
    11. OOP redux
    12. Interfaces
    13. Time for action – creating a manager interface
    14. Time for action – adopting an interface
    15. Abstract classes
    16. Class extensions
    17. Time for action – extending the string class
    18. Time for action – using an extension method
    19. Namespace redux
    20. Type aliasing
    21. Summary
    22. Pop quiz – leveling up
  17. Introducing Stacks, Queues, and HashSets
    1. Introducing stacks
    2. Basic syntax
    3. Time for action – storing collected items
    4. Popping and peeking
    5. Time for action – the last item collected
    6. Common methods
    7. Working with queues
    8. Basic syntax
    9. Adding, removing, and peeking
    10. Common methods
    11. Using HashSets
    12. Basic syntax
    13. Performing operations
    14. Summary
    15. Pop quiz – intermediate collections
  18. Exploring Generics, Delegates, and Beyond
    1. Introducing generics
    2. Generic objects
    3. Time for action – creating a generic collection
    4. Generic methods
    5. Time for action – adding a generic item
    6. Constraining type parameters
    7. Time for action – limiting generic elements
    8. Delegating actions
    9. Basic syntax
    10. Time for action – creating a debug delegate
    11. Delegates as parameter types
    12. Time for action – using a delegate argument
    13. Firing events
    14. Basic syntax
    15. Time for action – creating an event
    16. Handling event subscriptions
    17. Time for action – subscribing to an event
    18. Handling exceptions
    19. Throwing exceptions
    20. Time for action – checking negative scene indexes
    21. Using try-catch
    22. Time for action – catching restart errors
    23. Design pattern primer
    24. Common game patterns
    25. Summary
    26. Pop quiz – intermediate C#
  19. The Journey Continues
    1. Scratching the surface
    2. Remembering your object-oriented programming
    3. Approaching Unity projects
    4. Unity features we didn't cover
    5. Next steps
    6. C# resources
    7. Unity resources
    8. Unity certifications
    9. Hero's trial – putting something out into the world
    10. Summary
  20. Pop Quiz Answers
    1. Chapter 1 – Getting to Know Your Environment
    2. Pop quiz – dealing with scripts
    3. Chapter 2 – The Building Blocks of Programming
    4. Pop quiz – C# building blocks
    5. Chapter 3 – Diving into Variables, Types, and Methods
    6. Pop quiz #1 – variables and types
    7. Pop quiz #2 – understanding methods
    8. Chapter 4 – Using Collections and Controlling Your Code
    9. Pop quiz #1 – if, and, or but
    10. Pop quiz #2 – all about collections
    11. Chapter 5 – Working with Classes, Structs, and OOP
    12. Pop quiz – all things OOP
    13. Chapter 6 – Getting Your Hands Dirty with Unity
    14. Pop quiz – basic Unity features
    15. Chapter 7 – Movement, Camera Controls, and Collisions
    16. Pop quiz – player controls and physics
    17. Chapter 8 – Scripting Game Mechanics
    18. Pop quiz – working with mechanics
    19. Chapter 9 – Basic AI and Enemy Behavior
    20. Pop quiz – AI and navigation
    21. Chapter 10 – Revisiting Types, Methods, and Classes
    22. Pop quiz – leveling up 
    23. Chapter 11 – Introducing Stacks, Queues, and HashSets
    24. Pop quiz – intermediate collections
    25. Chapter 12 – Exploring Generics, Delegates, and Beyond
    26. Pop quiz – intermediate C#
  21. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Learning C# by Developing Games with Unity 2020 - Fifth Edition
  • Author(s): Harrison Ferrone
  • Release date: August 2020
  • Publisher(s): Packt Publishing
  • ISBN: 9781800207806