unity3d使用GUI實現打字機的文字顯示效果
阿新 • • 發佈:2019-01-25
程式碼也不復雜,直接上好了
using UnityEngine; using System.Collections; public class GUIPrintMachine : MonoBehaviour { /// <summary> /// 間隔時間 /// </summary> private float letterPause = 0.2f; public AudioClip clip; private AudioSource source; /// <summary> /// 暫存中間值 /// </summary> private string word; /// <summary> /// 要顯示的內容 /// </summary> private string text = "I'm Eagle,賴張殷,22,1994,handsome,live in guuangzhou"; void Start() { source = GetComponent<AudioSource>(); word = text; text = ""; StartCoroutine(TypeText()); } void OnGUI() { GUI.Label(new Rect(100, 100, 200, 200), "text show"); GUI.Label(new Rect(50, 50, 250, 250), text); } /// <summary> /// 打字機效果 /// </summary> /// <returns></returns> private IEnumerator TypeText() { foreach (char letter in word.ToCharArray()) { text += letter; if(clip ) { source.PlayOneShot(clip); } yield return new WaitForSeconds(letterPause); } } }