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 codebase:

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 2017 Game Optimization - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.