如何在Unity中使用space栏控制gui文本?

How to control gui text using the space bar in Unity?

(免责声明:我是 Unity 的新手,所以请多多包涵)

基本上我想要一个文本元素,一次显示多个句子一个字母,并在句子结尾处停止。此外,如果按下 space 栏,则应立即显示该句子的剩余文本,但如果该句子已完成,则应显示下一个句子。

到目前为止,我已经设法使用存储在数组中的字符串让文本一次显示一个字母。但是,我现在很难使用 space 栏从一个索引导航到另一个索引,如果按下 space 栏,我也很难显示完整的句子。

代码如下:

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

public class TextScript : MonoBehaviour {

public Animator bar;
public float letterPause = 0.1f;
string[] strArray = new string[3];
string str;
int i;
int count;

void Start () {
    bar.enabled = true;

    strArray[0] = "Hello and welcome to the game";
    strArray[1] = "The is the next line of code";
    strArray[2] = "Testing the space bar";
    gameObject.GetComponent<Text> ().text = "";
    StartCoroutine(TypeText ());
}

void Update () {
    if (Input.GetKeyDown ("space")) {
        gameObject.GetComponent<Text> ().text = "";
        count = count + 1;
        i = i + 1;
        TypeText();
    }
}

IEnumerator TypeText () {
    for (i = 0; i < strArray.Length; i++) {
        str = strArray[i];
        if (i == count) {
            foreach (char letter in str.ToCharArray()) {
                gameObject.GetComponent<Text> ().text += letter;
                yield return new WaitForSeconds (letterPause);
            }
        }
    }
}

}

所以,我不确定实现我真正想要的东西的最佳方法是什么。任何帮助都会很棒!

我在这个盒子上没有统一来测试这个但是试试这个

public float letterPause = 0.1f;
public float SentencePause = 2.0f;
string[] strArray = new string[3];
bool skipToNext = false;

void Start () {

    strArray[0] = "Hello and welcome to the game";
    strArray[1] = "The is the next line of code";
    strArray[2] = "Testing the space bar";
    gameObject.GetComponent<Text> ().text = "";
    StartCoroutine(TypeText ());
}

void Update () {
    if (Input.GetKeyDown ("space")) {
        skipToNext = true;
    }
}

IEnumerator TypeText () {
    for (int i = 0; i < strArray.Length; i++) 
    {
        foreach (char letter in strArray[i].ToCharArray()) 
        {
            if (skipToNext)
            {
                gameObject.GetComponent<Text> ().text = strArray[i];
                skipToNext = false;
                break;
            }

            gameObject.GetComponent<Text> ().text += letter;
            yield return new WaitForSeconds (letterPause);
        }
        yield return new WaitForSeconds (SentencePause);
        gameObject.GetComponent<Text> ().text = "";
    }

    gameObject.GetComponent<Text> ().text = strArray[strArray.Length - 1];
}

已编辑以反映评论更新。