Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: lalamax3d on July 20, 2013, 05:40:37 PM

Title: ngui forward event question
Post by: lalamax3d on July 20, 2013, 05:40:37 PM
simple case, wthere a panel has 5 buttons. rather than apply a script to each button, i wanted to put 1 script on panel, which take care of everything.
please correct me if i am wrong.. current assumption is, multiple methods to solve this
a- put component "Button Message" on each and mention the function to be called
b- use component "Forward Event" on each button and then in panel script, use 'onClick' function to listen them
c- use "UIEventListener" somehow magically ( most modern approach )

Current status
i can solve via method 'a' >> pros: simple / easy >> cons: have to mention function on each button component
i am trying to use method 'b' >> pros: good / decent cons: i am having difficulty in detecting which button is pressed in 'onClick' function (in panel)
ideally, if someone can give few steps demo / explanation about how to use third approach, i'll be greatful.

huge thanks in advance and regards,lala
Title: Re: ngui forward event question
Post by: ArenMook on July 21, 2013, 04:39:10 AM
Assuming your script references the buttons, you can subscribe to button events using the UIEventListener.Get(buttonGO).onClick += MyClickFunction;

The function will receive a game object parameter that tells you which button triggered the event.
Title: Re: ngui forward event question
Post by: lalamax3d on July 21, 2013, 05:52:24 AM
yes, panel script reference all gui widgets (buttons etc) at start (onStart) it varifies or create references.

i tried >> here is my script

if i used your line as is, it gave 3 errors. one of them was about trying parenthesis for function call. so i did that and 2 errors went away.
currently my line is like
  1. UIEventListener.Get(btnAddAlarm).onClick += OnAddAlarm();
now only error editor is showing is
Assets/_scripts/AlarmSettings.cs(32,50): error CS0019: Operator `+=' cannot be applied to operands of type `UIEventListener.VoidDelegate' and `void'
Title: Re: ngui forward event question
Post by: lalamax3d on July 21, 2013, 05:56:45 AM
if i don't put parenthesis, i.e
  1. UIEventListener.Get(btnAddAlarm).onClick += OnAddAlarm;
>> then errors are like this
Assets/_scripts/AlarmSettings.cs(32,50): error CS0123: A method or delegate `AlarmSettings.OnAddAlarm()' parameters do not match delegate `UIEventListener.VoidDelegate(UnityEngine.GameObject)' parameters
Assets/_scripts/AlarmSettings.cs(32,50): error CS0428: Cannot convert method group `OnAddAlarm' to non-delegate type `UIEventListener.VoidDelegate'. Consider using parentheses to invoke the method
Assets/_scripts/AlarmSettings.cs(32,50): error CS0019: Operator `+=' cannot be applied to operands of type `UIEventListener.VoidDelegate' and `method group'

any suggestions..
Title: Re: ngui forward event question
Post by: lalamax3d on July 21, 2013, 06:21:39 AM
ahh, its working now, i read your second line again carefully, my bad. function should have argument of gameobject type.
exactly what i was hoping for. huge thanks for reply
have a good day