【发布时间】:2014-09-13 02:57:19
【问题描述】:
在 Unity 4.6 中试用新的 uGUI,编写按钮脚本...遍历目录并为每个文件创建一个按钮... AddListener 位中 f.Name 的值应为每个按钮独立设置。相反,每个按钮都具有要处理的最后一个 f.Name 的值(目录中按字母顺序排列的最后一个文件)。有什么想法吗?
foreach (FileInfo f in fileInfo) {
Button btnCurrLvl = (Button) Instantiate(btnLvl);
btnCurrLvl.GetComponentInChildren<Text>().text = f.Name.Remove(f.Name.Length-5);
Debug.Log(f.Name); // f.Name is different every time
btnCurrLvl.name = f.Name; // renaming the btns works
btnCurrLvl.GetComponent<Button>().onClick.AddListener(() => LoadLocalLvl(f.Name)); // all the listeners on all created buttons are set to the last value of f.Name!?
}
【问题讨论】:
-
请使用 unity3d 解决此类问题。