I really wanted a way to pick a callback function for my script, from within the editor. But, I didn’t want to add an event trigger component, and even if I did, I’d have to bastardize one of the existing events for my custom purposes.
I searched around first, to no avail. Since I found nothing, I figured I’d post what I eventually figured out here.
First we add:
#using UnityEngine.EventSystems;
#using UnityEngine.Events;
to the top of the monobehavior’s script
Then add a public memeber to the class:
public EventTrigger.TriggerEvent customCallback;
After compiling, we can now see the option to select a (list of) functions, in the editor. It looks just like what you see when working with an event trigger component. But, notice these are INSIDE the monobehavior component, titled “customCallback”, and will not be executed unless we say so.
Elsewhere in the class we want to actually execute this function, which we do like this:
customCallback.Invoke(eventData);
As far as how to get, eventData, it doesn’t really matter. You can pass along a received event’s eventData, or create one yourself like this:
BaseEventData eventData= new BaseEventData(EventSystem.current);
eventData.selectedObject=this.gameObject;
Note whatever custom callback function we want to select in the editor, must be of the form:
void foo(BaseEventData);