NotificationCenterGenerics

Author: capnbishop

Update: chrisg, Marcus_Mattsson

Update Notes
The original script breaks with #pragma strict or mobile development since it's using dynamic typing for the observer, so this script uses a Generic List of Components instead of a type-less ArrayList. Code changed, but the rest is copied from the original page NotificationCenter. A C# port has been added to the bottom of the page.

Description
NotificationCenterGenerics allows for easy and powerful messaging between objects in Unity. An object can register as an observer and receive notifications of a certain type when they occur. When that notification type is posted elsewhere, all objects that registered as an observer for that notification type will receive a message that it has occurred, along with the associated data.

This system allows for objects to communicate their status and events with one another, without having to directly reference each object that receives the notification. An object can simply post an event when it occurs, and any interested party can register to receive notification when it happens.

Using a notification center will allow for dynamic and flexible coding practices. For example, a pinball game can have a bumper that posts a notification when the ball collides with it. An object that keeps track of score can register to receive a notification when this event occurs, and increment the score accordingly without having to communicate directly with or keep track of the bumper. Another object can play a sound when it receives notification of a bumper impact. Another object can display a particle effect when it receives notification of a bumper impact. Other features can be added to respond to this event later on without having to modify the existing objects, because they independently choose to receive and act on the notification event.

This script was inspired by Blurst Technology's Messenger system, with a convention twist to make it easier to use and a little more similar to the Cocoa framework's NSNotificationCenter.

Usage
This is an auto-instantiating singleton class. There is no need to attach it to an object. Simply place it in a project's assets and use it by calling to the DefaultCenter. The static method will automatically create a game object, add a NotificationCenter component, and create a reference to that component.

Post a notification by calling the PostNotification method:

The PostNotification method comes in several varieties, all of which are valid. It only requires two parameters: the sender, and the notification name. Optionally it can accept a miscellaneous data object. This can be anything that needs to be communicated. For instance, the OnBumperCollision notification could send information about the collision, or even send the Collision object itself. Often the data object is best used as a Hashtable, so that it can include a variety of keyed information.

Notice that PostNotification can be called by either passing individual parameters, or by passing a Notification object. The Notification object can be subclassed to make a streamlined set of notification data. The Notification object also must hold the sender, the notification name, and (optionally) a data object.

Register to receive notifications by calling the AddObserver method:

DefaultCenter is a static method that returns an instance of the default notification center. If it doesn't exist yet, it will be automatically instantiated. AddObserver accepts two parameters for the object that wishes to receive notifications, and the notification to be received. The notification that's being registered is the function call that will be executed for the event.

Remove an observer with the RemoveObserver method:

RemoveObserver simply removes the object from the list of observers for the specified notification event.

To receive notification, simply implement a method with the notification name:

Notification methods must accept the Notification object. It contains the three properties for sender, name (of the notification), and data (optional object).