Search Unity

Anyone know a simple asset that lets you assign event handlers to script methods?

Discussion in 'General Discussion' started by Wom, Mar 12, 2014.

  1. Wom

    Wom

    Joined:
    Feb 7, 2013
    Posts:
    38
    I want something that works like NGUI, where you can assign one or more script methods as handlers for events like "OnClick" etc. In fact, I want exactly that - but I don't want to write editor scripts for all my stuff and the NGUI code doesn't seem to be very compatible with the PropertyDrawer infrastructure (all his stuff is written as Editor scripts).

    I'd like something simple - I'm not after playmaker or anything like that - just a tool to help me hook up specific component script events to other script methods by dragging and dropping in the editor. But I don't want to write editor scripts for every component - so I want something that works as a PropertyDrawer (either via attribute or with a simple serialized class that I have to use).

    Am I missing an easy *simple* way to do this? (but in that case, why does NGUI need all of its EventDelegate infrastructure?)
     
  2. wccrawford

    wccrawford

    Joined:
    Sep 30, 2011
    Posts:
    2,039
    I'm trying to wrap my head around what you're asking because I think you might have a good idea here. Anything that makes GUI easier is good idea, IMO.

    Can you be a little more specific in exactly what you want to see in the inspector, and what you'd see in the code you'd write for the script you'd put on the game object?
     
  3. DallonF

    DallonF

    Joined:
    Nov 12, 2009
    Posts:
    620
    This isn't exactly helpful, but I think Daikon Forge GUI does this out of the box.
     
  4. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    NGUI is like it is because it was written before Property Drawers existed. I think you can do this with property drawers (and like Dallon said, I think this is the way DFGUI does it). You can have one public game object called "Target", and then a public string called "Handler" with a new [MethodName] property attribute that you'd write. The property drawer for the Handler string would draw a dropdown list, and populate it with all the public void methods on all components on the target object, which I believe you can get by looping through them and calling GetType(component).GetMethodNames();
     
  5. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    I should add... if you don't care about having a drop-down, you don't need a property drawer. Just have a public GameObject target field and a public string handler field. Then call target.SendMessage(handler);
     
  6. Wom

    Wom

    Joined:
    Feb 7, 2013
    Posts:
    38
    So the key here was to search the asset store for the word "delegate" instead of "event". I found two assets I liked, both free.

    The first was an old one called "zz Signal Slot", it has a weird approach where the link is set up as another component, but it seemed to work and was nice and simple.
    Downsides:
    • doesn't seem to support notifying multiple targets with a single event (was hopeful that multiple components would do it, but didn't work, this is a show-stopper for me)
    • old and unmaintained, uses deprecated APIs
    • the forum thread and one of the reviews mention some serious bugs

    The second was "Editor Delegates", which has a more traditional approach to the interface, allows multiple targets for a single event and lets you pass parameters as well, neat!
    Downsides:
    • have to write a custom (but empty, at least) editor class for each script that wants to declare delegates
    • can't limit a target to specific script, so it lists all methods on all components and because it can deal with methods that take parameters, that gets to be a big list very quickly
    Going to go with this one for the moment.

    I liked this approach when I read it because of the simplicity, even wrote a "MonoBehaviourDelegate" class that encapsulated the target component and method name. And it was good enough in the inspector for a single one, but I need to notify multiple targets. But when I tried to declare a list of them, the Unity editor didn't know how to deal with it (just drew the normal list interface, which doesn't work).
     
    Last edited: Mar 13, 2014