ConnectionEvent

ConnectionEvent Overview:

ConnectionEvent is a generic event system in Unity that allows you to define and manage events with different parameter types. It provides a way to broadcast events and allows other components to listen for and respond to those events.

Key Features:

  1. Generic Design:

    • ConnectionEvent is designed with generics to support events with different parameter types (T1 and T2).

    • The class is available in three variations: ConnectionEvent<T1, T2>, ConnectionEvent<T>, and ConnectionEvent.

  2. Event Handling:

    • Provides methods (AddListener and RemoveListener) to attach and detach listeners to the event.

    • Supports multiple listeners for an event.

  3. Event Invocation:

    • The Call method is used to invoke the event, triggering all attached listeners.

    • Generic parameters (T1, T2) are passed to the listeners when the event is called.

  4. Debug Logging:

    • Supports optional debug logging, controlled by the isDebug parameter.

    • Logs information about the event when it is called.

How to Use:

  1. Instantiate the Event:

    • Create an instance of ConnectionEvent or its variations in a script or component.

// Example 1: ConnectionEvent with two generic parameters
ConnectionEvent<int, string> myEvent = new ConnectionEvent<int, string>(isDebug: true, logString: "MyEvent");

// Example 2: ConnectionEvent with a single generic parameter
ConnectionEvent<float> floatEvent = new ConnectionEvent<float>(isDebug: true, logString: "FloatEvent");

// Example 3: ConnectionEvent without generic parameters
ConnectionEvent noParamEvent = new ConnectionEvent(isDebug: true, logString: "NoParamEvent");
  1. Add and Remove Listeners:

    • Attach and detach methods to the event using AddListener and RemoveListener.

// Example 1: Add and remove listeners to ConnectionEvent with two generic parameters
myEvent.AddListener(MyMethod);
myEvent.RemoveListener(MyMethod);

// Example 2: Add and remove listeners to ConnectionEvent with a single generic parameter
floatEvent.AddListener(FloatMethod);
floatEvent.RemoveListener(FloatMethod);

// Example 3: Add and remove listeners to ConnectionEvent without generic parameters
noParamEvent.AddListener(NoParamMethod);
noParamEvent.RemoveListener(NoParamMethod);
  1. Invoke the Event:

    • Call the Call method to trigger the event and notify all attached listeners.

// Example 1: Invoke ConnectionEvent with two generic parameters
myEvent.Call(42, "Hello");

// Example 2: Invoke ConnectionEvent with a single generic parameter
floatEvent.Call(3.14f);

// Example 3: Invoke ConnectionEvent without generic parameters
noParamEvent.Call();

Example Usage:

public class MyComponent : MonoBehaviour
{
    // Example: Instantiate ConnectionEvent with two generic parameters
    private ConnectionEvent<int, string> myEvent = new ConnectionEvent<int, string>(isDebug: true, logString: "MyEvent");

    private void Start()
    {
        // Example: Add a listener to the event
        myEvent.AddListener(HandleMyEvent);
    }

    private void HandleMyEvent(int value, string message)
    {
        // Custom logic to handle the event
        Debug.Log($"Event Received - Value: {value}, Message: {message}");
    }

    private void Update()
    {
        // Example: Invoke the event
        if (Input.GetKeyDown(KeyCode.Space))
        {
            myEvent.Call(42, "Spacebar Pressed");
        }
    }
}

Benefits:

  • Generic design allows flexibility in handling events with different parameter types.

  • Supports clean and modular event handling in Unity scripts.

  • Debug logging aids in development and troubleshooting.

Considerations:

  • Ensure that listeners are properly added and removed to prevent memory leaks.

  • Use appropriate parameter types when defining and handling events.

  • Debug logging can be toggled on or off based on the isDebug parameter.

  • The CallerMemberName attribute is used to automatically set the logString parameter to the calling method's name, providing additional context in debug logs.

Last updated