gpt4 book ai didi

c# - Unity Button OnClick AddListener 问题 - 与 For Each Loop 中最后一项的回调相同

转载 作者:行者123 更新时间:2023-11-30 16:08:38 25 4
gpt4 key购买 nike

所以我有一个脚本 (Unity C#),它运行一个生成答案数组的 IEnumerator 函数,我想以列表格式显示它们。

所以我使用:

public Button QuestionButton;

void Start(){
StartCoroutine(PresentQuestion());
}

IEnumerator PresentQuestion(){
CurrentStage = Stage.QuestionStart;

CurrentQuestion = "How many times do we have to do this to make it work?";
Answers.Add ("1");
Answers.Add ("2");
Answers.Add ("3");
Answers.Add ("4");

float newY = 80.0f;
foreach(string answer in Answers){
newY-=65.0f;
string locanswer = answer;
Button btn = Instantiate(QuestionButton);
btn.transform.position = new Vector3(225, newY, 0);
btn.transform.SetParent(QuestionUI.transform, false);
btn.gameObject.SetActive(true);
btn.transform.FindChild("Text").GetComponent<Text>().text = locanswer;
btn.onClick.AddListener(() => AnswerClicked(locanswer));
}
QuestionText.text = CurrentQuestion;
}

void AnswerClicked(string value){
print (value);
}

QuestionButton 是我在检查器中链接的按钮游戏对象,它实例化很好,文本设置很好,但它是在我添加监听器时出现的。

当我去点击按钮时,我“每次”都得到循环中的最后一个字符串或 _answer。

我不确定为什么实例化的每个新 Button (btn) 对象都不会在 AnswerClicked 处获得自己的 Listener。谁能解释一下?

仅供引用:QuestionUI 是我的 Canvas 。

谢谢。

更新:

此代码有效,但我之前的问题仍然存在。我将尝试使用我的实际代码进行更新。

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class Testing : MonoBehaviour {

public string CurrentQuestion;
public ArrayList Answers = new ArrayList();

public Button QuestionButton;
public GameObject QuestionUI;
public Text QuestionText;

void Start(){
StartCoroutine(PresentQuestion());
}

IEnumerator PresentQuestion(){
CurrentQuestion = "How many times do we have to do this to make it work?";
Answers.Add ("1");
Answers.Add ("2");
Answers.Add ("3");
Answers.Add ("4");

float newY = 80.0f;
foreach(string answer in Answers){
newY-=65.0f;
string locanswer = answer;
Button btn = Instantiate(QuestionButton);
btn.transform.position = new Vector3(225, newY, 0);
btn.transform.SetParent(QuestionUI.transform, false);
btn.gameObject.SetActive(true);
btn.transform.FindChild("Text").GetComponent<Text>().text = locanswer;
btn.onClick.AddListener(() => AnswerClicked(locanswer));
}
QuestionText.text = CurrentQuestion;

return null;
}

void AnswerClicked(string value){
print (value);
}
}

最佳答案

我遇到了同样的问题,我发现在单独的方法中设置 onClick 监听器对我有用。

在这种情况下我会这样做:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class Testing : MonoBehaviour {

public string CurrentQuestion;
public ArrayList Answers = new ArrayList();

public Button QuestionButton;
public GameObject QuestionUI;
public Text QuestionText;

void Start(){
StartCoroutine(PresentQuestion());
}

IEnumerator PresentQuestion(){
CurrentQuestion = "How many times do we have to do this to make it work?";
Answers.Add ("1");
Answers.Add ("2");
Answers.Add ("3");
Answers.Add ("4");

float newY = 80.0f;
foreach(string answer in Answers){
newY-=65.0f;
Button btn = Instantiate(QuestionButton);
btn.transform.position = new Vector3(225, newY, 0);
btn.transform.SetParent(QuestionUI.transform, false);
btn.gameObject.SetActive(true);
btn.transform.FindChild("Text").GetComponent<Text>().text = locanswer;
SetButtonOnClick(btn, answer);
}
QuestionText.text = CurrentQuestion;

return null;
}

void SetButtonOnClickAnswer(Button button, string value){
button.onClick.AddListener(() => AnswerClicked(value));
}

void AnswerClicked(string value){
print (value);
}
}

(免责声明:我没有对此进行测试,但我在我的项目中运行了一个非常相似的代码,这解决了问题。)

关于c# - Unity Button OnClick AddListener 问题 - 与 For Each Loop 中最后一项的回调相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28922083/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com