Game Development Reference
In-Depth Information
A better way
To break this dependency between the game objects and the need to keep references or the
need to discover each other at the design or runtime stage, we need an intermediary that
all objects know about, that is, a Manager class.
With this manager class, it will manage the list of game objects that want to listen to the
messages and provide an easy way to notify anyone who's listening.
To implement this, we will use the singleton behavior described earlier by creating three
simple, reusable components as a test case.
First, we create the manager class itself. So, create a MessagingManager.cs C#
script and then replace its contents as follows:
using System;
using System.Collections.Generic;
using UnityEngine;
public class MessagingManager : MonoBehaviour
{
//Static singleton property
public static MessagingManager Instance { get; private
set; }
// public property for manager
private List<Action> subscribers = new List<Action>();
}
The first property is the singleton instance for the manager class, while the second is a
list of delegates that will be used to keep track of who needs to be notified.
Next, we add the Awake function to initialize the singleton approach:
void Awake()
{
Debug.Log("Messaging Manager Started");
//First, we check if there are any other instances
conflicting
Search WWH ::




Custom Search