Reader small image

You're reading from  Game Development Patterns with Unity 2021 - Second Edition

Product typeBook
Published inJul 2021
Reading LevelBeginner
PublisherPackt
ISBN-139781800200814
Edition2nd Edition
Languages
Tools
Right arrow
Author (1)
David Baron
David Baron
author image
David Baron

David Baron is a game developer with over 15 years of experience in the industry. He has worked for some well-known AAA, mobile, and indie game studios in Montreal, Canada. His skill set includes programming, design, and 3D art. As a programmer, he has worked on various games for various platforms, including virtual reality, mobile, and consoles.
Read more about David Baron

Right arrow
A Short Primer to Programming in Unity

This chapter is a short primer to help you get familiar with advanced C# language and Unity engine features. I'm not going to attempt to explain in depth any of the subject matter presented here because it's beyond the scope of this book. Still, I will at least introduce you to some core concepts to avoid confusion when they are referenced in the upcoming chapters. I encourage those that have advanced knowledge of C# and Unity programming to skip this chapter. But I recommend beginners and intermediate developers take the time to review the content of this chapter to get a general idea of the language and engine features we will use to implement and design our game systems.

In all cases, complete mastery over C# and Unity is not needed to comprehend this book, just general awareness and familiarity with some advanced critical...

What you should already know

In this section, I'm listing some core C# language and Unity engine features that you should already be familiar with before continuing to more advanced parts of this book.

The following are some core features of C#:

  • Familiarity with class access modifiers such as public and private
  • Fundamental knowledge of basic primitive data types (int, string, bool, float, and arrays)
  • A conceptual understanding of inheritance and the relation between a base class and a derived class

The following are some core features of Unity:

  • A basic understanding of how to write a MonoBehaviour script and attach it to a GameObject as a component
  • Ability to create a new Unity Scene from scratch and manipulate GameObjects inside the editor
  • Familiarity with Unity's basic event functions (Awake, Start, Update) and their sequence of execution 

If you are not familiar with the concepts listed previously, I would recommend reading the books and documentation that are listed...

C# language features

Languages features such as events and delegates might be too advanced for beginners, so if you consider yourself in that category, don't worry; you can still enjoy this book. Just read the beginner-level chapters such as the ones that explain patterns such as Singleton, State, Facade, and Adapter.

The following C# advanced language features are fundamental to the optimal implementation of some design patterns that we will be implementing in the upcoming chapters:

  • Static: Methods and members of a class with the static keyword can be accessed directly with its name and without initializing an instance. Static methods and members are helpful because they are easily accessible from anywhere in your code. The following example showcases a class that uses the keyword to establish a globally accessible event bus:
using UnityEngine.Events;
using System.Collections.Generic;

namespace Chapter.EventBus
{
public class RaceEventBus
{
private static readonly
...

Unity engine features 

Unity is a fully featured engine, including a comprehensive scripting API, an animation system, and many additional features for game development. We can't cover them all in this book, so I will only list the core Unity components that we will be using in the upcoming design pattern chapters:

  • Prefabs: A prefab is a prefabricated container of assembled GameObjects and components. For example, you can have individual prefabs for each type of vehicle in your game and dynamically load them in your scene. Prefabs permit you to construct and organize reusable game entities as building blocks.
  • Unity Events and Actions: Unity has a native event system; it's very similar to the C# event system but with extra engine-specific features, such as the ability to view and configure them in the Inspector.
  • ScriptableObjects: A class that derives from the ScriptableObject base class can act as a data container. The other native Unity base class, named MonoBehaviour...

Summary

This chapter is intended to be used as a primer to set up a shared knowledge base before moving along to the book's hands-on section. But mastery over the features presented in the previous sections is not required to start using design patterns in Unity. We will review some of them again in more detail in upcoming chapters, this time in a practical context.

In the next chapter, we are going to review our first pattern, the infamous Singleton. We will use it to implement a Game Manager class responsible for initializing the game.

Further reading

For more information, you can refer to the following material:

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Game Development Patterns with Unity 2021 - Second Edition
Published in: Jul 2021Publisher: PacktISBN-13: 9781800200814
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
David Baron

David Baron is a game developer with over 15 years of experience in the industry. He has worked for some well-known AAA, mobile, and indie game studios in Montreal, Canada. His skill set includes programming, design, and 3D art. As a programmer, he has worked on various games for various platforms, including virtual reality, mobile, and consoles.
Read more about David Baron