首页 > 解决方案 > 一个用于更新两个 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适用于两个文本框。

如何显示每个值?

标签: c#unity3d

解决方案


推荐阅读