Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to: select a callback function for your script, from the editor.

Discussion in 'Scripting' started by Glurth, Feb 3, 2015.

  1. Glurth

    Glurth

    Joined:
    Dec 29, 2014
    Posts:
    109
    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:

    Code (CSharp):
    1. #using UnityEngine.EventSystems;
    2. #using UnityEngine.Events;
    to the top of the monobehavior's script

    Then add a public memeber to the class:

    Code (CSharp):
    1. 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:

    Code (CSharp):
    1. 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:

    Code (CSharp):
    1. BaseEventData eventData= new BaseEventData(EventSystem.current);
    2. eventData.selectedObject=this.gameObject;
    Note whatever custom callback function we want to select in the editor, must be of the form:
    Code (CSharp):
    1. void foo(BaseEventData);
     
  2. steunity2

    steunity2

    Joined:
    Jun 16, 2015
    Posts:
    8
    I've been googled around. Finally found this thread.
    Awesome share! Thank you!
     
  3. Glurth

    Glurth

    Joined:
    Dec 29, 2014
    Posts:
    109
    Oh great, glad it was helpful! Any feedback/suggestions on making it better/easier to understand?
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,199
    There's been a big thread on it, but in case you didn't see it, the UnityEvent class does pretty much the same thing.
     
  5. Glurth

    Glurth

    Joined:
    Dec 29, 2014
    Posts:
    109
    Oh, that's nice! UnityEvent doesn't require you get or generate any BaseEventData, and lets you call multiple functions at once by adding "listeners". I like it, thanks Baste!

    Then again, I guess not having to pass the BaseEventData could also be a disadvantage since you no longer have ANY parameters.
     
  6. steunity2

    steunity2

    Joined:
    Jun 16, 2015
    Posts:
    8
    UnityEvent seems good too!
    Both EventTrigger.TriggerEvent and UnityEvent will be very useful for me.

    By the way, I'm implementing a "focus" system, and these callbacks are used for OnFocus and OnUnfocus.
     
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You can also build a full blown custom inspector using reflection. You can chain methods together and step through there results. Or you can find methods that are nested inside members of other types.

    It's mostly overkill for all practical purposes. But it was fun to build.
     
  8. Glurth

    Glurth

    Joined:
    Dec 29, 2014
    Posts:
    109
    paulseverinfi likes this.