Author Topic: Using Event Delegate to pass an argument to a button.onClick function  (Read 16628 times)

redmotion

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 11
    • View Profile
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.

redmotion

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 11
    • View Profile
Re: Using Event Delegate to pass an argument to a button.onClick function
« Reply #1 on: August 15, 2014, 03:36:00 PM »
After watching this 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!
« Last Edit: August 15, 2014, 03:44:31 PM by redmotion »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Using Event Delegate to pass an argument to a button.onClick function
« Reply #2 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. }

redmotion

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 11
    • View Profile
Re: Using Event Delegate to pass an argument to a button.onClick function
« Reply #3 on: August 17, 2014, 03:15:55 AM »
Thanks Aren, that will make my code less of a hack job!