Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Game Development Patterns with Unity 2021

You're reading from   Game Development Patterns with Unity 2021 Explore practical game development using software design patterns and best practices in Unity and C#

Arrow left icon
Product type Paperback
Published in Jul 2021
Publisher Packt
ISBN-13 9781800200814
Length 246 pages
Edition 2nd Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
David Baron David Baron
Author Profile Icon David Baron
David Baron
Arrow right icon
View More author details
Toc

Table of Contents (22) Chapters Close

Preface 1. Sections 1: Fundamentals
2. Before We Begin FREE CHAPTER 3. The Game Design Document 4. A Short Primer to Programming in Unity 5. Section 2: Core Patterns
6. Implementing a Game Manager with the Singleton 7. Managing Character States with the State Pattern 8. Managing Game Events with the Event Bus 9. Implement a Replay System with the Command Pattern 10. Optimizing with the Object Pool Pattern 11. Decoupling Components with the Observer Pattern 12. Implementing Power-Ups with the Visitor Pattern 13. Implementing a Drone with the Strategy Pattern 14. Using the Decorator to Implement a Weapon System 15. Implementing a Level Editor with Spatial Partition 16. Section 3: Alternative Patterns
17. Adapting Systems with an Adapter 18. Concealing Complexity with a Facade Pattern 19. Managing Dependencies with the Service Locator Pattern 20. About Packt 21. Other Books You May Enjoy

Testing the Race Event Bus

Now that we have the core elements of the pattern in place, we can write some code to test our RaceEventBus class. For reasons of brevity, I have removed all the behavior code in each client class to focus on the use of the pattern:

  1. For starters, we are going to write a countdown timer that subscribes to the COUNTDOWN race event type. Once the COUNTDOWN event is published, it will trigger a 3-second countdown to the start of the race. And at the exact moment the count reaches its end, it will publish the START event to signal the beginning of the race:
using UnityEngine;
using System.Collections;

namespace Chapter.EventBus
{
public class CountdownTimer : MonoBehaviour
{
private float _currentTime;
private float duration = 3.0f;

void OnEnable() {
RaceEventBus.Subscribe(
RaceEventType.COUNTDOWN, StartTimer);
}

void OnDisable() {
RaceEventBus.Unsubscribe(
RaceEventType.COUNTDOWN...
lock icon The rest of the chapter is locked
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.
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 $19.99/month. Cancel anytime
Banner background image