unity 中让Text的文字动态刷新形式

Wesley13
• 阅读 604

第一种刷新文字形式

unity 中让Text的文字动态刷新形式 unity 中让Text的文字动态刷新形式

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

public class SensorTextRefresh2
{

    // Use this for initialization
    string showstring;
    public Text _text;
    public string otherName = "normal";
    string current;
    RefreshState refreshState;
    int index = 0;

    public void Init(Text _t, string _s, int _index, string _otherName)
    {
        refreshState = RefreshState.start;

        _s = float.Parse(_s).ToString("0.00");

        if (index == 0)
        {
            showstring = _s;
            current = _s;

        }
        else if (index == 1)
        {

            //if (_text.transform.parent.name == "transmitter" || _text.transform.parent.name == "temperature1")
            {
                showstring = _s;
                current = _s;
            }
            //else
            //{

            //    showstring = _s + "℃";
            //    current = _s + "℃";
            //}

        }
        else
        {

        }
        _text = _t;

        index = _index;
        otherName = _otherName;


        //Debug.LogError( "     " + _text.transform.parent.parent.parent.name);



    }

    public void SetString(string _s)
    {
        //if (refreshState == RefreshState.start)
        {
            _s = float.Parse(_s).ToString("0.00");

            if (index == 0)
            {
                current = _s;
                _text.text = "";
                _text.text = current;
            }
            else if (index == 1)
            {
                current = _s;

                _text.text = "";

                //if (otherName.Contains("PP"))
                //{

                //    Debug.LogError("    " + _text.text);

                //}
                //if (otherName.Contains("TP"))
                //{

                //    Debug.LogError("    " + _text.text);

                //}
                //if (otherName.Contains("LP"))
                //{

                //    Debug.LogError("    " + _text.text);

                //}
                //if (otherName.Contains("JP"))
                //{

                //    Debug.LogError("    " + _text.text);

                //}
                //if (otherName.Contains("YP"))
                //{

                //    Debug.LogError("    " + _text.text);

                //}

            }
            else
            {

            }
        }
    }

    // Update is called once per frame
    float timer = 1.0f;
    float stringLength = 0;
    float speed = 1.0f;
    int i = 0;
    public void Update()
    {

        if (_text == null)
            return;
        if (refreshState == RefreshState.start)
        {

            _text.text = "";
            refreshState = RefreshState.show;
        }
        if (refreshState == RefreshState.show)
        {
            stringLength += Time.deltaTime * speed;
            if (stringLength <= 1)
            {

                string temp = showstring.Substring(0, showstring.Length);
                //_text.text = temp.Substring(0, (int)stringLength - 1);
                //_text.text += "<color=#E7E7E9FF>" + temp.Substring((int)stringLength - 1, 1) + "</color>";              
                if (otherName.Contains("TP") || otherName.Contains("TF") || otherName.Contains("温度"))
                {
                    _text.text = ("<color=#E7E7E9FF>" + temp.Substring(0, showstring.Length) + "</color>" + "℃");

                }
                else if (otherName.Contains("PP") || otherName.Contains("PF") || otherName.Contains("压力"))
                {

                    _text.text = "<color=#E7E7E9FF>" + temp.Substring(0, showstring.Length) + "</color>" + "Mpa";

                }
                else if (otherName.Contains("JP") || otherName.Contains("YP") || otherName.Contains("LP") || otherName.Contains("JF") || otherName.Contains("YF") || otherName.Contains("LF") || otherName.Contains("计量泵") || otherName.Contains("油剂泵") || otherName.Contains("挤压机频率"))
                {
                    _text.text = "<color=#E7E7E9FF>" + temp.Substring(0, showstring.Length) + "</color>" + "Hz";
                }
                else if (otherName.Contains("电流"))
                {

                    _text.text = "<color=#E7E7E9FF>" + temp.Substring(0, showstring.Length) + "</color>" + "A";

                }

            }
            else if (stringLength > 1 && stringLength <= 2)
            {

                if (otherName.Contains("TP") || otherName.Contains("TF") || otherName.Contains("温度"))
                {
                    _text.text = "℃";
                }
                else if (otherName.Contains("PP") || otherName.Contains("PF") || otherName.Contains("压力"))
                {

                    _text.text = "Mpa";
                }
                else if (otherName.Contains("JP") || otherName.Contains("YP") || otherName.Contains("LP") || otherName.Contains("JF") || otherName.Contains("YF") || otherName.Contains("LF") || otherName.Contains("计量泵") || otherName.Contains("油剂泵") || otherName.Contains("挤压机频率"))
                {
                    _text.text = "Hz";
                }
                else if (otherName.Contains("电流"))
                {

                    _text.text = "A";

                }
            }
            else
            {
                if (otherName.Contains("TP") || otherName.Contains("TF") || otherName.Contains("温度"))
                {
                    _text.text = showstring.Substring(0, (int)showstring.Length) + "℃"; ;
                }
                else if (otherName.Contains("PP") || otherName.Contains("PF") || otherName.Contains("压力"))
                {
                    _text.text = showstring.Substring(0, (int)showstring.Length) + "Mpa";
                }
                else if (otherName.Contains("JP") || otherName.Contains("YP") || otherName.Contains("LP") || otherName.Contains("JF") || otherName.Contains("YF") || otherName.Contains("LF") || otherName.Contains("计量泵") || otherName.Contains("油剂泵") || otherName.Contains("挤压机频率"))
                {
                    _text.text = showstring.Substring(0, (int)showstring.Length) + "Hz";
                }
                else if (otherName.Contains("电流"))
                {

                    _text.text = showstring.Substring(0, (int)showstring.Length) + "A";

                }

            }

            if (stringLength > 3.0f)
            {
                //结束
                stringLength = 0;
                refreshState = RefreshState.end;
            }
        }
        if (refreshState == RefreshState.delay)
        {

            _text.text = "";


            stringLength += Time.deltaTime;
            if (stringLength >= timer)
            {
                refreshState = RefreshState.end;
            }
        }
        if (refreshState == RefreshState.end)
        {
            stringLength = 0;
            if (!showstring.Equals(current))
            {
                showstring = current;

            }
            refreshState = RefreshState.start;
        }

    }

    enum RefreshState
    {
        start,
        show,
        delay,
        end
    }
}

View Code

第二种刷新文字形式

unity 中让Text的文字动态刷新形式 unity 中让Text的文字动态刷新形式

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

public class SensorTextRefresh1
{

    // Use this for initialization
    string showstring;
    public Text _text;
    string current;
    RefreshState refreshState;
    int index = 0;
    public void Init(Text _t, string _s)
    {
        refreshState = RefreshState.start;

        {
            showstring = _s + "℃";
            current = _s + "℃";
        }

        _text = _t;
    }
    public void SetString(string _s)
    {
        if (refreshState == RefreshState.start)
        {

            {
                current = _s + "℃";
                _text.text = "";
            }
        }
    }

    // Update is called once per frame
    float timer = 0.3f;
    float stringLength = 0;
    float speed = 4.0f;
    public void Update()
    {
        if (_text == null)
            return;
        if (refreshState == RefreshState.start)
        {
            _text.text = "";
            refreshState = RefreshState.show;
        }
        if (refreshState == RefreshState.show)
        {
            stringLength += Time.deltaTime * speed;
            int a = (int)(stringLength / 0.5f);
            if (a % 2 == 0)
            {
                //_text.text = showstring.Substring(0, (int)stringLength) + "█";
                //_text.text = showstring.Substring(0, (int)stringLength) + "<color=#c6ff00ff>█</color>";
                string temp = showstring.Substring(0, (int)stringLength);
                _text.text = temp.Substring(0, (int)stringLength - 1);
                _text.text += "<color=#E7E7E9FF>" + temp.Substring((int)stringLength - 1, 1) + "</color>";
            }
            else
            {
                _text.text = showstring.Substring(0, (int)stringLength);
            }
            //_text.text = showstring.Substring(0, (int)stringLength);
            if(_text.text.Equals(showstring))
            {
                //结束
                stringLength = 0;
                refreshState = RefreshState.delay;
            }
        }
        if (refreshState == RefreshState.delay)
        {
            stringLength += Time.deltaTime;
            if(stringLength>=timer)
            {
                refreshState = RefreshState.end;
            }
        }
        if(refreshState == RefreshState.end)
        {
            stringLength = 0;
            if(!showstring.Equals(current))
            {
                showstring = current;
            }
            refreshState = RefreshState.start;
        }

    }

    enum RefreshState
    {
        start,
        show,
        delay,
        end
    }
}

View Code

点赞
收藏
评论区
推荐文章
待兔 待兔
5个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Easter79 Easter79
3年前
vue element table 表头添加图标
!(https://oscimg.oschina.net/oscnet/189d567e07e0844612470867b06a35c0aaf.jpg)!(https://oscimg.oschina.net/oscnet/dc3b454d663d8618924b78796345a145b3f.jpg)1<template
Stella981 Stella981
3年前
Jira & Confluence 在敏捷转型中的重要性
!(https://oscimg.oschina.net/oscnet/543410bd0b319367933d992f9d80d35cd54.jpg)Atlassian产品的设计理念!(https://oscimg.oschina.net/oscnet/fac3d33081927df5f8a26204c98
Stella981 Stella981
3年前
Nebula Graph 在微众银行的实践
本文为微众银行大数据平台工程师——周可在nMeetup深圳场的演讲文字稿,演讲视频参见:B站(戳「阅读原文」观看)!(https://oscimg.oschina.net/oscnet/884d81502c744b76be00eb2a1b07d3c7.png)自我介绍下,我是微众银行大数据平台的工程
Stella981 Stella981
3年前
KaliTools说明书+BurpSuit实战指南+SQL注入知识库+国外渗透报告
!(https://oscimg.oschina.net/oscnet/d1c876a571bb41a7942dd9752f68632e.gif"15254461546.gif")0X00KaliLinux Tools中文说明书!(https://oscimg.oschina.net/oscnet/
Stella981 Stella981
3年前
Android So动态加载 优雅实现与原理分析
背景:漫品Android客户端集成适配转换功能(基于目标识别(So库35M)和人脸识别库(5M)),导致apk体积50M左右,为优化客户端体验,决定实现So文件动态加载.!(https://oscimg.oschina.net/oscnet/00d1ff90e4b34869664fef59e3ec3fdd20b.png)点击上方“蓝字”关注我
Stella981 Stella981
3年前
Jenkins流水线即代码之扩展共享库
!(https://oscimg.oschina.net/oscnet/ab8ee75c43cb1a3fd0fac241648861b03c5.gif)!(https://oscimg.oschina.net/oscnet/1a35fdf03222f188f706711d2b43eae6a14.gif)!(https://osci
Wesley13 Wesley13
3年前
mysql系列(三)——mysql架构与存储引擎
一、MySql逻辑架构!(https://oscimg.oschina.net/oscnet/up35e1824330cd1df7ac66652b9b1dbc41965.png)1.连接层!(https://oscimg.oschina.net/oscnet/up272cb2634e262cc2df52220a063
Stella981 Stella981
3年前
Python_爬虫_案例汇总:
1.豆瓣采集!(https://oscimg.oschina.net/oscnet/29e918bf2ebced4da903f8ab789cd37ca35.jpg)!(https://oscimg.oschina.net/oscnet/83a646b5655e70dc379744e8ebfe145238d.jpg)1codin
Stella981 Stella981
3年前
Eclipse 中的Maven常见报错及解决方法
1.不小心将项目中的MavenDependencies删除报错!(https://oscimg.oschina.net/oscnet/fd35e500e2580bca2afb81f35233b87a6ee.png)项目报错:!(https://oscimg.oschina.net/oscnet/8623bd4293fea39ca83a6