【发布时间】:2023-04-02 04:01:01
【问题描述】:
在下面的文件中,我使用视频that you can find here 在屏幕上制作打字机效果。我现在遇到了一些问题。
无论出于何种原因,每当我使用它时,它都会切断最后一个字母(即输入“Hello there”会输出“Hello ther”)。关于为什么会发生这种情况的任何想法?
我正在使用的编程是一个修改版本以适合我的游戏:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class TypeWriterEffect : MonoBehaviour {
public float delay = 0.1f;
public float startDelay = 0f;
public string fullText;
public bool showGameHS;
public bool showTotalScore;
public string totalGameScore;
private string currentText = "";
// Use this for initialization
void Start () {
totalGameScore = PlayerPrefs.GetString("totalGameScore"); // total score throughout the game
if (showTotalScore)
{
fullText = TimerScript.fullScore.ToString() + "."; // a "." is added to fix this issue
}
else if (showGameHS) // the local highscore
{
fullText = totalGameScore + "."; // a "." is added to fix this issue
}
StartCoroutine(ShowText());
}
IEnumerator ShowText(){
yield return new WaitForSeconds(startDelay); // this was added on as a basic start delay
for (int i = 0; i < fullText.Length; i++){
currentText = fullText.Substring(0,i);
this.GetComponent<Text>().text = currentText;
yield return new WaitForSeconds(delay);
}
}
}
【问题讨论】:
-
i < fullText.Length总是在字符串的完整长度之前停止一个索引 -> 最后一个符号的子字符串切割
标签: c# unity3d user-interface text