How to handle multiple button on addListener
Hi, i already search this question on the forum and the solution given doesn't work for me. When i call the addListener function for all my buttons and when i click on any button i always have the same one coming back : "Button6".
I did try this : bout = b.GetComponent (); because i call a delegate function this means i need to serialize the variable to be updated but it doesn't work.
Here my function to add listener on everybutton :
public static void addListenerButton(GameObject canvas){
Component[] lesBoutons;
Button bout;
lesBoutons = canvas.GetComponentsInChildren<Button> ();
foreach (Component b in lesBoutons) {
bout = b.GetComponent<Button> ();
b.GetComponent<Button>().onClick.
AddListener (delegate{taskOnClick(canvas, "y", bout);});
b.tag = "ButtonPage1";
}
}
And here my function taskOnClick :
public static void taskOnClick(GameObject canvas, string jsonFlux, Button leButton){
Debug.Log (leButton);
Debug.Log ("You have clicked the button!");
GameObject rawImageGO = new GameObject ();
rawImageGO.transform.parent = canvas.transform;
rawImageGO.AddComponent<RawImage> ();
RawImage rawImageRT = rawImageGO.GetComponent<RawImage> ();
rawImageRT.rectTransform.sizeDelta = new Vector2 (1500F, 1000F);
rawImageRT.rectTransform.position = canvas.GetComponent<RectTransform> ().position;
rawImageGO.name = "StreamingVideo";
}
Thank's in advance.
I'm using c# btw.
Answer by sp1N · Nov 13, 2016 at 10:24 AM
I'm back because i solve my problem. If someone got the same problem he will be happy to know how to solve it. Here's the code :
public static void addListenerButton(GameObject canvas){
Button[] lesBoutons;
string bout;
lesBoutons = canvas.GetComponentsInChildren<Button> ();
foreach (Button b in lesBoutons) {
addListener(canvas, b);
b.tag = "ButtonPage1";
}
}
public static void addListener(GameObject canvas, Button leButton){
leButton.onClick.AddListener (() => taskOnClick (canvas, "y", leButton.name));
}
public static void taskOnClick(GameObject canvas, string jsonFlux, String leButton){
Debug.Log (leButton);
Debug.Log ("You have clicked the button!");
GameObject rawImageGO = new GameObject ();
rawImageGO.transform.parent = canvas.transform;
rawImageGO.AddComponent<RawImage> ();
RawImage rawImageRT = rawImageGO.GetComponent<RawImage> ();
rawImageRT.rectTransform.sizeDelta = new Vector2 (1500F, 1000F);
rawImageRT.rectTransform.position = canvas.GetComponent<RectTransform> ().position;
rawImageGO.name = "StreamingVideo";
}
Answer by Glurth · Nov 12, 2016 at 07:37 PM
Not sure if this is the issue, but this seems a bit odd:
bout = b.GetComponent<Button> ();
because b
IS already the button component. In fact, I would recommend you declare lesBoutons as an array of Button
s, rather than Component
s. Then, inside the loop, you can simply use b
, for you button reference:
foreach (Button b in lesBoutons) {
b.onClick.AddListener(... b ....);}
Thanks for your reply. But i already tried this and it doesn't work. When i click on the "button1" my Debug.Log on the taskOnClick function said it's "Button6" for all buttons i clicked.
Answer by bongert · Oct 08, 2018 at 12:38 PM
Hey, I think I'm facing the same problem right now. But I don't really get your solution, you just create the method addListener()? Doesn't work for me... At the moment I'm instantiating the buttons in one script:
public void SetPhrase(string[] phrases)
{
Button[] textBut = new Button[phrases.Length];
for(int i=0; i<phrases.Length; i++)
{
int closureIndex = i;
textBut[closureIndex] = Instantiate(textButton, contentPanel, false);
textBut[closureIndex].transform.Find("Text").gameObject.GetComponent<Text>().text = phrases[closureIndex];
}
}
So far so good, but when TaskWithParameters() is called in the script attached to the Button-Prefab the output is always the one of the last instantiated button:
public class ClickScript : MonoBehaviour
{
public Button button;
public string phrase;
void Start()
{
button = gameObject.GetComponent<Button>();
phrase = gameObject.transform.Find("Text").gameObject.GetComponent<Text>().text;
addListener(button, phrase);
}
public static void addListener(Button b, string p)
{
b.onClick.AddListener(() => TaskWithParameters(b, p));
}
public static void TaskWithParameters(Button b, string p)
{
print(b.GetComponent<ClickScript>().buttonIndex + ". Button clicked");
GameObject.FindWithTag("Player").GetComponent<Talking>().Sprich(p);
}
}
But the public string of each button ist the right one! Just TaskWithParameters doesn't work properly. If you know what I'm doing wrong let me know, thanks :-)