等到音频结束前设置为活动是假的 [英] Wait until audio is finished before set active is false

查看:206
本文介绍了等到音频结束前设置为活动是假的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目,我希望有一个健全的发挥,然后为对象的活动状态被设置为false,目前两者都在同一时间发生这样的声音不能播放。如果我保持活跃状态​​,真正的话,我会听到播放声音。

我怎样才能确保前设置为活动状态切换到假音完成后,任何帮助或建议是AP preciated。

下面是一个选择我的code的;

 无效OnTriggerEnter(撞机等)
 {
     如果(other.tag ==捡)
     {
         如果(!isPlayed){
             source.Play();
             isPlayed = TRUE;
         }
     }
     如果(other.gameObject.CompareTag(捡))
     {
         other.gameObject.SetActive(真);
         数=计+ 1;
         SetCountText();
     }
 }


解决方案

使用协程乔说。启动协程的冲突对象启用各一次。

 无效OnTriggerEnter(撞机等)
 {
     如果(other.tag ==捡)
     {
         如果(!isPlayed){
             source.Play();
             isPlayed = TRUE;
         }
     }
     如果(other.gameObject.CompareTag(捡))
     {
         other.gameObject.SetActive(真);
         数=计+ 1;
         SetCountText();
         StartCoroutine(waitForSound(其他)); //开始协程
     }
 } IEnumerator的waitForSound(撞机等)
    {
        //等到声音播放完毕
        而(source.isPlaying)
        {
            产量返回NULL;
        }       // Auidio播放完毕,关闭游戏物体
        other.gameObject.SetActive(假);
    }

in my project I want a sound to play and then for an objects active state to be set to false, at the moment both are happening at the same time so the sound doesn't play. If I keep the active state as true then I will hear the sound play.

How can I make sure that the audio finishes before the set active state is switched to false, any help or advice is appreciated.

Here's a selection of my code;

 void OnTriggerEnter(Collider other)
 {
     if (other.tag == "Pick Up") 
     {
         if (!isPlayed) {
             source.Play ();
             isPlayed = true;
         }
     }
     if (other.gameObject.CompareTag ("Pick Up"))
     {
         other.gameObject.SetActive (true);
         count = count + 1;
         SetCountText ();
     }
 }

解决方案

Use coroutine as Joe Said. Start coroutine each time the collided object is enabled.

void OnTriggerEnter(Collider other)
 {
     if (other.tag == "Pick Up") 
     {
         if (!isPlayed) {
             source.Play ();
             isPlayed = true;
         }
     }
     if (other.gameObject.CompareTag ("Pick Up"))
     {
         other.gameObject.SetActive (true);
         count = count + 1;
         SetCountText ();
         StartCoroutine(waitForSound(other)); //Start Coroutine
     }
 }



 IEnumerator waitForSound(Collider other)
    {
        //Wait Until Sound has finished playing
        while (source.isPlaying)
        {
            yield return null;
        }

       //Auidio has finished playing, disable GameObject
        other.gameObject.SetActive(false);
    }

这篇关于等到音频结束前设置为活动是假的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆