在运行时(Unity)更改文本的颜色?

时间:2015-04-28 16:19:35

标签: colors unity3d runtime

我的Canvas中嵌入了 GameObject ,其中包含“文本(脚本)”组件。我希望在该元素的运行时期间更改color.a属性。有人知道怎么做吗?我似乎无法使用任何GetComponent<Type> ()命令访问它。

4 个答案:

答案 0 :(得分:5)

如果您想要更改文本颜色的R,G,B或A组件,您可以这样做:

&#13;
&#13;
Public Text text;
float r=0.2f,g=0.3f,b=0.7f,a=0.6f;

void Start()
{
  text=gameobject.GetComponent<Text>();
  text.color= new Color(r,g,b,a);
}
&#13;
&#13;
&#13;

答案 1 :(得分:2)

据我所知,你必须为text.color分配新的颜色。您可以使用自己的颜色来指定或使用其中一种标准颜色:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class test : MonoBehaviour {

public Text text;

void Start () 

    {
       text = gameObject.GetComponent<Text> ();
       text.color = Color.white;
    }
}

答案 2 :(得分:1)

InputStream in = MyClass.getClassLoader().getResourceAsStream("myprops.properties");
Properties props = new Properties()
props.load(in) 
String someProperty = props getProperty("someProperty"); // xxx
  

将此附加到层次结构中文本的UI组件

public Text myText;

答案 3 :(得分:0)

使用CrossFadeAlpha。请参阅使用链接。