The EventHander adds a generic event system to the character controller. This system is preferred internally over Unity’s event system because it doesn’t allocate any garbage when executing. It is also easier to extend and events can be bound to a specific object or can be executed globally. As with any robust event system, multiple objects can be subscribed to the same event. The following method definition is used to listen for an event:

EventHandler.RegisterEvent(object obj, string name, Action handler)

This function is overloaded so events with up to five parameters can also be sent:

EventHandler.RegisterEvent<int>(object obj, string name, Action<int> handler)

Unregistering from an event is very similar to registering for an event:

EventHandler.UnregisterEvent<int>(object obj, string name, Action<int> handler)

An event can then be executed with:

EventHandler.ExecuteEvent(object obj, string name)

If the event is a global event then the first parameter (object obj) is omitted.  As an example lets have an object listen for the “OnDeath” event. The entire component looks like:

using UnityEngine;
using Opsive.Shared.Events;

public class MyComponent : MonoBehaviour
    public void Awake()
        // Register for the event when the component starts. The event registers with the local GameObject (the first parameter) so 
        // the component must be added to the same GameObject as the object that is sending the "OnDeath" event.
        EventHandler.RegisterEvent<Vector3, Vector3, GameObject>(gameObject, "OnDeath", OnDeath);

    /// <summary>
    /// Receives the "OnDeath" event.
    /// </summary>
    private void OnDeath(Vector3 position, Vector3 force, GameObject attacker)
        Debug.Log("The object " + gameObject + " died.");

    public void OnDestroy()
        // Unregister from the event when the component is no longer interested in it. In this example the component is interested for the lifetime of 
        // the component (Awake -> OnDestroy). 
        EventHandler.UnregisterEvent<Vector3, Vector3, GameObject>(gameObject, "OnDeath", OnDeath);