使GUI.Box停留3秒钟

时间:2012-08-26 20:10:30

标签: c# user-interface unity3d

我正在使用C#脚本编写Unity项目。 GUI.Box将出现在屏幕顶部。当玩家离开现场时,该框将消失。在玩家离开指定地点后,如何让盒子再停留3秒钟?

Danpe的代码更正(工作代码):

bool shown = false;

void OnGUI () {
if (car.transform.position.y>=43 && car.transform.position.y<=44)
{
    shown = true;
}
else if (shown)
{
    StartCoroutine(DisapearBoxAfter(3.0f)); 
}
if(shown) 
{
    GUI.Box(new Rect((Screen.width/2)-200,0,400,30) , "King of the hill");
}
}

IEnumerator DisapearBoxAfter(float waitTime) { 
// suspend execution for waitTime seconds
yield return new WaitForSeconds(waitTime);
shown = false;
}




void Update () {
    OnGUI ();
}

2 个答案:

答案 0 :(得分:1)

bool shown = false;

void OnGUI () {
    if (car.transform.position.y>=43 && car.transform.position.y<=44)
    {
        shown = true;
    }
    else if (shown)
    {
        StartCoroutine(DisapearBoxAfter(3.0)); 
    }
    if(shown) {
        GUI.Box(new Rect((Screen.width/2)-200,0,400,30) , "King of the hill");
    }
}

IEnumerator DisapearBoxAfter(float waitTime) {
    // suspend execution for waitTime seconds
    return yield WaitForSeconds (waitTime);
    shown = false;
}


void Update () {
    OnGUI ();
}

答案 1 :(得分:0)

function Start ()
{
    ShowBox ();
}

function ShowBox ()
{
    // show label
    show = true;

    // cancel invoking method if already set to call after 3 seconds
    CancelInvoke("HideBox");

    // will call HideBox () after 3 sec
    Invoke ("HideBox", 3.0F);
}

function HideBox ()
{
   // dont show label
   show = false;
}

function Update ()
{
     if (car.transform.position.y>=43 && car.transform.position.y<=44)
     {
         ShowBox ();
     }
}

function OnGUI ()
{
     if(shown) 
     {
          GUI.Box(new Rect((Screen.width/2)-200,0,400,30) , "King of the hill");
     }
}