Message registration

The following code contains a pair of simple classes that register with the messaging system, each requesting to have one of their methods called whenever certain types of messages have been broadcast from anywhere in our code base:

public class EnemyManagerWithMessagesComponent : MonoBehaviour {  private List<GameObject> _enemies = new List<GameObject>();  [SerializeField] private GameObject _enemyPrefab;  void Start() {    MessagingSystem.Instance.AttachListener(typeof(CreateEnemyMessage),                                             this.HandleCreateEnemy);  }  bool HandleCreateEnemy(Message msg) {    CreateEnemyMessage castMsg = msg as CreateEnemyMessage;    string[] names = { "Tom", "Dick", "Harry" };    GameObject enemy = GameObject.Instantiate(_enemyPrefab,  5.0f * Random.insideUnitSphere, ...

Get Unity Game Optimization - Third Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.