Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: redmotion on August 15, 2014, 02:12:48 PM

Title: Using Event Delegate to pass an argument to a button.onClick function
Post by: redmotion on August 15, 2014, 02:12:48 PM
I can add a function via code to a series of instantiated prefabs with this:-

  1. UIButton third = assignmentEntry.transform.GetChild(2).GetComponent<UIButton>(); // gets the button component
  2.                        
  3. EventDelegate.Set( third.onClick, onClickAssignmentListEntry );  // adds the onClickAssignmentListEntry function fine

However, I want the function to receive an argument so I can identify which button out of the prefabs was pressed ( each will be given an individual number).

I am aware that I need to use the EventDelegate.Parameter to add these arguments before setting the event delegate, however I can’t work out how this should be structured.

Ideally the function would be:-

  1. public void onClickAssignmentListEntry ( int entry number ) {
  2.         // each button will have it’s own entry number
  3. }

How do I create an event delegate to enable the function onClickAssignmentListEntry to be passed an integer ( or a string that I can cast to an integer)?

Many thanks in advance.
Title: Re: Using Event Delegate to pass an argument to a button.onClick function
Post by: redmotion on August 15, 2014, 03:36:00 PM
After watching this https://www.youtube.com/watch?v=K3lvXIvJFKc (https://www.youtube.com/watch?v=K3lvXIvJFKc)

I decided to cheat (e.g.: avoid the first approach using eventdelegate) and use:

  1. UIButton third = assignmentEntry.transform.GetChild(2).GetComponent<UIButton>();
  2. third.name =   i.ToString(); // setting each button name to a unique number (i) in the for loop
  3. EventDelegate.Set( third.onClick, onClickAssignmentListEntry )

and changed the target function to:-

  1. public void onClickAssignmentListEntry (  ) {
  2. Debug.LogWarning ( UIButton.current.name +" button pressed" );
  3.         }

Getting me the same result without the hoop jumping required with event delegate parameters.

Feel free to post a better solution!
Title: Re: Using Event Delegate to pass an argument to a button.onClick function
Post by: ArenMook on August 16, 2014, 09:52:01 AM
You can currently pass literals via code in 3.7.0 like so:
  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, "MyClick");
  9.                 del.parameters[0].value = 123;
  10.                 EventDelegate.Set(btn.onClick, del);
  11.         }
  12.  
  13.         void MyClick (int someValue)
  14.         {
  15.                 Debug.Log(someValue);
  16.         }
  17. }
In previous versions you have to use properties or anonymous delegates instead (which is technically shorter code anyway).
  1. using UnityEngine;
  2.  
  3. public class Test : MonoBehaviour
  4. {
  5.         void Start ()
  6.         {
  7.                 UIButton btn = GetComponent<UIButton>();
  8.                 EventDelegate.Set(btn.onClick, delegate() { MyClick(123); });
  9.         }
  10.  
  11.         void MyClick (int someValue)
  12.         {
  13.                 Debug.Log(someValue);
  14.         }
  15. }
Title: Re: Using Event Delegate to pass an argument to a button.onClick function
Post by: redmotion on August 17, 2014, 03:15:55 AM
Thanks Aren, that will make my code less of a hack job!