unity实现文本打字机效果(支持富文本)

摘要:
1、 渲染:二。代码如下:usingSystem.Collections;使用System.Collections.Generic;使用UnityEngine;使用UnityEngine.UI;使用系统;publicclassCommonStoryText:BaseMeshEffect{[SerializeField]privateinttextIndex;priv

一.效果图:

unity实现文本打字机效果(支持富文本)第1张

二.代码如下:

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

public class CommonStoryText : BaseMeshEffect
{
    [SerializeField]
    private int textIndex;

    private UIVertex vertex = new UIVertex();

    //记录刷新时间
    private float m_TimeCount = 0;

    private float m_charTime = 0;

    //是否开始显示打字
    private int state = 0;

    //每个字的出现间隔
    private float m_TimeSpace = 0;

    //要显示的Text组件
    private Text m_Text;

    private int alphaIndex = 0;

    private float[] arrVertex;

    private int aniComplete = 0;
    private int m_charLen = 0;
    private int hasInit = 0;

    public override void ModifyMesh(VertexHelper vh)
    {
        if (!IsActive())
        {
            return;
        }
        if(state == 0)
        {
            return;
        }
        m_charLen = vh.currentVertCount/4;
        //文本长度
        if(state == 1)
        {
            for (var i = 0; i < vh.currentVertCount; i++)
            {
                vh.PopulateUIVertex(ref vertex, i);
                Color c = vertex.color;
                c.a = 0;
                vertex.color = c;
                vh.SetUIVertex(vertex, i);
            }
            state = 2;
            return;
        }
        int vCount = 4;
        int vertexIndex = 0;
        int charLen = m_charLen;
        for(int i=0; i<charLen; i++)
        {
            float alpha = arrVertex[i];
            for (int j = 0; j < vCount; j++)
            {
                if (vertexIndex < vh.currentVertCount)
                {
                    vh.PopulateUIVertex(ref vertex, vertexIndex);
                    Color c = vertex.color;
                    c.a = alpha;
                    vertex.color = c;
                    vh.SetUIVertex(vertex, vertexIndex);
                }
                vertexIndex++;
            }
        }
        if(alphaIndex >= charLen && state > 0)
        {
            state = 0;
            aniComplete = 1;
        }

    }
    protected override void Awake()
    {
        m_Text = gameObject.GetComponent<Text>();
        textIndex = 0;
        m_Text.text = "";
    }
    protected override  void Start()
    {
    }
    void Update ()
    {
        if (state == 2)
        {
            if(hasInit == 0)
            {
                hasInit = 1;
                arrVertex = new float[m_charLen];
                for(int i=0; i<m_charLen; i++)
                {
                    arrVertex[i] = 0;
                }
            }
            doText();
            doAlphaText();
        }
        if(aniComplete == 1)
        {
            aniComplete = 0;
            onTextAniComplete();
        }
    }
    private void doText()
    {
        int charLen = m_charLen;
        if(textIndex >= charLen)
        {
            return;
        }
        if (Time.time - m_charTime > 0.05)
        {
            m_charTime = Time.time;
            textIndex++;
        }
    }
    private void doAlphaText()
    {
        if (Time.time - m_TimeCount <= 0.01)
        {
            return;
        }
        m_TimeCount = Time.time;
        int charLen = m_charLen;
        bool needModify = false;
        for(int i=alphaIndex; i<textIndex && i<charLen; i++)
        {
            float alpha = arrVertex[i];
            alpha += 0.1f;
            needModify = true;
            if(alpha >= 1)
            {
                alphaIndex++;
                alpha = 1;
            }
            arrVertex[i] = alpha;
        }
        if(needModify)
        {
            graphic.SetVerticesDirty();
        }
    }
    public void playAni(string contents)
    {
        if(state > 0)
        {
            return;
        }
        m_Text.text = contents;
        hasInit = 0;
        alphaIndex = 0;
        state = 1;
        textIndex = 0;
        graphic.SetVerticesDirty();
    }
    public void stopAni()
    {
        state = 0;
    }
    public void showText()
    {
        if(hasInit == 0)
        {
            return;
        }
        alphaIndex = m_charLen;
        textIndex = m_charLen;
        for(int i=0; i<m_charLen; i++)
        {
            arrVertex[i] = 1.0f;
        }
        graphic.SetVerticesDirty();
    }
    private void onTextAniComplete()
    {

    }
    public void test()
    {
        playAni("这是一段剧情对话,<color=#FF0000>尽量长一点</color>方便测试.");
    }
}

三.新建一个unity工程 创建ui text节点 添加上面的脚本 代码里调用playAni接口.

免责声明:文章转载自《unity实现文本打字机效果(支持富文本)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇关于数据并发JavaEE 对象的串行化(Serialization)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Unity3d监听手机暂停与退出事件

做移动互联网类型的开放,很多情况得考虑移动设备的暂停与退出时,做某些数据操作或UI。 1,退出事件,Unity3d,InPut就包含了: Input.GetKey(KeyCode.Escape) 、 Input.GetKey(KeyCode.Home) 、Input.GetKey(KeyCode.Menu); 2,暂停事件,Unity3d的 OnAppli...

Unity 3D 如何修改新建脚本中的 C# 默认创建的 Script 脚本格式

  首先在Unity的安装路径下找到 Unity5EditorDataResourcesScriptTemplates路径的(81-C# Script-NewBehaviourScript.cs.txt)的模板文件,根据你的需要,把内容修改成自己要想的默认格式就好....

依赖注入之unity(winform方式)

依赖注入之unity(winform方式) 要讲unity就必须先了解DI和IOC及DIP,如下链接提供DI和IOC的基础:https://www.cnblogs.com/zlp520/p/12015973.html 一.什么是unity? unity是实现依赖注入的IOC容器,通过unity可以降低代码的耦合度。 二.下载并添加引用: Microsoft...

关于Unity中的几何体,材质和FBX模型

一、创建几何体的类型 1: 创建平面 Plane;2: 创建立方体 Cube;3: 创建球体 Sphere;4: 创建胶囊体 Capsule;5: 创建圆柱体 Cylinder;6: 3D文字 3D text; 创建出来的几何体,自带这些组件: 1.transform 2.Mesh Filter:网格 3.Collider:碰撞体 4.Mesh Rende...

unity3D游戏开发三之unity编辑器二

转:http://blog.csdn.net/kuloveyouwei/article/details/23020995 下面我们介绍下GameObject,游戏对象/物体,通过游戏对象我们可以创建游戏对象,如灯光、粒子、模型、GUI等。 GameObject菜单 通过Create Other,我们可以创建系统自带的一些游戏对象,具体如下: Partic...

Unity 鼠标旋转物体360展示

PC端 usingUnityEngine; usingSystem.Collections; public classDragRound : MonoBehaviour { publicTransform obj; public float speed = 2; private bool _mouseDown =...