C# 在Unity中单击按钮时,如何显示问答游戏问题?
我正在尝试制作一个有四个按钮的问答游戏,答案和问题存储在一个数组中。字母应该一次打印一个,在显示前3个索引后,只有当按下屏幕上的按钮时,才会显示下一个索引 到目前为止,我似乎只能得到要显示的前3个索引,但我不确定如何获得按钮来触发数组中要显示的下一个问题 我的代码是:C# 在Unity中单击按钮时,如何显示问答游戏问题?,c#,arrays,unity3d,C#,Arrays,Unity3d,我正在尝试制作一个有四个按钮的问答游戏,答案和问题存储在一个数组中。字母应该一次打印一个,在显示前3个索引后,只有当按下屏幕上的按钮时,才会显示下一个索引 到目前为止,我似乎只能得到要显示的前3个索引,但我不确定如何获得按钮来触发数组中要显示的下一个问题 我的代码是: public GameObject button1; public GameObject button2; public GameObject button3; public GameObject button4; public
public GameObject button1;
public GameObject button2;
public GameObject button3;
public GameObject button4;
public float letterPause = 0.05f;
public float sentencePause = 2.0f;
string[] strArray = new string[13];
int i;
int count = 3;
int score;
bool nextQuestion = false;
void Start () {
strArray[0] = "Hello and welcome!";
strArray[1] = "This 11 question quiz is going to test your knowledge";
strArray[2] = "Question 1";
strArray[3] = "Question 2";
strArray[4] = "Question 3";
strArray[5] = "Question 4";
strArray[6] = "Question 5";
strArray[7] = "Question 6";
strArray[8] = "Question 7";
strArray[9] = "Question 8";
strArray[10] = "Question 9";
strArray[11] = "Question 10";
strArray[12] = "Question 11";
StartCoroutine (TypeText ());
}
IEnumerator TypeText () {
for (i = 0; i < count; i++) {
foreach (char letter in strArray[i].ToCharArray()) {
gameObject.GetComponent<Text> ().text += letter;
yield return new WaitForSeconds (letterPause);
}
if (i != count - 1) {
yield return new WaitForSeconds (sentencePause);
gameObject.GetComponent<Text> ().text = "";
}
}
StartCoroutine (TypeText2 ());
}
IEnumerator TypeText2 () {
while (nextQuestion == true) {
for (i = 3; i < strArray.Length; i++) {
foreach (char letter in strArray[i].ToCharArray()) {
gameObject.GetComponent<Text> ().text += letter;
yield return new WaitForSeconds (letterPause);
}
gameObject.GetComponent<Text> ().text = "";
}
}
}
public void OnClickButton1() {
button1.GetComponentInChildren<Text> ().text = "";
button2.GetComponentInChildren<Text> ().text = "";
button3.GetComponentInChildren<Text> ().text = "";
button4.GetComponentInChildren<Text> ().text = "";
nextQuestion = true;
}
public void OnClickButton2() {
button1.GetComponentInChildren<Text> ().text = "";
button2.GetComponentInChildren<Text> ().text = "";
button3.GetComponentInChildren<Text> ().text = "";
button4.GetComponentInChildren<Text> ().text = "";
nextQuestion = true;
}
public void OnClickButton3() {
button1.GetComponentInChildren<Text> ().text = "";
button2.GetComponentInChildren<Text> ().text = "";
button3.GetComponentInChildren<Text> ().text = "";
button4.GetComponentInChildren<Text> ().text = "";
nextQuestion = true;
}
public void OnClickButton4() {
button1.GetComponentInChildren<Text> ().text = "";
button2.GetComponentInChildren<Text> ().text = "";
button3.GetComponentInChildren<Text> ().text = "";
button4.GetComponentInChildren<Text> ().text = "";
nextQuestion = true;
}
你的第二次合作计划不正确。我使用count变量作为问题在数组中的位置来询问并使用它进行迭代 这就是解决方案:
using UnityEngine;
using System.Collections;
public class QuizScript : MonoBehaviour {
public GameObject button1;
public GameObject button2;
public GameObject button3;
public GameObject button4;
public float letterPause = 0.05f;
public float sentencePause = 2.0f;
string[] strArray = new string[13];
int i;
int count = 3;
int score;
bool nextQuestion = false;
void Start () {
strArray[0] = "Hello and welcome!";
strArray[1] = "This 11 question quiz is going to test your knowledge";
strArray[2] = "Question 1";
strArray[3] = "Question 2";
strArray[4] = "Question 3";
strArray[5] = "Question 4";
strArray[6] = "Question 5";
strArray[7] = "Question 6";
strArray[8] = "Question 7";
strArray[9] = "Question 8";
strArray[10] = "Question 9";
strArray[11] = "Question 10";
strArray[12] = "Question 11";
StartCoroutine (TypeText ());
}
IEnumerator TypeText () {
for (i = 0; i < count; i++) {
foreach (char letter in strArray[i].ToCharArray()) {
gameObject.GetComponent<text> ().text += letter;
yield return new WaitForSeconds (letterPause);
}
if (i != count - 1) {
yield return new WaitForSeconds (sentencePause);
gameObject.GetComponent<text> ().text = "";
}
}
}
IEnumerator TypeText2 () {
gameObject.GetComponent<text> ().text = "";
if(count > strArray.Length - 1) {
//Winninng or loosing condition
} else {
//Show the next question
foreach (char letter in strArray[count - 1].ToCharArray()) {
gameObject.GetComponent<text> ().text += letter;
yield return new WaitForSeconds (letterPause);
}
}
}
public void OnClickButton1() {
button1.GetComponent<text> ().text = "";
button2.GetComponent<text> ().text = "";
button3.GetComponent<text> ().text = "";
button4.GetComponent<text> ().text = "";
count++;
StartCoroutine (TypeText2 ());
}
public void OnClickButton2() {
button1.GetComponent<text> ().text = "";
button2.GetComponent<text> ().text = "";
button3.GetComponent<text> ().text = "";
button4.GetComponent<text> ().text = "";
count++;
StartCoroutine (TypeText2 ());
}
public void OnClickButton3() {
button1.GetComponent<text> ().text = "";
button2.GetComponent<text> ().text = "";
button3.GetComponent<text> ().text = "";
button4.GetComponent<text> ().text = "";
count++;
StartCoroutine (TypeText2 ());
}
public void OnClickButton4() {
button1.GetComponent<text> ().text = "";
button2.GetComponent<text> ().text = "";
button3.GetComponent<text> ().text = "";
button4.GetComponent<text> ().text = "";
count++;
StartCoroutine (TypeText2 ());
}
}
而不是每次都找到组件。
希望这有帮助。您使用什么GUI系统来创建按钮?如果是新的Unity UI系统,只需在此MonoBehavior上创建一个公共方法,并将其链接到按钮的属性中即可。我正在使用新的Unity UI系统,但我不确定您所说的链接到按钮的属性是什么意思。我将第二个协程更改为您的,这导致了混乱[2]重复两次,因此我必须将第一个协同程序的条件更改为for I=0;i
button1.GetComponent<text> ();
text button1Text = button1.GetComponent<text> ();
button1Text.text = "";