Author Topic: Assign EventDelegate and Parameters from script  (Read 6822 times)

zeus_82

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Assign EventDelegate and Parameters from script
« 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.

KeithT

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 55
    • View Profile
Re: Assign EventDelegate and Parameters from script
« Reply #1 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);

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile

zeus_82

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Re: Assign EventDelegate and Parameters from script
« Reply #3 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.

:-)

aer0ace

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 15
    • View Profile
Re: Assign EventDelegate and Parameters from script
« Reply #4 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?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Assign EventDelegate and Parameters from script
« Reply #5 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.

aer0ace

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 15
    • View Profile
Re: Assign EventDelegate and Parameters from script
« Reply #6 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.
« Last Edit: February 12, 2015, 01:40:36 AM by aer0ace »