Unity - 从脚本更改文本UI alpha颜色?

时间:2016-08-23 08:26:21

标签: c# unity3d text colors

新手在这里。所以我希望能够从gameManager脚本中淡入/淡出一些文本元素。我试过了

Toast.makeText(view.getContext(), "Invalid Login Or Password",    Toast.LENGTH_LONG).show();

但它给了我错误

2 个答案:

答案 0 :(得分:2)

尝试使用Lerp。只要将'newColor'的'alpha'设置为0,文本就会淡出。

 public Text example;
 public Color newColor;
 public float fadeTime = 0.1f; //maybe rename this to fadeSpeed

 //this should be called somewhere in Update
 void FadeOut()
 {
    example.color = Color.Lerp(example.color, newColor, fadeTime * Time.deltaTime);
 }

编辑:使用协同程序

void CallingMethod()
{
    StartCoroutine(FadeOut());
}

//note the change from 'void' to 'IEnumerator'
IEnumerator FadeOut()
{
    //ugly while, Update would be ideal
    while (example.color.a > 0)
    {
        example.color = Color.Lerp(example.color, newColor, fadeTime * Time.deltaTime);
        yield return null;
    }
    //code after fading is finished
}

答案 1 :(得分:0)

您无法直接修改颜色值。而是尝试创建一个单独的颜色变量并根据需要进行修改。

Color color = example.color;
color.a -= fadeTime;
example.color = color.a;

如果要平滑淡出对象,请考虑使用Coroutines