Author Topic: How to check if Button is clicked from main script?  (Read 7742 times)

alenb

  • Guest
How to check if Button is clicked from main script?
« on: July 17, 2012, 08:20:09 PM »
Hi All,

Instead of having individual scripts for each NGUI Button I would like to simply reference the button from my main script and check if it has been clicked. Is such a thing possible? I've been banging my head against the table for a day and a half and I cannot for the life of me find any option that allows me to do such a thing.

Please if anyone has any ideas let me know.


Thank you =)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to check if Button is clicked from main script?
« Reply #1 on: July 17, 2012, 10:27:00 PM »
Of course. Use the UIEventListener.Get(buttonGameObject).onClick += YourClickCallback;

alenb

  • Guest
Re: How to check if Button is clicked from main script?
« Reply #2 on: July 17, 2012, 10:58:57 PM »
Thank you so much for your reply. It's all working now =)

lbzzbl

  • Guest
Re: How to check if Button is clicked from main script?
« Reply #3 on: July 18, 2012, 09:49:12 AM »
in this case, which is the preferable usage or gives better performance?

UIEventListener.Get(buttonGameObject).onClick += YourClickCallBack

or

setting the button to have a UIButtonMessage script and call YourClickCallBack?

PhilipC

  • Guest
Re: How to check if Button is clicked from main script?
« Reply #4 on: July 18, 2012, 10:38:08 AM »
From a performance standpoint

UIEventListener.Get(buttonGameObject).onClick += YourClickCallBack

is faster and UIButtonMessage uses SendMessage which is slower then directly calling the function.

lbzzbl

  • Guest
Re: How to check if Button is clicked from main script?
« Reply #5 on: July 18, 2012, 12:21:10 PM »
thanks a lot.

looks like it is time to change the code... :)

PhilipC

  • Guest
Re: How to check if Button is clicked from main script?
« Reply #6 on: July 18, 2012, 02:03:01 PM »
If you've done it the other way i would say leave it unless your really looking for something to do. Its not THAT much worse...