UGUI文字打印效果

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class TypeEffect : MonoBehaviour {

    Text typeText;
    string typeContent;

    float typeInterval = 0.2f;
    float typePreTime = 0;

    int typeLength = 0;
    bool isTyping = false;

    string strHead = "<color=#ff0000ff><size=25>";
    string strTail = "</size></color>";
	void Start () {
        typeText = GetComponent<Text>();
        typeContent = typeText.text;
        typePreTime = Time.time;
        isTyping = true;
	}

	void Update () {
        Effect();
	}
    void Effect()
    {
        if (isTyping)
        {
            if (Time.time-typePreTime > typeInterval)
            {
                typePreTime = Time.time;
                typeLength++;
                if (typeLength < typeContent.Length)
                {
                    typeText.text = strHead + typeContent.Substring(0, typeLength) + strTail;
                }
                else
                {
                    typeText.text = strHead + typeContent + strTail;
                    isTyping = false;
                }
            }
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章