我试图在(int值)中传递被点击按钮的索引,并写成这个- gp.GetComponentInChildren ().onClick.AddListener(() => Repeat(rep));
void chatDialogs() {
foreach (Transform child in this.transform) {
GameObject.Destroy (child.gameObject);
}
for (int i = 5; i > 0 ; i--) {
int currentStep = Laststep - i;
if (currentStep >= 0) {
gp = (GameObject)Instantiate (playerPreFab);
gp.transform.SetParent (this.transform);
}
gp.GetComponentInChildren<Button> ().onClick.AddListener(() =>
Repeat(**transform.GetSiblingIndex()**));
}
public void Repeat(int speakstep) {
Application.ExternalCall("textspeak", speakstep);
}
在Repeat()的speakstep对象中,应该传递被点击按钮的索引,但它获取我所点击的每个按钮的最后一个索引。
发布于 2019-05-28 02:11:21
我认为你可以这样做:
AddButtonListener(gp.GetComponentInChildren<Button> (), i);
在for
循环中,当AddButtonListener
被定义为:
void AddButtonListener(Button b, int index)
{
b.onClick.AddListener(()=>{Repeat(index)});
}
通过这种方式,您可以在侦听器函数中捕获按钮索引(但我不确定此模式的正确名称是什么),并且我并没有实际运行它就编写了这段代码。希望你现在有足够的信息让它工作。
https://stackoverflow.com/questions/56330152
复制相似问题