Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Sending Custom Events via the EvenSystem
using System;
using System.Collections.Generic;
using UnityEngine.Events;
// interface you implement in your MB to receive events
public interface ICustomHandler : IEventSystemHandler
{
void OnCustomCode(CustomEventData eventData);
}
// Custom data we will send via the event system
public class CustomEventData : BaseEventData
{
public string m_CustomData;
public CustomEventData(EventSystem eventSystem, string customData)
: base(eventSystem)
{
m_CustomData = customData;
}
}
// container class that holds the execution logic
// called by the event system to delecate the call to
// the intercea
public static class MyCustomEvents
{
// call that does the mapping
private static void Execute(ICustomHandler handler, BaseEventData eventData)
{
// The ValidateEventData makes sure the passed event data is of the correct type
handler.OnCustomCode (ExecuteEvents.ValidateEventData<CustomEventData> (eventData));
}
// helper to return the functor that should be invoked
public static ExecuteEvents.EventFunction<ICustomHandler> customEventHandler
{
get { return Execute; }
}
}
//Custom input module that can send the events
public class MyInputModule : BaseInputModule
{
// list of objects to invoke on
public GameObject[] m_TargetObjects;
// called each tick on active input module
public override void Process()
{
// if we don't have targets return
if (m_TargetObjects == null || m_TargetObjects.Length == 0)
return;
// for each target invoke our custom event
foreach (var target in m_TargetObjects)
ExecuteEvents.Execute (target, new CustomEventData (eventSystem, "Custom Data"), MyCustomEvents.customEventHandler);
}
}
// Class that implements the Handler
public class MyCustomMB : MonoBehaviour, ICustomHandler
{
public void OnCustomCode(CustomEventData eventData)
{
Debug.Log (eventData.m_CustomData);
}
}

oferei commented Oct 5, 2015

Thanks a lot for this!

aggsol commented Nov 6, 2015

👍

On line 57, where the event is executed, where does 'eventSystem' come from? Where is it defined?

@sstrohkorb it is inherited from BaseInputModule

Can someone explain this to me more clearly like delegate-event? I can somewhat understand this by that kind point of view. The MyInputModule is the trigger; MyCustomEvent is the event; CustomEventData is the event data; ICustomHandler is the delegate i think and MyCustomMB is the listener right? If that so, where the heck ICustomHandler handler parameter in Excute method came from so the handler.OnCustomCode could be executed? as far as i know interface must be initialized first so it would know which method from which class will be executed.

MrFJ commented Sep 27, 2017

Thanks, this helped a lot! Look for "3D EventTrigger", or whatever I decide to call it, on the asset store (probably free) in a few weeks!
image

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment