Unity 脚本,但也许这只是一个我没有看到的明显 C# 错误……
有一个脚本可以逐个字母地“打印”文本。它应用于按钮按钮的文本(文本,按钮的后代)。您希望脚本仅在按钮可单击(可交互)时运行。
我创建了一个公共变量来在 Unity 中将一个按钮拖入其中并使用了 GetComponent() 但由于某种原因它不起作用。
下面是一个简短的脚本。为什么没有成功,错误在哪里?提前致谢!
UPD 脚本略有更新但仍无法正常工作。
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
// attach to UI Text component (with the full text already there)
public class UITextTypeWriter : MonoBehaviour {
public Text txt;
string story;
public Button ThisButton;
void Start()
{
txt = GetComponent<Text>();
story = txt.text;
txt.text = "";
if (ThisButton.interactable) {
StartCoroutine(PlayText());
}
}
IEnumerator PlayText()
{
foreach (char c in story)
{
txt.text += c;
yield return new WaitForSeconds(0.125f);
}
}
}
isActiveAndEnabled -如果组件最初处于活动状态,将返回false ,如果脚本被激活(GO 未处于活动状态,然后启用或停用,然后激活。) - 如果我错了,请纠正我。
以评论为准(当可交互参数改变时触发):