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的文字动态刷新形式相关推荐
- Unity中的Text内容有空格导致换行,以及让每行首字符不出现标点符号
ps:本篇内容应该属于半转载半原创,因为空格换行和首字符不出现标点.都是查询资料后我借用别人的成果再进行的整理的.但是发布时转载和原创只能选择一个因此我就选择其一了. 先来说下错误换行的问题,Unit ...
- 浅谈Unity中的文字显示
以UGUI系统为例,字体分为内置字体.外部导入字体.自定义字体. 文字一般通过Text组件进行显示.目前Unity已经官方支持TextMeshPro,因此也有一些文字通过TextMeshPro-Tex ...
- Unity UGUI 之 实现 Text 文本文字过长,省略号显示(TextMeshPro 和常规 Text 二种方法)
Unity UGUI 之 实现 Text 文本文字过长,省略号显示(TextMeshPro 和常规 Text 二种方法) 目录 Unity UGUI 之 实现 Text 文本文字过长,省略号显示(Te ...
- 【Unity】用Text+Image实现神奇的文字下划线
写需求的时候搜了一些文字加下划线的方法,UGUI的Text,大都需要写一堆代码来封装Text,比较麻烦 考虑过用TextMeshPro,不过TextMeshPro不能公用Text的字体,有点坑 利用T ...
- 【100个 Unity踩坑小知识点】 | Unity中Text文本框 和 InputField文本输入框 内容换行问题
Unity 小科普 老规矩,先介绍一下 Unity 的科普小知识: Unity是 实时3D互动内容创作和运营平台 . 包括游戏开发.美术.建筑.汽车设计.影视在内的所有创作者,借助 Unity 将创意 ...
- Unity中Text组件段首如何缩进两个字符
Unity中Text组件段首如何缩进两个字符 我们在使用Unity的Text组件时,如果想实现段首缩进两个字符的功能,一般情况下会想到添加空格的字符如"\0\0",来实现,但是应该 ...
- Unity中的GUI编程
原文地址 在这篇文章中我将给读者介绍Unity中的图形用户界面(GUI)编程.Unity有一个非常强大的GUI脚本API.它允许你使用脚本快速创建简单的菜单和GUI. 简介 Unity提供了使用脚本创 ...
- 在Unity中构建Pong克隆:UI和游戏玩法
In Part 1 of this tutorial – the Retro Revolution: Building a Pong Clone in Unity – we created a Pon ...
- 关于Unity中的UGUI优化,你可能遇到这些问题
一.界面制作 Q1:UGUI里的这个选项 ,应该是ETC2拆分Alpha通道的意思,但是在使用中并没起作用?请问有没有什么拆分的标准和特别要求呢? 据我们所知,alpha split 的功能最初只对 ...
最新文章
- 字节老板在群里diss员工:上班时间聊游戏,工作很闲吗?员工回怼:查聊天记录,看聊天时间占工作时间百分比!网友:真·扁平化管理!...
- Android Launcher3(一) -- 启动过程
- docker端口映射或启动容器时报错 driver failed programming external connectivity on endpoint quirky_allen
- python 比较list差异
- 如何应対syn flood
- mysql分页查询语法
- 应用化学:类卤代甲烷CH3X结构的神经网络
- ASP.NET学习路线图
- KUKA 声明变量时的几点注意
- 讨论群问题:Linux 下的批量操作,第2种很赞
- 关于图像高速缩放算法,目前看到的最好的最清晰的一篇文章2
- s081-2020 Lab4 traps
- 写给新手看的机器学习原理--基于机器翻译
- 使用JavaScript进行iframe的DOM操作
- win7系统创建打印服务器,图文分享win7下添加打印服务器端口
- 【日常】从批量合并 PDF 到 PyPDF2 的使用
- Android 引入布局
- sql server技术知识
- 集合竞价与连续竞价01
- 通过SPSS使用命令语法实现快速删除变量的步骤
热门文章
- pycharm如何改为中文显示?
- 利用Spring的Aop实现项目的日志监控
- Jenkins构建之常用的触发器和Git Hook自动构建
- 高斯拟合原理_AlexNet原理和实现
- 解决Java Web项目无法读取配置文件问题
- Android自定义sleep图,android自定义view实现钟表效果
- centos7 nginx配置php7,centos7下yum安装nginx和php7和php-fpm
- codesoft各个版本的 dll_win10安装pytorch-gpu版本
- javascript取随机数_查缺补漏一些 Javascript 的小技巧笔记
- python中cgi到底是什么_十分钟搞懂什么是CGI(转)