UGUI 實現文本打字效果,ugui文本打字效果

來源:互聯網
上載者:User

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 實現這個功能:










聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.