Author Topic: eventReceiver For All Controls?  (Read 1476 times)

Rafe

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 78
    • View Profile
eventReceiver For All Controls?
« on: July 18, 2012, 12:40:24 AM »
Hi,

I've been using nGUI for about a day. I am very impressed by how fast I can take my GUI from another sprite system and recreate it. More importantly, I feel like I have a lot of control. The tools are great!

I am missing delegates, but messages are fine for UI-level performance, so no issue there. However, in my first script, I found the slider had eventReceiver, which allows for delegate-like ease-of-use. It looks like if I want something similar for a button, I need to add a ButtonMessage component for each message. Is that right?

What I would love to have is eventReceiver for everything.

In my script that handles the logic for a panel of buttons, I can set it in Awake making it very fast and easy to control in my script without having to worry about the inspector set up at this level.

Would this break a paradigm? It should be quite easy to implement. Thoughts?

Rafe

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 78
    • View Profile
Re: eventReceiver For All Controls?
« Reply #1 on: July 18, 2012, 01:05:10 AM »
I realized I could add the component via script and get much the same functionality. I don't think there is anything wrong with just doing this...

This example is for a button that starts a level. It is a script on the GUI root that needs to handle the button click. I already have a reference to the UIButton "playButton".

        var messenger = this.playButton.gameObject.AddComponent<UIButtonMessage>();
        messenger.target = this.gameObject;
        messenger.trigger = UIButtonMessage.Trigger.OnClick;
        messenger.functionName = "PlayButtonOnClick";


The only thing I don't like about this is having to pass the name of the function instead of a reference. I'd have to try it but I believe you can store a reference and call it even with JavaScript. It isn't a true C# event delegate, but I believe it can be done. Perhaps a new component UIConnectFunction or some such thing... I'm just brainstorming.

Rafe

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 78
    • View Profile
Re: eventReceiver For All Controls?
« Reply #2 on: July 18, 2012, 01:10:09 AM »
And then I find UIEventListener: http://www.tasharen.com/ngui/docs/class_u_i_event_listener.html
        UIEventListener.Get(this.playButton.gameObject).onClick += this.PlayButtonOnClick;

Awesome.
« Last Edit: July 18, 2012, 01:14:40 AM by Rafe »