Unity3d Unity:LoadScene从计时器触发时不工作
在我的游戏中,当玩家死亡时,会播放一个死亡的声音,一旦声音结束,场景应该在用户仍然有足够的生命时重新加载 在我听到声音之前,该剧在调用死亡函数时立即死亡:Unity3d Unity:LoadScene从计时器触发时不工作,unity3d,Unity3d,在我的游戏中,当玩家死亡时,会播放一个死亡的声音,一旦声音结束,场景应该在用户仍然有足够的生命时重新加载 在我听到声音之前,该剧在调用死亡函数时立即死亡: public static void Death() { AddCoinScript.coinCounter = 0; LivesScript.livesCounter--; if (LivesScript.livesCounter > -1)//to get 0 live {
public static void Death()
{
AddCoinScript.coinCounter = 0;
LivesScript.livesCounter--;
if (LivesScript.livesCounter > -1)//to get 0 live
{
Debug.Log("TIMER");
var currentScene = SceneManager.GetActiveScene();
SceneManager.LoadScene(currentScene.name);
}
else
{
//TO DO GameOver
}
}
这很有魅力。
但现在我给它加了一个死亡的声音。不幸的是,unity没有提供一个事件处理程序,当声音播放完毕时,我希望场景不再立即重新加载,但在死亡之声播放完毕后,我决定自己创建一个计时器。死亡之声结束后,计时器立即启动。这就是该功能的功能:
public static void Death()
{
AddCoinScript.coinCounter = 0;
LivesScript.livesCounter--;
PlayDeathSound();
System.Timers.Timer timer = new System.Timers.Timer();
timer.Interval = aSDeath.clip.length * 1000;
timer.Start();
timer.Elapsed += delegate
{
timer.Stop();
if (LivesScript.livesCounter > -1)//to get 0 live
{
Debug.Log("TIMER");
var currentScene = SceneManager.GetActiveScene();
SceneManager.LoadScene(currentScene.name);
}
else
{
//TO DO GameOver
}
};
}
如您所见,为了确保计时器真正启动,我设置了debug.LogTIMER来查看它是否真的工作。猜猜看,它真的是这样。调试现在在其控制台中显示计时器。但你知道什么不再管用了吗?下面的两行代码
var currentScene = SceneManager.GetActiveScene();
SceneManager.LoadScene(currentScene.name);
这和之前的工作原理是一样的,但是当定时器触发时,它们就被忽略了?这怎么可能呢?
当我把它全部改回来时,它又能工作了。只有当计时器触发这两行时,它们才会被忽略
这太奇怪了,还是我遗漏了什么?谢谢大家! 好吧,我不是C和委托方面的专家,但显然它创建了一个单独的线程,您只能在主线程上使用SceneManager.GetActiveScene 由于我对委托不太确定,我将提供一个更简单的解决方案。您可以使用协同程序,因为您知道需要等待的时间如下:
public void Death()
{
StartCoroutine(DeathCoroutine());
}
IEnumerator DeathCoroutine()
{
AddCoinScript.coinCounter = 0;
LivesScript.livesCounter--;
PlayDeathSound();
// wait for duration of the clip than continue executing rest of the code
yield return new WaitForSeconds(aSDeath.clip.length);
if (LivesScript.livesCounter > -1)//to get 0 live
{
Debug.Log("TIMER");
var currentScene = SceneManager.GetActiveScene();
SceneManager.LoadScene(currentScene.name);
}
else
{
//TO DO GameOver
}
}
好吧,我不是C和委托方面的专家,但显然它创建了一个单独的线程,您只能在主线程上使用SceneManager.GetActiveScene 由于我对委托不太确定,我将提供一个更简单的解决方案。您可以使用协同程序,因为您知道需要等待的时间如下:
public void Death()
{
StartCoroutine(DeathCoroutine());
}
IEnumerator DeathCoroutine()
{
AddCoinScript.coinCounter = 0;
LivesScript.livesCounter--;
PlayDeathSound();
// wait for duration of the clip than continue executing rest of the code
yield return new WaitForSeconds(aSDeath.clip.length);
if (LivesScript.livesCounter > -1)//to get 0 live
{
Debug.Log("TIMER");
var currentScene = SceneManager.GetActiveScene();
SceneManager.LoadScene(currentScene.name);
}
else
{
//TO DO GameOver
}
}
使用协同程序怎么样?您只需在播放机死机时启动它,并在您的声音仍在播放时退出。使用协同程序怎么样?您只需在播放机死机时启动它,并在您的声音仍在播放时发出声音。您可以尝试设置一个类似isDead的bool,然后检查更新中的AudioSource.isplay。一旦这是假的,isDead是真的加载scenewell,这就是我最后要做的,但是,这并没有回答我的问题,上面的函数到底是怎么不起作用的…你可以尝试设置一个类似isDead的bool,然后检查音频源。在更新中显示。一旦这是假的,isDead是真的加载scenewell,这就是我最后要做的,但是,这并没有回答我的问题,上面的函数到底是怎么不起作用的。。。