I have a system that creates buttons based on the files in a directory. I need to call a function in a script based on what a child object of the button's Text component text property reads. Basically, i need to modify each button to call a function with a specific paramter. How would I do this?
Asked
Active
Viewed 9,028 times
1 Answers
1
I think you need to check this : Event trigger Script
https://docs.unity3d.com/ScriptReference/EventSystems.EventTrigger.html
The Event Trigger Script allow you to call a method of child component on trigger (click, mouseover..).
(check this : http://answers.unity3d.com/questions/974622/unity-502f1-ui-button-onclick-function.html)
EDIT :
If you do not know in advance which method is to be assigned: you can use System.Reflection to retrieve the associated method.
string name = "BtnAction";
MethodInfo m = t.GetMethod(name);
I hope this will help you :
For each button, attach the components "Event Trigger" and your script with all your methods.
Then use reflection to get the associated method.
Finally, set the Event Trigger component using :
button.onClick.AddListener (FunctionToCall);

Dorian Caup
- 26
- 3
-
Do you mind explaining the links? This is a link only answer. – DᴀʀᴛʜVᴀᴅᴇʀ Sep 12 '16 at 14:12
-
Sorry I was updating my message. – Dorian Caup Sep 12 '16 at 14:28