Author Topic: UIEventListener vs OnClick  (Read 7145 times)

Maxii

  • Jr. Member
  • **
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 55
    • View Profile
    • Strategic Forge
UIEventListener vs OnClick
« 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!


I'm looking for an artist partner to complement my programming skill in developing a space-based Civilization-like empire building game, ala GalCiv2 or Distant Worlds based on Unity3D.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIEventListener vs OnClick
« Reply #1 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.
« Last Edit: October 15, 2013, 02:49:02 PM by ArenMook »

Maxii

  • Jr. Member
  • **
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 55
    • View Profile
    • Strategic Forge
Re: UIEventListener vs OnClick
« Reply #2 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?
I'm looking for an artist partner to complement my programming skill in developing a space-based Civilization-like empire building game, ala GalCiv2 or Distant Worlds based on Unity3D.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIEventListener vs OnClick
« Reply #3 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.

OnlineCop

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 51
    • View Profile
Re: UIEventListener vs OnClick
« Reply #4 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).

calbar

  • Guest
Re: UIEventListener vs OnClick
« Reply #5 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.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIEventListener vs OnClick
« Reply #6 on: October 15, 2013, 02:49:11 PM »
Yup, typo.