1

我正在使用 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 ();
}
4

2 回答 2

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 ();
}
于 2012-08-26T20:17:55.963 回答
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");
     }
}
于 2012-08-31T10:36:49.870 回答