问题描述
对于 2D 平台游戏的过场动画,我编写了一个脚本,可以像打字机一样显示文本。由于文本可能很长,我想为用户实现一个选项来快进/跳过动画并在按键时显示全文。这就是我现在所拥有的:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TypeWriter : MonoBehavIoUr
{
public float delay = 0.05f;
public string fullText;
private string currentText = "";
// Start is called before the first frame update
void Start()
{
StartCoroutine(showtext());
}
IEnumerator showtext()
{
for (int i = 0; i < fullText.Length + 1; i++)
{
currentText = fullText.Substring(0,i);
this.GetComponent<Text>().text = currentText;
yield return new WaitForSeconds(delay);
}
}
}
有人可以帮我吗?我是 unity 和 C# 的新手。
解决方法
设置标志应该可以解决问题:
"json5": {
"version": "1.0.1","resolved": "https://........","integrity": "sha1-d........","requires": {
"minimist": "^1.2.0"
},"dependencies": {
"minimist": {
"version": "1.2.0","resolved": "https://npm.......","integrity": "sha1-......="
}
}
},
只需在某个时候将 bool skip = false;
IEnumerator ShowText()
{
for (int i = 0; i < fullText.Length + 1; i++)
{
currentText = fullText.Substring(0,i);
this.GetComponent<Text>().text = currentText;
if (!skip)
yield return new WaitForSeconds(delay);
}
}
设置为 skip
,它就会导致循环不屈服,这将导致循环完全完成(一个小的优化是在跳过标志时不循环)已设置并只用剩余的字母填充文本,但我怀疑即使使用循环也会有性能问题,因为它如此简单)。
您可以根据接收到的按键/某些输入将标志设置为 true。