c# - 一个用于更新两个 UI 文本的脚本
问题描述
我有多个想要在屏幕上显示的分数,并且我想创建一个文本颜色(范围 0 到 100 将显示红色到绿色)。
这是我添加预制件的脚本(2 个 UI 文本):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class scoring: MonoBehaviour
{
Text text;
Text StatText2;
public static int scoringValue = 80;
public static int scoringValue2 = 10;
float scoreDouble = scoringValue/100f;
float scoreDouble2 = scoringValue2/100f;
Color maxColor = new Color32(21, 193, 25, 255);
Color minColor = new Color32(229, 85, 85, 255);
// Start is called before the first frame update
void Start()
{
text = GetComponent<Text>();
StatText2 = GetComponent<Text>();
}
// Update is called once per frame
void Update()
{
text.text =scoringValue.ToString();
StatText2.text = scoringValue2.ToString();
text.GetComponent<Text>().color = Color.Lerp(minColor, maxColor, scoreDouble);
StatText2.GetComponent<Text>().color = Color.Lerp(minColor, maxColor, scoreDouble2);
//Debug.Log(scoreDouble);
}
}
结果10
适用于两个文本框。
如何显示每个值?
解决方案
推荐阅读
- sitecore - Sitecore 9.1.1 从 8.2 升级 - 是否有任何数据库更改?
- docker-compose - Docker-compose 容器退出,代码为 0
- javascript - 如何使用 Reactjs 在内部呈现带有文本的复选框?
- angularjs - Angular 1 表单验证错误未显示
- dataset - 如何从 HDF 文件中提取新的 Xarray 数据集?
- algorithm - 使用 BubbleSort 完全排序所需的通过次数?
- java - 使用 WHERE 子句 JOIN 更新另一个表
- c++ - 在 Qt Creator 中调试 c++ 程序时如何正确忽略 throw 中断?
- vue.js - 图像src vue中的动态路径
- api - 使用 Tensorflow-API 运行实时对象检测时出错