i have the following code
public void OnClickMaterialChooserDir(int idx)
{
Debug.Log("OnClickMaterialChooserDir "+idx);
}
public void PopulateMaterialChooserPanel()
{
//Debug.Log("PopulateMaterialChooserPanel");
string myPath = "Assets/Resources/Materials/";
DirectoryInfo matdir = new DirectoryInfo(myPath);
DirectoryInfo[] dirInfo = matdir.GetDirectories();
//FileInfo[] info = dir.GetFiles("*.*");
int i = 0;
foreach (DirectoryInfo dir in dirInfo)
{
//ItemGameObject is my prefab pointer that i previous made a public property
//and assigned a prefab to it
DefaultControls.Resources TempResource = new DefaultControls.Resources();
GameObject buttonGO = DefaultControls.CreateButton(TempResource);
buttonGO.AddComponent<LayoutElement>();
Button dirButton = buttonGO.GetComponent<Button>();
dirButton.onClick.AddListener(() => { OnClickMaterialChooserDir(i); }); //AddListener(delegate { OnClickMaterialChooserDir(i); });
Debug.Log("PopulateMaterialChooserPanel "+i);
i++;
Text text = dirButton.transform.GetChild(0).GetComponent<Text>();
text.text = dir.Name;
text.fontStyle = FontStyle.Bold;
text.fontSize = 16;
dirButton.transform.SetParent(_MaterialChooserScrollViewContent, false);
}
}
It creates buttons in a scrollview based on the directories in a resources folder, and it works, the buttons are created, the correct label text is set and i can click.
but when i get the callback to OnClickMaterialChooserDir idx is always 3 (the number of directories in this case)
i got the idea for the code from here
i tried both the delegate method and the one you see here and both give the same result
does anyone know what’s wrong?
thx
PS, PopulateMaterialChooserPanel() is only called from Start
i suspect the problem is the buttons are created at run time, not sure