Unity Wait Inside if 语句
Unity Wait Inside if Statement
如果有 if 语句,我该如何等待?我需要时间来播放音频文件。
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "Player")
{
audio.Play();
// yield return new WaitForSeconds(.5f);
gameObject.SetActive(false);
}
}
你可以像这样'dodgy'做一些漂亮的事情:
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "Player")
{
audio.Play();
float timer = 0;
do{timer += Time.deltaTime;}while(timer != 1)
gameObject.SetActive(false);
}
}
但我建议您使用更新循环中的内容实际跟踪音频的进度,例如:
if (!audio.isPlaying) { //Audio playing }
或
if(audio.time > 1) { //Audio has been playing for more than one second }
希望对您有所帮助。
忘了link...
audio.isPlaying
= http://docs.unity3d.com/ScriptReference/AudioSource-isPlaying.html
audio.time
= http://docs.unity3d.com/ScriptReference/AudioSource-time.html
如果有 if 语句,我该如何等待?我需要时间来播放音频文件。
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "Player")
{
audio.Play();
// yield return new WaitForSeconds(.5f);
gameObject.SetActive(false);
}
}
你可以像这样'dodgy'做一些漂亮的事情:
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "Player")
{
audio.Play();
float timer = 0;
do{timer += Time.deltaTime;}while(timer != 1)
gameObject.SetActive(false);
}
}
但我建议您使用更新循环中的内容实际跟踪音频的进度,例如:
if (!audio.isPlaying) { //Audio playing }
或
if(audio.time > 1) { //Audio has been playing for more than one second }
希望对您有所帮助。
忘了link...
audio.isPlaying
= http://docs.unity3d.com/ScriptReference/AudioSource-isPlaying.html
audio.time
= http://docs.unity3d.com/ScriptReference/AudioSource-time.html