Game Development Reference
In-Depth Information
A custom event Interface
The interface for your event is by far the simplest part of the puzzle and looks
like the following code. Just add this to the AlarmSystem script (after the
AlarmEventData class section) we created earlier:
// The interface you implement
// in your MonoBehaviours to receive events
public interface IAlarmHandler : IEventSystemHandler
{
void OnAlarmTrigger(AlarmEventData eventData);
}
Here we are simply defining:
• The name of the interface to be used on all classes implementing our alarm
system (will this also include turrets?)
Standards suggest that all interfaces are prefixed with the uppercase letter
I, so that they are easily distinguishable and easy to read.
• A simple method that will be used as the template in classes that implement
the interface (there can be more than one if you support multiple actions)
Now that this is ready, it can be used on classes in the project; however, next we
need to be able to handle the event.
A custom event static container
So that the Event System can call your custom event in the same way that it calls
the rest of the events in its control, you need some boilerplate code so it can use it
effectively, which looks like the following code. Add this after the IAlarmHandler
interface in the AlarmSystem script:
// container class that holds the execution logic that is called
// by the event system to delegate the call to the interface
public static class MyAlarmTriggerEvents
{
// call that does the mapping
private static void Execute(IAlarmHandler handler,
BaseEventData eventData)
{
 
Search WWH ::




Custom Search