UGUI 實現文本打字效果,ugui文本打字效果
孫廣東 2015.6.17
熟悉NGUI的人可定知道了。
但是NGUI弄的有些繁瑣, 感興趣的人可以將NGUI的TypewriterEffect類轉成 UGUI特定的,因為有些以來的其他指令碼,不愛弄。
我這個只是簡化的內容。夠用了
using System;using UnityEngine;using UnityEngine.UI;using UnityEngine.Events;/// <summary>/// 此指令碼是能夠將文本字串隨著時間打字或褪色顯示。/// </summary>[RequireComponent(typeof(Text))][AddComponentMenu("Typewriter Effect")]public class TypewriterEffect : MonoBehaviour{ public UnityEvent myEvent; public int charsPerSecond = 0; // public AudioClip mAudioClip; // 打字的聲音,不是沒打一個字播放一下,開始的時候播放結束就停止播放 private bool isActive = false; private float timer; private string words; private Text mText; void Start() { if (myEvent == null) myEvent = new UnityEvent(); words = GetComponent<Text>().text; GetComponent<Text>().text = string.Empty; timer = 0; isActive = true; charsPerSecond = Mathf.Max(1, charsPerSecond); mText = GetComponent<Text>(); } void ReloadText() { words = GetComponent<Text>().text; mText = GetComponent<Text>(); } public void OnStart() { ReloadText(); isActive = true; } void OnStartWriter() { if (isActive) { try { mText.text = words.Substring(0, (int) (charsPerSecond * timer)); timer += Time.deltaTime; } catch (Exception) { OnFinish(); } } } void OnFinish() { isActive = false; timer = 0; GetComponent<Text>().text = words; try { myEvent.Invoke(); } catch (Exception) { Debug.Log("問題"); } } void Update() { OnStartWriter(); }}
還沒有完
想使用前幾天 介紹的外掛程式 TextFX 實現這個功能: