Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Why can't we just have events? ( Detecting Clicks )

Discussion in 'UGUI & TextMesh Pro' started by ________, Aug 21, 2014.

  1. ________

    ________

    Joined:
    Aug 1, 2013
    Posts:
    7
    I'm trying to work out how to add events at runtime. I've only been doing this for an hour or so but so far to listen for a click I have:

    var k=new EventTrigger.TriggerEvent();
    k.AddListener(delegate(BaseEventData arg0){
    var somethingUsable=(arg0 as PointerEventData);
    Debug.Log(somethingUsable);
    });
    var j = new EventTrigger.Entry(){eventID=EventTriggerType.PointerClick,callback=k};
    GetComponent<EventTrigger>().delegates.Add( j );

    That is horrible.
    To un-listen I need to keep a reference to k and j.
    The casting is horrible.

    Why can't we just have GetComponent<EventTrigger>().PointerClick += delegate( PointerEventData arg0 ){ };
     
  2. taylank

    taylank

    Joined:
    Nov 3, 2012
    Posts:
    182
    I have to agree. I was wrestling with this earlier, wondering whether I was missing an obvious shortcut. It's really awkward. Thanks for pointing out the casting, btw. Somehow I missed that I could cast BaseEventData as other types.
     
  3. ________

    ________

    Joined:
    Aug 1, 2013
    Posts:
    7
    I got unnecessarily angry about it :)
     
  4. nventimiglia

    nventimiglia

    Joined:
    Sep 20, 2011
    Posts:
    153
    I think your doing it work.

    Button.onClick.AddListener(Call);

    void Call()
    {
    }
     
  5. ________

    ________

    Joined:
    Aug 1, 2013
    Posts:
    7
    That seems to only be for OnClick, not the other event types.
    It also assumes you want events only from Buttons, not things like text.
     
  6. nventimiglia

    nventimiglia

    Joined:
    Sep 20, 2011
    Posts:
    153
    InputText.onSubmit.AddListener(SubmitText);

    void SubmitText(string text){}

    For text there is also the onValidate delegate group

    I am offering these as workarounds. I agree, the EventTrigger api is sloppy.
     
  7. ________

    ________

    Joined:
    Aug 1, 2013
    Posts:
    7
    I'm talking about all available events that are currently only accessible via EventTrigger.
    OnDeselect, OnDrag, OnDrop, OnMove, OnPointerClick, OnPointerDown etc etc, for Buttons, Text and other Canvas based Behaviours.

    The only friendly way of using these is via the inspector, a reasonable expectation would be exposing these events via actual events ( c# events ) or at the very least mirroring the functionality of Button.OnClick for all events in EventTrigger
     
  8. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,221
    There is a MUCH easier way :)

    So write a monobehaviour and implement the desired interface. If you put this on an object that works with the event system (an image or text element is a good example) it will receive the call when you perform the event.

    The interfaces we have are:

    Code (csharp):
    1.  
    2.             IPointerEnterHandler,
    3.             IPointerExitHandler,
    4.             IPointerDownHandler,
    5.             IPointerUpHandler,
    6.             IPointerClickHandler,
    7.             IDragHandler,
    8.             IDropHandler,
    9.             IScrollHandler,
    10.             IUpdateSelectedHandler,
    11.             ISelectHandler,
    12.             IDeselectHandler,
    13.             IMoveHandler
    14.  
     
  9. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,221
  10. ________

    ________

    Joined:
    Aug 1, 2013
    Posts:
    7
    Ah that's more like it, now i can rig up events/delegates :)
    Interfaces aren't normally Unity's thing, so i didn't even look.
     
  11. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,221
    Yea, we are trying to move towards better c# practices. UI system was a good a place as any to start, and the whole team cares about this stuff. Death to magic methods.
     
    callen, GarthSmith, Jessy and 7 others like this.
  12. taylank

    taylank

    Joined:
    Nov 3, 2012
    Posts:
    182
    Hey Tim, I've given the interfaces a try. It works but I'm getting error messages like this:

    Type IDragHandler expected MouseEventSignaller received. i:0. Object reference not set to an instance of an object

    MouseEventSignaller is the monobehaviour that implements IDragHandler. As I said, it works as far as the UI functionality goes but I'm wondering what I am doing wrong to get the error message. Any ideas?

    Edit: I was having this problem due to a mistake on my part by adding the monobehaviour twice to the gameobject. The problem went away when I corrected this.
     
    Last edited: Aug 22, 2014
    Tim-C likes this.
  13. tasadar

    tasadar

    Joined:
    Nov 23, 2010
    Posts:
    290
    to procedurally generate ui interfaces the suggested interfacing method is not always viable. all scripts do not have to be inherited from monobehavior, i have only 1 monobeh. inherited class in my whole game. also there has to be some way to provide a parameter to the events. it could be like Button.onClick.AddListener(Function, ParameterList);
     
  14. Togis

    Togis

    Joined:
    Aug 24, 2014
    Posts:
    4
    The IPointerClickHandler is very cool, but how could we differenciate left click and rigth click on an UI gameObject with the Pointer (for pc games) ?
     
  15. Diablo404

    Diablo404

    Joined:
    Mar 15, 2013
    Posts:
    136
  16. alt.tszyu

    alt.tszyu

    Joined:
    Nov 12, 2013
    Posts:
    110
     
  17. v21

    v21

    Joined:
    Jun 13, 2009
    Posts:
    9
    This is actually a error that's happened after the drag handler event has fired, in your own scripts. It's a known bug that it doesn't report errors correctly.
     
  18. Enoch

    Enoch

    Joined:
    Mar 19, 2013
    Posts:
    198
    Not sure how helpful this is but I had a great amount of trouble setting event callbacks from code until I stumbled upon Events.UnityEventTools, that had a bunch of useful static functions for setting event callbacks, when the callbacks had a single parameter. This is absolutely one of those areas I would love to see more documentation, I still want to see the editor/inspector code for setting those events.
     
  19. taylank

    taylank

    Joined:
    Nov 3, 2012
    Posts:
    182
    For my project I ended up creating a class with static c# events corresponding to every kind of UI event, and relaying all the UI events through that. That way I can pass as many parameters as I want. On the receiving end I just check which UI element fired the event, and decide to react or not based on that.
     
  20. villalbaweb

    villalbaweb

    Joined:
    Sep 29, 2019
    Posts:
    4