【问题标题】:How to change UI Text Color如何更改 UI 文本颜色
【发布时间】:2016-10-12 10:58:57
【问题描述】:

我的脚本适用于 3D 游戏对象,但不适用于 Text 组件。我怎样才能让它在文本上工作?非常感谢您的帮助。

using UnityEngine;
using System.Collections;

public class Colors : MonoBehaviour

{
public float timer = 0.0f;
Renderer rd;

void Start()
{
    rd = gameObject.GetComponent<Renderer>();
}


void Update()
{
    timer += Time.deltaTime;
    if (timer >= 2.0f)//change the float value here to change how long it takes to switch.
    {
        // pick a random color
        Color newColor = new Color(Random.value, Random.value, Random.value, 1.0f);
        // apply it on current object's material
        rd.material.color = newColor;

        timer = 0;
    }
}

}

【问题讨论】:

  • 谁支持了这个问题 - 请编辑它,使它真正包含问题(“请帮助”不是一个问题)。
  • 您不能以这种方式使用 Update()。只需使用 Invoke 来制作计时器。这很容易。
  • 你能给我看看吗?我是编程新手。

标签: c# unity3d scripting


【解决方案1】:

对于 UI 文本,您使用 Text.color 而不是 Renderer.material.colorRenderer 用于附有Mesh Renderer 的 3D 对象。确保将其附加到带有 Text 组件的 GameObject 上,否则会出现错误。

public float timer = 0.0f;
Text txt;

void Start()
{
    txt = gameObject.GetComponent<Text>();
}


void Update()
{
    timer += Time.deltaTime;
    if (timer >= 2.0f)//change the float value here to change how long it takes to switch.
    {
        // pick a random color
        Color newColor = new Color(Random.value, Random.value, Random.value, 1.0f);
        // apply it on current object's material
        txt.color = newColor;

        timer = 0;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-21
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 2020-02-05
    • 2021-04-24
    • 2022-01-21
    相关资源
    最近更新 更多