WaitHandle和Thread.Sleep错误 [英] Error with WaitHandle and Thread.Sleep
问题描述
我在这里四处寻找使脚本等待几秒钟的方法,发现本文.我拿了一些代码,并在游戏中使用了它,以使其等待,但它并没有达到我想要的效果.我将其放置在两条Debug.Log行之间,但它会等待指定的时间,然后立即记录这两件事,而不是等到第一个触发后才等待.
I looked around on here for a way to make my script wait a few seconds and found this article which people recommended. I took some of this code and used it in my game to make it wait but it doesn't act as I want it to. I placed it between two Debug.Log lines but it waits for the designated time and then logs both things at once instead of waiting after the first has triggered.
static void Waiter(){
waitHandle.WaitOne ();
}
Debug.Log (choice);
new Thread (Waiter).Start ();
Thread.Sleep (5000);
waitHandle.Set ();
Debug.Log (enemyChoice);
推荐答案
要在Unity中等待,请使用协程和 Time.deltaTime
或 WaitForSeconds
.请注意,如果您等待Thread.Sleep
,则整个程序将冻结.不要这样做,因为UI也不会响应.
To wait in Unity, you use coroutine and WaitForSeconds
. You can also use coroutine with Time.deltaTime
or Time.time
but the simplest way to do this is to use WaitForSeconds
. Note that if you wait with Thread.Sleep
, your whole program will freeze. Don't do that because the UI will not be responsive too.
void Start()
{
StartCoroutine(doSomethingThenWait());
}
IEnumerator doSomethingThenWait()
{
UnityEngine.Debug.Log("Before Waiting");
//Wait for 2 Seconds
yield return new WaitForSeconds(2);
UnityEngine.Debug.Log("After Waiting");
}
这篇关于WaitHandle和Thread.Sleep错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!