Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: Maxii on August 18, 2013, 04:53:23 PM

Title: UIEventListener vs OnClick
Post by: Maxii on August 18, 2013, 04:53:23 PM
I'm curious. I've used both

  1. UIEventListener.Get(thisGameobject).onClick += MyOnClickMethod;
  2. void MyOnClickMethod(GameObject sender) { // do something }

and

  1. void OnClick() { MyOnClickMethod() }
  2. void MyOnClickMethod() { // do something }

to interact with Ngui events. What are the differences, advantages/disadvantages, etc.?

btw, Great product! Can't wait to see what you do with Unity's next gen GUI!


Title: Re: UIEventListener vs OnClick
Post by: ArenMook on August 19, 2013, 08:50:09 AM
Event listener subscribes to remote events. OnClick() receives the event on the game object. That's it.
Title: Re: UIEventListener vs OnClick
Post by: Maxii on August 19, 2013, 09:19:08 AM
Clarification: So the principal reason to use EventListener to subscribe is so one can unsubscribe (when an instantiable gameobject is destroyed for instance)? If one doesn't need to unsubscribe (as in a permanent UI gameobject for instance), then simply implementing OnClick() on the receiving gameobject is sufficient?
Title: Re: UIEventListener vs OnClick
Post by: ArenMook on August 19, 2013, 09:50:15 PM
No, UIEventListener is mainly for subscribing to events on remote objects.

For example, when you have a game manager class that you want to subscribe to events triggered by 10 different buttons. Instead of attaching a custom script to each button, just have a event listeners pointing to functions inside your game manager class.
Title: Re: UIEventListener vs OnClick
Post by: OnlineCop on August 20, 2013, 08:56:56 AM
An example would be:

If you construct a button (MyStartGameButton.prefab) and child it under a UICamera hierarchy, that UICamera will try to pass it an OnClick event. If there is no component attached to it, nothing happens, as there is no receiver. But, if you define the 'void OnClick()' method in a new component, and attach that component to the MyStartGameButton, the UICamera will pass the OnClick event to it.

(As an aside, if you attach 20 different components to the same button, and each of them had its own OnClick method defined, then all of those components receive that OnClick event. That's a lot of clicks for a single button. ;) )

Alternately, let's say that you want to define a single component in your scene to handle all the buttons therein. You can create a single controller class and use the 'UIEventListener.Get(...)' subscription model to point to each of the buttons and subscribe to each of their OnClick events. Then all of the OnClick events passed from the UICamera are returned to that single component, and you can handle them all in one place (instead of having dozens of components all around your scene).
Title: Re: UIEventListener vs OnClick
Post by: calbar on October 15, 2013, 11:56:43 AM
Just wanted to thank you for the excellent explanation OnlineCop, it really helped me understand this.

On a side note, ArenMook, did you mean "Event listener subscribes to remote events" in your first reply? Unless I'm wrong, that might have been part of the confusion.
Title: Re: UIEventListener vs OnClick
Post by: ArenMook on October 15, 2013, 02:49:11 PM
Yup, typo.