C# Game Programming Cookbook for Unity 3D
eBook - ePub

C# Game Programming Cookbook for Unity 3D

  1. 296 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

C# Game Programming Cookbook for Unity 3D

About this book

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.

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription.
No, books cannot be downloaded as external files, such as PDFs, for use outside of Perlego. However, you can download books within the Perlego app for offline reading on mobile or tablet. Learn more here.
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Yes! You can use the Perlego app on both iOS or Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app.
Yes, you can access C# Game Programming Cookbook for Unity 3D by Jeff W. Murray in PDF and/or ePUB format, as well as other popular books in Computer Science & Computer Science General. We have over one million books available in our catalogue for you to explore.

1

Making Games in a
Modular Way

1.1Important Programming Concepts

There are some things you will need to know to utilize this book fully. For a lot of programmers, these concepts may be something you were taught in school. As a self-taught coder who did not find out about a lot of these things until long after I had been struggling on without them, I see this section as a compilation of ‘things I wish I’d known earlier’!

1.1.1 Manager and Controller Scripts

Sometimes, even I get confused about the difference between a manager and a controller script. There is no established protocol for this, and it varies depending on who you ask. Like some readers of the first edition of this book, I find it a little confusing and I will try to clear that up.

1.1.1.1 Managers

Managers deal with overall management, in a similar way to how a Manager would work in a workplace situation.

1.1.1.2 Controllers

Controllers deal with systems that the managers need to do their jobs. For example, in the racing game example game for this book, we have race controller scripts and a global race manager script. The race controller scripts are attached to the players and track their positions on the track, waypoints, and other relevant player-specific race information. The global race manager script talks to all the race controller scripts attached to the players to determine who is winning and when the race starts or finishes.

1.1.1.3 Communication between Classes

An important part of our modular structure is how scripts are going to communicate with each other. It is inevitable that we will need to access our scripts from different areas of the game code, so you will need to know how to do that in Unity.
  1. Direct referencing scripts via variables set in the editor by the Inspector window.
The easiest way to have script Components talk to each other (that is, scripts attached to GameObjects in the Scene as Components) is to have direct references, in the form of public variables within your code. They can then be populated in the Inspector window of the Unity editor with a direct link to another Component on another GameObject.
  1. GameObject referencing using SendMessage.
SendMessage sends a message to a GameObject or Transform and calls a function within any of its attached script Components. It only works one way, so it is only useful when we do not need any kind of return result. For example:
 someGameObject.SendMessage(“DoSomething”);
Above, this would call the function DoSomething() on any of the script Components attached to the GameObject referenced by someGameObject.
  1. Static variables.
The static variable type makes a variable accessible to other scripts without a direct reference to the GameObject it is attached to. This is particularly useful behavior where several different scripts may want to manipulate a variable to do things like adding points to a score or set the number of lives of a player, and so on.
An example declaration of a static variable might be:
 public static GameManager aManager;
You can have private or public static variables:

1.1.1.4 Public Static

A public static variable exists everywhere and may be accessed by any other script.
For example, imagine a player script which needs to tell the Game Manager to increase the current score by one:
  1. In our GameManager.cs Game Manager script, we set up a variable, public static typed:
 public static gameScore;
  1. In any other script, we can now access this static variable and alter the score as needed:
 GameController.gameScore++;

1.1.1.5 Private Static

A private static variable is accessible only to any other instances of the same type of class. Other types of class will not be able to access it.
As an example, imagine all players need to have a unique number when they are first spawned into the game. The player class declares a variable named uniqueNum like this:
 Private static int uniqueNum;
When a player script is first created, it uses the value of uniqueNum for itself and increases uniqueNum by one:
 myUniqueNum = uniqueNum; uniqueNum++;
The value of uniqueNum will be shared across all player scripts. The next player to be spawned will run the same start up function, getting uniqueNum again – only this time it has been increased by one, thanks to the player spawned earlier. This player again gets its own unique number and increases the static variable ready for the next one.

1.1.2 The Singleton

A singleton is a commonly used method for allowing access to an instance of a class, accessible to all other scripts. This pattern is ideal for code that needs to communicate with the entire game, such as a Game Manager.

1.1.2.1 Using the Singleton in Unity

The most straightforward way to achieve singleton-like behavior is to use a static variable and, when Unity calls the Awake() function, set the instance variable to become a reference to the script instance it represents. The keyword ‘this’ refers to the instance of the script it is a part of.
For example:
 public class MyScript : Monobehaviour { private static MySingleton instance; void Awake() { if (instance != null) Destroy(this); instance = this; } }
The singleton instance of our script can be accessed anywhere, by practically any script, with the following syntax:
 MySingleton.Instance.SomeFunctionInTheSingleton();

1.1.3 Inheritance

Inheritance is a complex...

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
  11. Chapter 2: Making a 2D Infinite Runner Game
  12. Chapter 3: Building the Core Game Framework
  13. Chapter 4: Player Structure
  14. Chapter 5: Recipes Common Components
  15. Chapter 6: Player Movement Controllers
  16. Chapter 7: Weapon Systems
  17. Chapter 8: Waypoints Manager
  18. Chapter 9: Recipe Sound and Audio
  19. Chapter 10: AI Manager
  20. Chapter 11: Menus and User Interface
  21. Chapter 12: Saving and Loading Profiles
  22. Chapter 13: Loading Level Scenes
  23. Chapter 14: Racing Game
  24. Chapter 15: Blaster Game Example
  25. Index