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

第二种刷新文字形式

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

 

posted @ 2018-09-08 19:47  U3DEngineer  阅读(5469)  评论(0编辑  收藏  举报