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:
Generic Design:
ConnectionEvent
is designed with generics to support events with different parameter types (T1
andT2
).The class is available in three variations:
ConnectionEvent<T1, T2>
,ConnectionEvent<T>
, andConnectionEvent
.
Event Handling:
Provides methods (
AddListener
andRemoveListener
) to attach and detach listeners to the event.Supports multiple listeners for an event.
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.
Debug Logging:
Supports optional debug logging, controlled by the
isDebug
parameter.Logs information about the event when it is called.
How to Use:
Instantiate the Event:
Create an instance of
ConnectionEvent
or its variations in a script or component.
Add and Remove Listeners:
Attach and detach methods to the event using
AddListener
andRemoveListener
.
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 thelogString
parameter to the calling method's name, providing additional context in debug logs.
Last updated