Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: n8 on December 12, 2012, 12:17:01 PM

Title: How to pass arguments along with UIButtonMessage
Post by: n8 on December 12, 2012, 12:17:01 PM
I have been running into several scenarios where I am needing to pass an argument along to a function when a button is pressed.  I have been using crazy workarounds for some time now as I didn't really want to mess with the underlying core of NGUI scripts.  Finally ran into a case where I needed to generate buttons at runtime that then needed the ability to pass specific GameObjects back to another object to be instantiated when that button was hit.   This solution seems to be working great in this context.

(note in order to preserve NGUI's private code I am only posting the changes necessary to make this work)

around line 26 add this:
public Object parameter = null;

now find all instances of the call "SendMessage".  There should only be 2 and located at the very bottom of the script in the "send" function (at time of writing).

replace the call of "SendMessage"
From:
SendMessage(functionName, gameObject, SendMessageOptions.DontRequireReceiver);
To:
SendMessage(functionName, parameter, SendMessageOptions.DontRequireReceiver);

Right now the "SendMessage" only supports one parameter (Unity thing), but there are some roundabout workarounds for getting more than one to pass.

If there was a specific reason that the generic "gameObject" was being passed instead of parameters, please let me know.  So far this seems to be working for my needs.

Please let me know if there is any confusion or explanation needed
Title: Re: How to pass arguments along with UIButtonMessage
Post by: ArenMook on December 12, 2012, 12:33:44 PM
I suggest writing your own custom script instead of using UIButtonMessage. This script of yours can have as many parameters as you wish, and you can simply pass a reference to it in SendMessage, then your receiver can do whatever it wants.
Title: Re: How to pass arguments along with UIButtonMessage
Post by: n8 on December 12, 2012, 12:44:03 PM
Aren,
I understand why you are encouraging a custom script. I can also agree that there could be a lot more argument functionality built into a custom script.  However, the UIButtonMessage script is already very well adapted to handling clicks/touches in the NGUI environment, and any custom script would prob just be an exact duplicate with the parameter option added.

I guess I just don't understand why the parameter option is really being used already by UIButtonMessage.  Seems like there are plenty of use cases for needing to pass a single parameter into a function on a button press.

To be clear, I think what you have done is great, just some clarification on the thought process would help.
Title: Re: How to pass arguments along with UIButtonMessage
Post by: ArenMook on December 12, 2012, 02:58:57 PM
So you can have a custom script on it. You get a game object -- GetComponent<CustomScript>() on it to get your script, and it can have as many parameters on it as you want.