C# Game Programming Cookbook for Unity 3D, 2nd Edition

Book description

This second edition of C# Game Programming Cookbook for Unity 3D expounds upon the first with more details and techniques. With a fresh array of chapters, updated C# code and examples, Jeff W. Murray’s book will help the reader understand structured game development in Unity unlike ever before.

New to this edition is a step-by-step tutorial for building a 2D infinite runner game from the framework and scripts included in the book. The book contains a flexible and reusable framework in C# suitable for all game types. From game state handling to audio mixers to asynchronous scene loading, the focus of this book is building a reusable structure to take care of many of the most used systems.

Improve your game's sound in a dedicated audio chapter covering topics such as audio mixers, fading, and audio ducking effects, or dissect a fully featured racing game with car physics, lap counting, artificial intelligence steering behaviors, and game management. Use this book to guide your way through all the required code and framework to build a multi-level arena blaster game.

Features

  • Focuses on programming, structure, and an industry-level, C#-based framework
  • Extensive breakdowns of all the important classes
  • Example projects illustrate and break down common and important Unity C# programming concepts, such as coroutines, singletons, static variables, inheritance, and scriptable objects.
  • Three fully playable example games with source code: a 2D infinite runner, an arena blaster, and an isometric racing game
  • The script library includes a base Game Manager, timed and proximity spawning, save profile manager, weapons control, artificial intelligence controllers (path following, target chasing and line-of-sight patrolling behaviors), user interface Canvas management and fading, car physics controllers, and more.

Code and screenshots have been updated with the latest versions of Unity. These updates will help illustrate how to create 2D games and 3D games based on the most up-to-date methods and techniques. Experienced C# programmers will discover ways to structure Unity projects for reusability and scalability. The concepts offered within the book are instrumental to mastering C# and Unity.

In his game career spanning more than 20 years, Jeff W. Murray has worked with some of the world's largest brands as a Game Designer, Programmer, and Director. A Unity user for over 14 years, he now works as a consultant and freelancer between developing his own VR games and experiments with Unity.

Table of contents

  1. Cover
  2. Half Title
  3. Title Page
  4. Copyright Page
  5. Dedication
  6. Table of Contents
  7. Acknowledgments
  8. Introduction
  9. Prerequisites
  10. Chapter 1: Making Games in a Modular Way
    1. 1.1 Important Programming Concepts
      1. 1.1.1 Manager and Controller Scripts
        1. 1.1.1.1 Managers
        2. 1.1.1.2 Controllers
        3. 1.1.1.3 Communication between Classes
        4. 1.1.1.4 Public Static
        5. 1.1.1.5 Private Static
      2. 1.1.2 The Singleton
        1. 1.1.2.1 Using the Singleton in Unity
      3. 1.1.3 Inheritance
      4. 1.1.4 Coroutines
      5. 1.1.5 Namespaces
      6. 1.1.6 Finite State Machines (FSM)
    2. 1.2 Naming Conventions
    3. 1.3 Where to Now?
  11. Chapter 2: Making a 2D Infinite Runner Game
    1. 2.1 Anatomy of a Unity Project
    2. 2.2 Open Unity, Then the Example Project for This Chapter
      1. 2.2.1 A Few Notes on the Example Project
      2. 2.2.2 Sprites
      3. 2.2.3 Animation
    3. 2.3 Open the Main Game Scene
    4. 2.4 The Game Manager
    5. 2.5 Making Platforms to Run On
    6. 2.6 Building the Player, RunMan
      1. 2.6.1 Adding the RunMan Sprite
      2. 2.6.2 Adding Physics and Collisions to RunMan
      3. 2.6.3 Player Scripts
      4. 2.6.4 RunMan Sounds
      5. 2.6.5 Animating RunMan
      6. 2.6.6 Scoring
    7. 2.7 The Game Loop and the Game Manager
      1. 2.7.1 Game States
      2. 2.7.2 The Main Loop of RunMan_GameManager.cs
      3. 2.7.3 Setting Up the RunMan_GameManager Component in the Editor
    8. 2.8 The Demise of RunMan
    9. 2.9 Adding the User Interface
      1. 2.9.1 Main Menu Setup
  12. Chapter 3: Building the Core Game Framework
    1. 3.1 Controllers and Managers
      1. 3.1.1 Managers
      2. 3.1.2 Controllers
    2. 3.2 Core Framework Scripts
      1. 3.2.1 BaseGameManager
      2. 3.2.2 ExtendedCustomMonoBehaviour
      3. 3.2.3 BaseCameraController
  13. Chapter 4: Player Structure
    1. 4.1 A Player Controller
    2. 4.2 Dealing with Input
    3. 4.3 User Data
      1. 4.3.1 The UserData Class
      2. 4.3.2 The BaseUserManager Class
    4. 4.4 The BasePlayerStatsController Class
    5. 4.5 Managing Players
  14. Chapter 5: Recipes Common Components
    1. 5.1 Introduction
    2. 5.2 AI
    3. 5.3 Camera
      1. 5.3.1 Third-Person Camera
      2. 5.3.2 Top-Down Camera
    4. 5.4 Game Control
      1. 5.4.1 GlobalRaceManager
      2. 5.4.2 RaceController
    5. 5.5 Input
      1. 5.5.1 Mouse Input
    6. 5.6 Level Loading
    7. 5.7 ScriptableObjects
      1. 5.7.1 ProfileScriptableObject
    8. 5.8 Spawning
      1. 5.8.1 A Spawner Component
      2. 5.8.2 Trigger Spawning
      3. 5.8.3 Timed Spawning
    9. 5.9 User Interface
      1. 5.9.1 CanvasManager
      2. 5.9.2 ScreenAndAudioFader
      3. 5.9.3 MenuWithProfiles
    10. 5.10 Utility
      1. 5.10.1 AlignToGround
      2. 5.10.2 AutomaticDestroyObject
      3. 5.10.3 AutoSpinObject
      4. 5.10.4 FaceCamera
      5. 5.10.5 LookAtCamera
      6. 5.10.6 PretendFriction
      7. 5.10.7 TimerClass
        1. 5.10.7.1 Modifying the Timer to Update Automatically
      8. 5.10.8 WaypointsController
    11. 5.11 Weapons
  15. Chapter 6: Player Movement Controllers
    1. 6.1 Humanoid Character
    2. 6.2 Wheeled Vehicle
    3. 6.3 Wheel Alignment
    4. 6.4 2D Platform Character Controller
  16. Chapter 7: Weapon Systems
    1. 7.1 Building the Scripts
      1. 7.1.1 The BaseWeaponController Class
      2. 7.1.2 BaseWeaponScript
      3. 7.1.3 Switching between Weapons
  17. Chapter 8: Waypoints Manager
    1. 8.1 Waypoint System
  18. Chapter 9: Recipe Sound and Audio
    1. 9.1 AudioMixers and Mixing Audio in Unity
      1. 9.1.1 Mixing Audio
      2. 9.1.2 Exposing Mixer Properties to Modify Them with Scripts
      3. 9.1.3 Audio Effects
    2. 9.2 The BaseSoundManager Class
    3. 9.3 Adding Sound to Weapons
  19. Chapter 10: AI Manager
    1. 10.1 AI States
    2. 10.2 The BaseAIController Class – Base AI Control
    3. 10.3 Enemy Stats
    4. 10.4 The AIBotController Class – Making an Enemy Bot Controller
    5. 10.5 The AISteeringController Class – An AI Steering Controller
    6. 10.6 Adding Weapon Control to the AI Controller
      1. 10.6.1 Attack States
      2. 10.6.2 BaseEnemyWeaponController
  20. Chapter 11: Menus and User Interface
    1. 11.1 CanvasManager Class/Canvas Management
    2. 11.2 The Main Menu
      1. 11.2.1 Designing the Main Menu
      2. 11.2.2 The BaseMainMenuManager Script
      3. 11.2.3 Combining BaseMainMenuManager to Use the ProfileManager Script for Saving and Loading Audio Settings
    3. 11.3 In-Game User Interface
  21. Chapter 12: Saving and Loading Profiles
    1. 12.1 The Save Profile ScriptableObject
    2. 12.2 The BaseProfileManager Class
  22. Chapter 13: Loading Level Scenes
    1. 13.1 A Script for Loading Levels: LevelLoader.cs
    2. 13.2 Multiple Level Loading
    3. 13.3 Building a Multi-Scene Level Loader in Unity
  23. Chapter 14: Racing Game
    1. 14.1 Ingredients
    2. 14.2 Main Menu Scene Overview
    3. 14.3 Level Select Scene Overview
    4. 14.4 The Core Scene Overview
      1. 14.4.1 Game Manager – The RaceGameManager Class
      2. 14.4.2 Race Control – The GlobalRaceManager Class
      3. 14.4.3 Race Control – The RaceController Class
      4. 14.4.4 Race Control – RacePlayerController Class
      5. 14.4.5 Audio
        1. 14.4.5.1 Vehicle Audio
        2. 14.4.5.2 Incidental Sounds
      6. 14.4.6 User Interface
  24. Chapter 15: Blaster Game Example
    1. 15.1 Ingredients
    2. 15.2 Main Menu Scene Overview
    3. 15.3 The Core Scene Overview
      1. 15.3.1 The BlasterGameManager Class
      2. 15.3.2 The BlasterPlayer Class
      3. 15.3.3 Enemy Bots and the BlasterEnemyBot Class
      4. 15.3.4 Weapons and Projectiles
      5. 15.3.5 User Interface
    4. 15.4 Level Scenes
      1. 15.4.1 Spawning Enemies
  25. Index

Product information

  • Title: C# Game Programming Cookbook for Unity 3D, 2nd Edition
  • Author(s): Jeff W. Murray
  • Release date: March 2021
  • Publisher(s): CRC Press
  • ISBN: 9781000359725