第一种刷新文字形式

  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4.  
  5. public class SensorTextRefresh2
  6. {
  7.  
  8. // Use this for initialization
  9. string showstring;
  10. public Text _text;
  11. public string otherName = "normal";
  12. string current;
  13. RefreshState refreshState;
  14. int index = ;
  15.  
  16. public void Init(Text _t, string _s, int _index, string _otherName)
  17. {
  18. refreshState = RefreshState.start;
  19.  
  20. _s = float.Parse(_s).ToString("0.00");
  21.  
  22. if (index == )
  23. {
  24. showstring = _s;
  25. current = _s;
  26.  
  27. }
  28. else if (index == )
  29. {
  30.  
  31. //if (_text.transform.parent.name == "transmitter" || _text.transform.parent.name == "temperature1")
  32. {
  33. showstring = _s;
  34. current = _s;
  35. }
  36. //else
  37. //{
  38.  
  39. // showstring = _s + "℃";
  40. // current = _s + "℃";
  41. //}
  42.  
  43. }
  44. else
  45. {
  46.  
  47. }
  48. _text = _t;
  49.  
  50. index = _index;
  51. otherName = _otherName;
  52.  
  53. //Debug.LogError( " " + _text.transform.parent.parent.parent.name);
  54.  
  55. }
  56.  
  57. public void SetString(string _s)
  58. {
  59. //if (refreshState == RefreshState.start)
  60. {
  61. _s = float.Parse(_s).ToString("0.00");
  62.  
  63. if (index == )
  64. {
  65. current = _s;
  66. _text.text = "";
  67. _text.text = current;
  68. }
  69. else if (index == )
  70. {
  71. current = _s;
  72.  
  73. _text.text = "";
  74.  
  75. //if (otherName.Contains("PP"))
  76. //{
  77.  
  78. // Debug.LogError(" " + _text.text);
  79.  
  80. //}
  81. //if (otherName.Contains("TP"))
  82. //{
  83.  
  84. // Debug.LogError(" " + _text.text);
  85.  
  86. //}
  87. //if (otherName.Contains("LP"))
  88. //{
  89.  
  90. // Debug.LogError(" " + _text.text);
  91.  
  92. //}
  93. //if (otherName.Contains("JP"))
  94. //{
  95.  
  96. // Debug.LogError(" " + _text.text);
  97.  
  98. //}
  99. //if (otherName.Contains("YP"))
  100. //{
  101.  
  102. // Debug.LogError(" " + _text.text);
  103.  
  104. //}
  105.  
  106. }
  107. else
  108. {
  109.  
  110. }
  111. }
  112. }
  113.  
  114. // Update is called once per frame
  115. float timer = 1.0f;
  116. float stringLength = ;
  117. float speed = 1.0f;
  118. int i = ;
  119. public void Update()
  120. {
  121.  
  122. if (_text == null)
  123. return;
  124. if (refreshState == RefreshState.start)
  125. {
  126.  
  127. _text.text = "";
  128. refreshState = RefreshState.show;
  129. }
  130. if (refreshState == RefreshState.show)
  131. {
  132. stringLength += Time.deltaTime * speed;
  133. if (stringLength <= )
  134. {
  135.  
  136. string temp = showstring.Substring(, showstring.Length);
  137. //_text.text = temp.Substring(0, (int)stringLength - 1);
  138. //_text.text += "<color=#E7E7E9FF>" + temp.Substring((int)stringLength - 1, 1) + "</color>";
  139. if (otherName.Contains("TP") || otherName.Contains("TF") || otherName.Contains("温度"))
  140. {
  141. _text.text = ("<color=#E7E7E9FF>" + temp.Substring(, showstring.Length) + "</color>" + "℃");
  142.  
  143. }
  144. else if (otherName.Contains("PP") || otherName.Contains("PF") || otherName.Contains("压力"))
  145. {
  146.  
  147. _text.text = "<color=#E7E7E9FF>" + temp.Substring(, showstring.Length) + "</color>" + "Mpa";
  148.  
  149. }
  150. 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("挤压机频率"))
  151. {
  152. _text.text = "<color=#E7E7E9FF>" + temp.Substring(, showstring.Length) + "</color>" + "Hz";
  153. }
  154. else if (otherName.Contains("电流"))
  155. {
  156.  
  157. _text.text = "<color=#E7E7E9FF>" + temp.Substring(, showstring.Length) + "</color>" + "A";
  158.  
  159. }
  160.  
  161. }
  162. else if (stringLength > && stringLength <= )
  163. {
  164.  
  165. if (otherName.Contains("TP") || otherName.Contains("TF") || otherName.Contains("温度"))
  166. {
  167. _text.text = "℃";
  168. }
  169. else if (otherName.Contains("PP") || otherName.Contains("PF") || otherName.Contains("压力"))
  170. {
  171.  
  172. _text.text = "Mpa";
  173. }
  174. 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("挤压机频率"))
  175. {
  176. _text.text = "Hz";
  177. }
  178. else if (otherName.Contains("电流"))
  179. {
  180.  
  181. _text.text = "A";
  182.  
  183. }
  184. }
  185. else
  186. {
  187. if (otherName.Contains("TP") || otherName.Contains("TF") || otherName.Contains("温度"))
  188. {
  189. _text.text = showstring.Substring(, (int)showstring.Length) + "℃"; ;
  190. }
  191. else if (otherName.Contains("PP") || otherName.Contains("PF") || otherName.Contains("压力"))
  192. {
  193. _text.text = showstring.Substring(, (int)showstring.Length) + "Mpa";
  194. }
  195. 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("挤压机频率"))
  196. {
  197. _text.text = showstring.Substring(, (int)showstring.Length) + "Hz";
  198. }
  199. else if (otherName.Contains("电流"))
  200. {
  201.  
  202. _text.text = showstring.Substring(, (int)showstring.Length) + "A";
  203.  
  204. }
  205.  
  206. }
  207.  
  208. if (stringLength > 3.0f)
  209. {
  210. //结束
  211. stringLength = ;
  212. refreshState = RefreshState.end;
  213. }
  214. }
  215. if (refreshState == RefreshState.delay)
  216. {
  217.  
  218. _text.text = "";
  219.  
  220. stringLength += Time.deltaTime;
  221. if (stringLength >= timer)
  222. {
  223. refreshState = RefreshState.end;
  224. }
  225. }
  226. if (refreshState == RefreshState.end)
  227. {
  228. stringLength = ;
  229. if (!showstring.Equals(current))
  230. {
  231. showstring = current;
  232.  
  233. }
  234. refreshState = RefreshState.start;
  235. }
  236.  
  237. }
  238.  
  239. enum RefreshState
  240. {
  241. start,
  242. show,
  243. delay,
  244. end
  245. }
  246. }

第二种刷新文字形式

  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4.  
  5. public class SensorTextRefresh1
  6. {
  7.  
  8. // Use this for initialization
  9. string showstring;
  10. public Text _text;
  11. string current;
  12. RefreshState refreshState;
  13. int index = ;
  14. public void Init(Text _t, string _s)
  15. {
  16. refreshState = RefreshState.start;
  17.  
  18. {
  19. showstring = _s + "℃";
  20. current = _s + "℃";
  21. }
  22.  
  23. _text = _t;
  24. }
  25. public void SetString(string _s)
  26. {
  27. if (refreshState == RefreshState.start)
  28. {
  29.  
  30. {
  31. current = _s + "℃";
  32. _text.text = "";
  33. }
  34. }
  35. }
  36.  
  37. // Update is called once per frame
  38. float timer = 0.3f;
  39. float stringLength = ;
  40. float speed = 4.0f;
  41. public void Update()
  42. {
  43. if (_text == null)
  44. return;
  45. if (refreshState == RefreshState.start)
  46. {
  47. _text.text = "";
  48. refreshState = RefreshState.show;
  49. }
  50. if (refreshState == RefreshState.show)
  51. {
  52. stringLength += Time.deltaTime * speed;
  53. int a = (int)(stringLength / 0.5f);
  54. if (a % == )
  55. {
  56. //_text.text = showstring.Substring(0, (int)stringLength) + "█";
  57. //_text.text = showstring.Substring(0, (int)stringLength) + "<color=#c6ff00ff>█</color>";
  58. string temp = showstring.Substring(, (int)stringLength);
  59. _text.text = temp.Substring(, (int)stringLength - );
  60. _text.text += "<color=#E7E7E9FF>" + temp.Substring((int)stringLength - , ) + "</color>";
  61. }
  62. else
  63. {
  64. _text.text = showstring.Substring(, (int)stringLength);
  65. }
  66. //_text.text = showstring.Substring(0, (int)stringLength);
  67. if(_text.text.Equals(showstring))
  68. {
  69. //结束
  70. stringLength = ;
  71. refreshState = RefreshState.delay;
  72. }
  73. }
  74. if (refreshState == RefreshState.delay)
  75. {
  76. stringLength += Time.deltaTime;
  77. if(stringLength>=timer)
  78. {
  79. refreshState = RefreshState.end;
  80. }
  81. }
  82. if(refreshState == RefreshState.end)
  83. {
  84. stringLength = ;
  85. if(!showstring.Equals(current))
  86. {
  87. showstring = current;
  88. }
  89. refreshState = RefreshState.start;
  90. }
  91.  
  92. }
  93.  
  94. enum RefreshState
  95. {
  96. start,
  97. show,
  98. delay,
  99. end
  100. }
  101. }

unity 中让Text的文字动态刷新形式的更多相关文章

  1. 3D语音天气球(源码分享)——在Unity中使用Android语音服务

    转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! 开篇废话: 这个项目准备分四部分介绍: 一:创建可旋转的"3D球":3 ...

  2. Unity中的万能对象池

    本文为博主原创文章,欢迎转载.请保留博主链接http://blog.csdn.net/andrewfan Unity编程标准导引-3.4 Unity中的万能对象池 本节通过一个简单的射击子弹的示例来介 ...

  3. Unity中的GC以及优化

    [简介] 常见的 Unity GC 知识点总结出来的思维导图 Unity 官方文档,正巧在博客园发现了已经有位大神(zblade)把原文翻译出来了,而且质量很高~,译文地址 在这里.下面我就可耻地把译 ...

  4. 关于Unity中的Bmpfont的使用

    系统字体,不占空间,效果一般. 自己拖的.TTF文件形成的字体,占空间很大,有特殊效果.一个TTF字库差不多占用3M左右,之所以这么大,是因为里面包含了所有的字,就像一本字典一样,所以字符都在里面. ...

  5. 浅谈Unity中的GC以及优化

    介绍: 在游戏运行的时候,数据主要存储在内存中,当游戏的数据不在需要的时候,存储当前数据的内存就可以被回收再次使用.内存垃圾是指当前废弃数据所占用的内存,垃圾回收(GC)是指将废弃的内存重新回收再次使 ...

  6. Unity中对SQL数据库的操作

    在Unity中,我们有时候需要连接数据库来达到数据的读取与储存.而在.NET平台下,ADO.NET为我们提供了公开数据访问服务的类.客户端应用程序可以使用ADO.NET来连接到数据源,并查询,添加,删 ...

  7. 关于Unity中的UGUI优化,你可能遇到这些问题

    https://blog.uwa4d.com/archives/QA_UGUI-1.html 关于Unity中的UGUI优化,你可能遇到这些问题 作者:admin / 时间:2016年11月08日 / ...

  8. 解读Unity中的CG编写Shader系列3——表面剔除与剪裁模式

    在上一个样例中,我们得到了由mesh组件传递的信息经过数学转换至合适的颜色区间以颜色的形式着色到物体上. 这篇文章将要在此基础上研究片段的擦除(discarding fragments)和前面剪裁.后 ...

  9. Unity TextMeshPro替代Text组件创建简体中文字体纹理集

    Unity原生的Text组件有一个毛病,只要文本放大字体放大就会有毛边或锯齿,一个更好的解决方案是用TextMeshPro替代ugui中的Text组件. TMPro采用SDF文字渲染技术,可以使文字放 ...

随机推荐

  1. HTTP 400错误--请求无效

    在发送请求后台数据时会报出来HTTP400错误,请求无效,出现这个请求无效报错说明请求没有进入到后台服务里 原因:1.前端提交数据的字段名称或者是字段类型和后台的实体类不一致.导致无法封装 2.前端提 ...

  2. cv2.getRotationMatrix2D函数

  3. Docker Swarm Mode 学习笔记 (部署服务)

    使用 docker service 命令来管理 Swarm 集群中的服务,该命令只能在管理节点上执行. 新建服务 docker service create --replicas 3 -p 80:80 ...

  4. Eclipse使用Maven,创建项目出现:Could not calculate build plan: Plugin org.apache.maven.plugins:maven-resour

    使用maven创建简单的项目时候经常会遇到 Could not calculate build plan: Plugin org.apache.maven.plugins:maven-resource ...

  5. Go 参数传递是传值还是传引用

    什么是传值(值传递)? 传值的意思是:函数传递的总是原来这个东西的一个副本.一个副拷贝.比如我们传递一个 int 类型的参数,传递 的其实这个参数的一个副本:传递一个指针类型的参数,其实传递的是这个指 ...

  6. JS及相关控件

    1.radio 1)不选中任何值 2)获取选中的值 3)让某个选项选中 4)发生改变时的事件 5)让某个选项不能选 2.CheckBox 1)选中 2)取消 3.select 1)获取下拉框选中项的显 ...

  7. 使用VSCode调试Jest

    0. 环境 Node版本:8.12.0 操作系统:windows10 1. 配置launch.json { "version": "0.2.0", " ...

  8. tomcat中显示本地图片①(已解决)

    解决方案 我直接放源码了. 原理就是:我虽然调用的是虚拟目录,但是会映射到对应路径的实际 第一步:(在tomcat的 server.xml中创建一个虚拟目录) 虚拟目录创建方式: <Contex ...

  9. kubernetes1.13.5安装部署

    Kubernetes 一.    Kubernetes介绍 各节点所需组件 Master:docker,kubeadm,kubelet, 组件 版本 备注 Kubelet 1.13.5 组件 Kube ...

  10. Project Euler 66: Diophantine equation

    题目链接 思路: 连分数求佩尔方程最小特解 参考博客 模板: LL a[]; bool min_pell(LL d, LL &x, LL &y) { LL m = floor(sqrt ...