Unity UI.text 逐字渐

using System;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
/// <summary> 
/// 此脚本是能够将文本字符串随着时间打字或褪色显示。 
/// </summary> 
[RequireComponent(typeof(Text))]
[AddComponentMenu("Typewriter Effect")]
public class TypewriterEffect : MonoBehaviour
{
    #region 逐个出字
    public UnityEvent myEvent;
    public string writepoem;
    public string write;
    public string poemlast;
    [HideInInspector]
    public int shiindex;
    private int writeindex;
    private int writeindexpoem;
    private int poemlastindex;
    // public AudioClip mAudioClip;             // 打字的声音,不是没打一个字播放一下,开始的时候播放结束就停止播放 
    private bool isActive = false;
    private int timer;
    private string words;
    private Text mText;
    private int lastindex=0;
    private bool teachershowshi=true;
    #endregion
    public float alphaindex;
    private string originColor;
    private float alpha;
    private float lastalpha;
    void Start()
    {
 #region 逐个出字
        if (myEvent == null)
            myEvent = new UnityEvent();
        words = GetComponent<Text>().text;
        GetComponent<Text>().text = string.Empty;
        timer =0;
        mText = GetComponent<Text>();
        writeindexpoem = words.IndexOf(writepoem);
        writeindex = words.IndexOf(write);
        poemlastindex = words.IndexOf(poemlast);
        #endregion
        originColor = ColorToHex(GetComponent<Text>().color);
    }

    public void OnStart()
    {
        isActive = true;
    }
    void OnStartWriter()
    {
        if (isActive)
        {
                try
                {
                    if (timer > writeindexpoem)
                    {
                        if (timer <writeindex)
                        {
                            mText.text = words.Substring(0, writeindex - 1) + "<color=#" + originColor + Alpha(alpha) + ">" + words.Substring(writeindex - 1, 1) + "</color>" + "</size>";
                            alpha += alphaindex;
                           if (alpha >= 255)
                           {
                              timer = writeindex;
                              mText.text = words.Substring(0, timer)+"</size>";
                              alpha = 0;
                           }
                      
                        }
                        else
                        {
                            if (timer >= poemlastindex)
                            {
                                mText.text = words.Substring(0, poemlastindex) + "<color=#" + originColor + Alpha(alpha) + ">"+words.Substring(poemlastindex, 1) + "</color>"+ "</size>";
                                alpha += alphaindex;
                                if (alpha >= 255)
                               {
                                  timer = words.Length;
                                  mText.text = words.Substring(0, timer-1);
                                  alpha =255;
                                  timer +=1;
                               }
                          
                            }
                            else
                            {
                                mText.text = words.Substring(0, timer)+"<color=#" + originColor + Alpha(alpha) + ">" + words.Substring(timer, 1) + "</color>" + "</size>";
                                alpha += alphaindex;
                                if (alpha >= 255)
                               {
                                  timer += 1;
                                  mText.text = words.Substring(0, timer) + "</size>";
                                  alpha = 0;
                               }
                           }
                        }
                    }
                    else
                    {
                        mText.text = words.Substring(0, timer) + "<color=#" + originColor + Alpha(alpha) + ">" + words.Substring(timer, 1) + "</color>";
                        alpha += alphaindex;
                        if (alpha >=255)
                        {
                           timer += 1;
                           mText.text = words.Substring(0, timer);
                           alpha =0;
                        }
                    }
                    shiindex = timer;
                }
                catch (Exception)
                {
                    OnFinish();
                }
        }
    }
    void OnFinish()
    {
        isActive = false;
        timer =0;
        GetComponent<Text>().text = words;
        try
        {
            myEvent.Invoke();
        }
        catch (Exception)
        {
            Debug.Log("问题");
        }
    }
    void Update()
    {
        if (teachershowshi)
        {
            OnStartWriter();
        }
    }
    private string ColorToHex(Color color)
    {
        int Val = 0;
        Val |= Mathf.RoundToInt(color.r * 255f) << 24;
        Val |= Mathf.RoundToInt(color.g * 255f) << 16;
        Val |= Mathf.RoundToInt(color.b * 255f) << 8;
        Val |= Mathf.RoundToInt(color.a * 255f);
        return Val.ToString("x8").Substring(0, 6); //返回值去掉alpha值
    }
    private string Alpha (float number)
    {
        int a = 0;
        a |= Mathf.RoundToInt(number);
        return a.ToString("x8").Substring(6, 2);
    }
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值