淡入淡出GUI.Box

时间:2016-11-16 17:55:57

标签: c# unity3d

如何淡入淡出GUI.Box?我试过搜索,但只能找到如何淡化文字。

void OnGUI()
{

    GUI.Box(new Rect(20, 20, 300, 100), "Find pH of paper");
}

1 个答案:

答案 0 :(得分:2)

自Unity的新GUI系统以来,不推荐使用OnGUI功能。

无论如何,GUI类提供了一个名为GUI.backgroundColor

的静态属性

只需设置颜色随时间的变化值即可。但是,我不知道是否会在OnGUI函数中考虑alpha。

以下是一个示例:

private void Update()
{
     GUI.backgroundColor = Color.Lerp(Color.white, Color.clear, Mathf.PingPong(Time.time, 1));
}

如果您想在用户执行操作时启动淡入淡出,也可以使用Coroutines

private IEnumerator FadeIn( float duration = 1 )
{
     for( float t = 0 ; t < duration ; t += Time.deltaTime )
     {
          GUI.backgroundColor = Color.Lerp(Color.clear, Color.white, t / duration);
          yield return null ;
     }
}

使用UGUI系统更简单,更清洁,更快捷。改变精灵的颜色,或alpha value of a Canvas Group将是一块蛋糕!

相关问题