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

C# Game Programming Cookbook for Unity 3D

Jeff W. Murray

Compartir libro
  1. 296 páginas
  2. English
  3. ePUB (apto para móviles)
  4. Disponible en iOS y Android
eBook - ePub

C# Game Programming Cookbook for Unity 3D

Jeff W. Murray

Detalles del libro
Vista previa del libro
Índice
Citas

Información del libro

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.

Preguntas frecuentes

¿Cómo cancelo mi suscripción?
Simplemente, dirígete a la sección ajustes de la cuenta y haz clic en «Cancelar suscripción». Así de sencillo. Después de cancelar tu suscripción, esta permanecerá activa el tiempo restante que hayas pagado. Obtén más información aquí.
¿Cómo descargo los libros?
Por el momento, todos nuestros libros ePub adaptables a dispositivos móviles se pueden descargar a través de la aplicación. La mayor parte de nuestros PDF también se puede descargar y ya estamos trabajando para que el resto también sea descargable. Obtén más información aquí.
¿En qué se diferencian los planes de precios?
Ambos planes te permiten acceder por completo a la biblioteca y a todas las funciones de Perlego. Las únicas diferencias son el precio y el período de suscripción: con el plan anual ahorrarás en torno a un 30 % en comparación con 12 meses de un plan mensual.
¿Qué es Perlego?
Somos un servicio de suscripción de libros de texto en línea que te permite acceder a toda una biblioteca en línea por menos de lo que cuesta un libro al mes. Con más de un millón de libros sobre más de 1000 categorías, ¡tenemos todo lo que necesitas! Obtén más información aquí.
¿Perlego ofrece la función de texto a voz?
Busca el símbolo de lectura en voz alta en tu próximo libro para ver si puedes escucharlo. La herramienta de lectura en voz alta lee el texto en voz alta por ti, resaltando el texto a medida que se lee. Puedes pausarla, acelerarla y ralentizarla. Obtén más información aquí.
¿Es C# Game Programming Cookbook for Unity 3D un PDF/ePUB en línea?
Sí, puedes acceder a C# Game Programming Cookbook for Unity 3D de Jeff W. Murray en formato PDF o ePUB, así como a otros libros populares de Informatik y Informatik Allgemein. Tenemos más de un millón de libros disponibles en nuestro catálogo para que explores.

Información

Editorial
CRC Press
Año
2021
ISBN
9781000359725
Edición
2
Categoría
Informatik

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...

Índice