using UnityEngine;
using System.Collections;
public class GUIPrintMachine : MonoBehaviour {
//间隔时间
private float letterPause = 0.2f;
public AudioClip clip;
private AudioSource source;
private string word; // 要显示的内容
private string text = "text test";
void Start() {
source = GetComponent();
word = text;
text = "";
StartCoroutine(TypeText());
}
void OnGUI() {
GUI.Label(new Rect(100, 100, 200, 200), "我叫张志勇,很帅气!");
GUI.Label(new Rect(50, 50, 250, 250), text);
}
private IEnumerator TypeText() {
foreach (char letter in word.ToCharArray()) {
text += letter;
if(clip ){
source.PlayOneShot(clip);
}
yield return new WaitForSeconds(letterPause);
}
}
}
这是GUI做的,当然现在这么流行UGUI,可以直接在这里改成UGUI,
1.Hierarchy中创建Text,可以修改一下Vertical Overflow用来测试
2.引入UI,c#中public一个Text
3.OnGUI中的闪出了,直接在Update中赋值Text.text = textString;
别忘了运行时候把Text拖文件上
如果是NGUI的话这样搞
NGUI创建Label,添加组件Typewriter Effect
Chars Per Second : 每秒显示多少字符