- Home /
Example of setting up a button OnClick event via scripting?
FYI - this is cross posted from the 4.6 forum. I wasn't getting any hits there, so I figured I'd try here as well.
My question is this:
Can anyone point me to an example of setting up a button OnClick event via scripting in 4.6?
The new GUI tutorials do a good job of showing how to use the inspector to make a button call arbitrary functions on an OnClick event (e.g. here). I figure there must be a way to do the same thing via scripting - to give a button a list of functions (and arguments for those functions) to call when it is clicked. However, I'm getting lost in the documentation (e.g. Event Tools, ButtonClickedEvent). There are lots of functions, but only very brief descriptions, and the meaning is beyond me.
Any advice would be greatly appreciated.
Answer by nur farazi · Jan 08, 2015 at 07:55 AM
for (int i =1; i<=50; i++) {
button.name=PlayerPrefs.GetString ("LeveltextNumber" + i.ToString ());
button.button.onClick.AddListener(delegate { test(button.name); });
}
public void test(string ok){
Debug.Log (ok);
}
it should solve the problem
please accept the answer if this solve your problem
@Redwolve it's similar but ins$$anonymous$$d of delegate...
you pass in function() { test(button.name); }
or just the whole function if you want. Though it's worth noting that some variables won't pass properly, but Game Objects seem to work fine. Also if your function doesn't have any variables to pass you can just pass in the name of the function .AddListener(test);
thank you very much nur farazi. It works
but I don't get the point of the loop. What is it for ?
How ever i have an issue : i'm trying to create multiple buttons in a loop, then assign a onClick event to each of them. The Event is supposed to call Test (with a increasing int), yet it seams to call Test with the last number iterated through.
void Start () {
for (int i = 0; i < 5; ++i ) {
GameObject button = Instantiate (prefab) as GameObject;
button.GetComponent<Button> ().onClick.AddListener ( delegate { Test (i); });
}
}
void Test ( int id ) {
Debug.Log (id.ToString ());
}
@Strass_Productions Hi, I have encountered the same issue almost after a YEAR. I was hopping you could have a solution to this Problem.
Thanking you, Hitarth Doctor
@Strass_Productions Hi, I have encountered the same issue almost after a YEAR. I JUST solved the problem by using a Coroutine to do the following:
button.GetComponent<Button> ().onClick.AddListener ( delegate { Test (i); });
Using this the calls get stored in the stack until the for loop ends. Giving us the desierd Results.
Hitarth Doctor
For everyone struggling with the problem when using integers ins$$anonymous$$d of strings in the loop: You can force the reservation of memory for each integer by the 'new' keyword. So the loop with integers becomes:
for (int i =1; i<=50; i++)
{
GameObject button = Instantiate (prefab) as GameObject;
int x = new int();
x = i;
button.GetComponent<Button> ().onClick.AddListener ( delegate { Test (x); });
}
void Test ( int id ) {
Debug.Log (id.ToString ());
}
Answer by LynnPi · Sep 26, 2014 at 03:30 AM
Just like this: xxxBtn.onClick.AddListener(YourDisposeFunction);
Hmm. That seems pretty easy. I'll give it a try. But then what about arguments? Using the inspector, you can fix it so that when a function is called, it is passed a single, pre-deter$$anonymous$$ed argument.
$$anonymous$$ake sure you have "using UnityEngine.UI;" at the top of your script.
For argument use:
xxxBtn.onClick.AddListener(delegate { YourDisposeFunction(YourArgument); });
Note : The new event won't appears in inspector at runtime, but it will be called on click.
You can also do it with a lamba expression too:
BUTTON.onClick.AddListener(() => YourFunction(YourParam));