新手在这里。所以我希望能够从gameManager脚本的某些文本元素中淡出/淡出。我试图统一 - 从脚本更改文本UI alpha颜色?
public float fadeTime = 0.1f;
public Text example;
void FadeOut()
{
example.color.a -= fadeTime;
}
但它给我的错误
新手在这里。所以我希望能够从gameManager脚本的某些文本元素中淡出/淡出。我试图统一 - 从脚本更改文本UI alpha颜色?
public float fadeTime = 0.1f;
public Text example;
void FadeOut()
{
example.color.a -= fadeTime;
}
但它给我的错误
尝试使用线性插值。只要您将'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
}
我无法将它放入Update()中,因为它必须集成在GetInput()方法中。我试着用for语句改变它,但它不起作用。 for(fadeTime = 1; fadeTime <= 0; fadeTime = fadeTime - 0.1f){example.color = Color.Lerp(example.color,newColor,fadeTime * Time.deltaTime); } – Sciencephile
更新不能使用时控制时间的另一种方法是协程。看我的编辑。 – AlexTudo
谢谢大家,虽然我很困惑,为什么不透明度会发生变化,即使fadeTime保持不变,也不是lerp应该将它转换为fadeTime显示的内容,所以我们必须增加fadeTime? – Sciencephile
您不能直接修改颜色值。请尝试创建一个单独的颜色变量并根据需要修改它。
Color color = example.color;
color.a -= fadeTime;
example.color = color.a;
如果你想顺利淡出的对象,可以考虑使用Coroutines。
你会得到什么错误? –
记得使用'using UnityEngine.UI; ' –
“无法修改”Graphic.color“的返回值,因为它不是变量” – Sciencephile