Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: zeus_82 on September 09, 2014, 11:45:53 AM

Title: Assign EventDelegate and Parameters from script
Post by: zeus_82 on September 09, 2014, 11:45:53 AM
Hey guys,

I want to assign an EventDelegate to a UIButton in addition to parameters,
I know I can do this :
button.GetComponent<UIButton>().onClick.Add(new EventDelegate(targetGameObject, "MethodToCall"));

But, how can I add parameters ?
So, If the MethodToCall has 2 parameters, I can pass them.
Please, help.
Title: Re: Assign EventDelegate and Parameters from script
Post by: KeithT on September 09, 2014, 12:45:49 PM
After lots of hunting this worked for me. It passed a reference to the UIButton script itself as the parameter.

        UIButton theBtnScript = theBtn.GetComponent<UIButton>();
        EventDelegate del = new EventDelegate();
        del.target = this;
        del.methodName = "NGUI3ButtonClickHandler";
        EventDelegate.Parameter theParameter = new EventDelegate.Parameter(theBtnScript, "ITEM_VALUE");
        del.parameters.SetValue(theParameter, 0);
        EventDelegate.Set(theBtnScript.onClick, del);
Title: Re: Assign EventDelegate and Parameters from script
Post by: ArenMook on September 10, 2014, 01:04:29 AM
http://www.tasharen.com/forum/index.php?topic=11113.msg51774#msg51774
Title: Re: Assign EventDelegate and Parameters from script
Post by: zeus_82 on September 10, 2014, 01:39:18 AM
Thank you both for the great help.

It worked.

EventDelegate eventDelegate = new EventDelegate(this, "TestMethod");
eventDelegate.parameters[0].value = 20;
button.GetComponent<UIButton>().onClick.Add( eventDelegate );

Although the argument would be null in the inspector, but the value is serialized and coupled
with the parameter.

:-)
Title: Re: Assign EventDelegate and Parameters from script
Post by: aer0ace on February 11, 2015, 01:29:21 AM
Sorry to necro this, but this is completely valid to the problems I'm running into.

I'm doing something similar:
My OnClickMe method gets hit, but my int num is 0. I tried other values, like string and GameObject, and they're null.

  1.         UIButton b = buttonXfrm.gameObject.GetComponent(typeof(UIButton)) as UIButton;
  2.  
  3.         EventDelegate del = new EventDelegate(this, "OnClickMe");
  4.         del.parameters[0].value = 123;
  5.         EventDelegate.Set(b.onClick, del);
  6.  
  7.  
  8.         private void OnClickMe(int num)
  9.         {
  10.  
  11.         }



Any ideas?
Title: Re: Assign EventDelegate and Parameters from script
Post by: ArenMook on February 11, 2015, 09:27:34 PM
Works just fine when I tried.

1. New scene.
2. ALT+SHIFT+S, ALT+SHIFT+C, attached UIButton script to it.
3. Attached this script to it:
  1. using UnityEngine;
  2.  
  3. public class Test : MonoBehaviour
  4. {
  5.         void Start ()
  6.         {
  7.                 UIButton btn = GetComponent<UIButton>();
  8.                 EventDelegate del = new EventDelegate(this, "OnClickMe");
  9.                 del.parameters[0].value = 123;
  10.                 EventDelegate.Set(btn.onClick, del);
  11.         }
  12.  
  13.         private void OnClickMe (int num)
  14.         {
  15.                 Debug.Log(num);
  16.         }
  17. }
  18.  
4. Hit Play, see "123" printed.
Title: Re: Assign EventDelegate and Parameters from script
Post by: aer0ace on February 11, 2015, 11:44:40 PM
Yup, that worked for me too. Thanks for checking this out for me.

Now to figure out my EBKAC.

EDIT:
And my error was, I was doing all that in a script that wasn't an NGUI control.