2016-11-16 78 views
-1

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

void OnGUI() 
{ 

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

回答

2

从Unity的新GUI系统开始,不推荐使用OnGUI函数。

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

只需设置的颜色的值随时间的静态属性。但是,我不知道alpha是否会被OnGUI函数考虑在内。

这里是这样做的一个例子:

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将是小菜一碟!