Skip to content

Events Extending

Richard Kopelow edited this page Aug 25, 2018 · 4 revisions

Events - Extending

Extending the event system to make events that pass around any datatype is simple, you need only extend four classes.

Unity Event

Unity Events are used by the Event Handler classes as a convenient way to connect events to script functionality.

  1. Inherit from UnityEvent<T> with T being the type you want.
public class ByteUnityEvent : UnityEvent<byte> {}

Event Backing

The Event Backing stores registered event handlers and triggers them all when the event is fired.

  1. Inherit from BaseEventBacking<T, Y> with T being the type you want to be the event argument, and Y being a UnityEvent of the same datatype.
  2. Give it a CreateAssetMenu attribute.
[CreateAssetMenu(fileName = "NewByteEvent", menuName = "Scriptable Objects/Events/Byte")]
public class ByteEvent : BaseEventHandler<byte, ByteUnityEvent> {}

Event Handler

The Event Handler is a script added to Game Objects that are meant to receive events. They act a the connection between Event Backings and the code that should execute on the event.

  1. Inherit from BaseEventHandler<T, Y, Z> with T being the argument type, Y being the Event Backing, and Z being the Unity Event.
public class ByteEventHandler : BaseEventHandler<byte, ByteEventBacking, ByteUnityEvent>

Event Field

The Event Field is what you define inside of a script to expose a reference to an event. It can be set to reference an Event Backing or directly reference a Handler.

  1. Inherit from BaseEvent<T, Y, Z> with T being the argument type, Y being the Event Backing, and Z being the Event Handler.
  2. Give it the Serializable attribute.
[Serializable]
public class ByteEvent : BaseEvent<byte, ByteEventBacking, ByteEventHandler> { }

Custom Property Drawer

Custom property drawers are used to clean up the look of Event Fields in the editor while maintaining the flexibility gained by using an Event Field rather than simply referencing the Event Backing.

  1. Define a class that inherits from EventDrawer. (This need only be done once, it can be used for all custom variables)
  2. Add a CustomPropertyDrawer attribute to the EventDrawer class.
[CustomPropertyDrawer(typeof(ByteEvent))]

Custom Inspector

Custom Inspectors are used to give the user the ability to trigger Event Backings manually from the editor with test values.

  1. Define a custom inspector for the Event Backing.
  2. Give it the CustomEditor attribute.
[CustomEditor(typeof(ByteEventBacking))]
public class ByteEventInspector : BaseEventInspector<byte> { }

Clone this wiki locally