IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    [原]UGUI 实现文本打字效果

    u010019717发表于 2015-06-19 08:53:01
    love 0

    孙广东 2015.6.17

    熟悉NGUI的人可定知道了。

    但是NGUI弄的有些繁琐, 感兴趣的人可以将NGUI的TypewriterEffect类转成 UGUI特定的,因为有些以来的其他脚本,不爱弄。


    我这个只是简化的内容。够用了


    using System;
    using UnityEngine;
    using UnityEngine.UI;
    using UnityEngine.Events;
    
    /// 
    /// 此脚本是能够将文本字符串随着时间打字或褪色显示。
    /// 
    
    [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;
            GetComponent().text = string.Empty;
            timer = 0;
            isActive = true;
            charsPerSecond = Mathf.Max(1, charsPerSecond);
            mText = GetComponent();
        }
    
        void ReloadText()
        {
            words = GetComponent().text;
            mText = GetComponent();
        }
    
        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 = words;
            try
            {
                 myEvent.Invoke();
            }
            catch (Exception)
            {
                Debug.Log("问题");
            }
        }
    
        void Update()
        {
            OnStartWriter();
        }
    }
    

    还没有完


    想使用前几天 介绍的插件 TextFX 实现这个功能:










    


沪ICP备19023445号-2号
友情链接