unity 中让Text的文字动态刷新形式
生活随笔
收集整理的這篇文章主要介紹了
unity 中让Text的文字动态刷新形式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
第一種刷新文字形式
using UnityEngine; using System.Collections; using UnityEngine.UI;public class SensorTextRefresh2 {// Use this for initializationstring 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 framefloat 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 initializationstring 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 framefloat 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?
轉載于:https://www.cnblogs.com/Study088/p/9610190.html
總結
以上是生活随笔為你收集整理的unity 中让Text的文字动态刷新形式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux安装vsftpd组件
- 下一篇: 「Codeforces」598E (区间