Messenger

From Headbone Creative
Jump to: navigation, search

The messenger framework is a lightweight system that uses C# delegates and callbacks to send messages without having to rely on GameObject.SendMessage.

This code is originally from here: Advanced C# Messenger

All of the functions of this class are static.

Contents

Helper Functions

void MarkAsPermanent (string eventType)

This tells the class not to unregister the event when the list of events is cleaned up.

void Cleanup ()

This removes all events from the list, unless they were marked as permanent.

void PrintEventTable ()

This prints all events to the debug console. It only has any effect in the Unity Editor.

Add Listener Functions

These add the function to the list. When the corresponding eventType string is broadcast with the correct number of parameters, the handler function will be called.

void AddListener(string eventType, Callback handler)

This adds a listener for a function with 0 parameters.

void AddListener<T>(string eventType, Callback<T> handler)

This adds a listener for a function with 1 parameter.

void AddListener<T, U>(string eventType, Callback<T, U> handler)

This adds a listener for a function with 2 parameters.

void AddListener<T, U, V>(string eventType, Callback<T, U, V> handler)

This adds a listener for a function with 3 parameters.

Remove Listener Functions

These remove the function from the list, preventing it from being called.

void RemoveListener(string eventType, Callback handler)

This removes a listener for a function with 0 parameters. The handler passed in should be the same one passed for this event to AddListener.

void RemoveListener<T>(string eventType, Callback<T> handler)

This removes a listener for a function with 1 parameter. The handler passed in should be the same one passed for this event to AddListener.

void RemoveListener<T, U>(string eventType, Callback<T, U> handler)

This removes a listener for a function with 2 parameters. The handler passed in should be the same one passed for this event to AddListener.

void RemoveListener<T, U, V>(string eventType, Callback<T, U, V> handler)

This removes a listener for a function with 3 parameters. The handler passed in should be the same one passed for this event to AddListener.

Broadcast Functions

These functions are called to post the event. All listeners with the same eventType string and number of parameters will have their handler functions called. If no handler functions were found, an exception will be raised.

void Broadcast(string eventType)

Posts an event with 0 parameters.

void Broadcast<T>(string eventType, T arg1)

Posts an event with 1 parameter.

void Broadcast<T, U>(string eventType, T arg1, U arg2)

Posts an event with 2 parameters.

void Broadcast<T, U, V>(string eventType, T arg1, U arg2, V arg3)

Posts an event with 3 parameters.

Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox