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.

  1. Add and Remove Listeners:

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

  1. Invoke the Event:

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

Example Usage:

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