使用Threading.Sleep1000;在C#中统一

问题描述

我正在统一进行测验。我想单击正确的答案时播放音频。尽管播放音频的行代码在thread.Sleep()之前,但是在进行测验时,它在启用音频之前就睡着了!救命。预先感谢。

interface ProjectAttrs {
  user: string;
  title: string;
  description: string;
  category: categories;
}

解决方法

音频播放发生在主线程上(Unity通常是单线程的),并且您的代码使当前(当前是主线程)进入睡眠状态,因此直到线程从睡眠中唤醒后才播放音频。

要解决此问题,您可能需要考虑使用Unity coroutines。启动协程,它会yield return new WaitForSeconds(5);,然后开始下一个测验。

,

我认为团结在停止睡眠之后正在播放音频吗?在这种情况下,为什么不简单使用协程呢?像这样:

public void OnMouseDown(){
     {
           CheckAnswer();
     }
     public void CheckAnswer();
     {
        if (Correct == true)
        {
            audioo = this.gameObject.GetComponent<AudioSource>();
            StartCoroutine(PlaySound(audioo));
        }
        else
        {

        }
    }

IEnumerator PlaySound(AudioSource sound)
{
    //Play the sound here,then load up the next question.
    audioo.enabled = true;
    yield return new WaitForSeconds(5f);
    NextQuiz.SetActive(true);
    CurrentQuiz.SetActive(false);
}

此外,您还可以通过简单地创建2个AudioClip变量并将正确的声音分配给一个对象,以及将不正确的声音分配给另一个对象来替换不断停用和重新激活对象上的音频源,然后使用audioo.Play( clipName),以便播放适当的音乐,例如:

public AudioClip correctSound;
public AudioClip incorrectSound;

public void OnMouseDown(){
     {
           CheckAnswer();
     }
     public void CheckAnswer();
     {
        if (Correct == true)
        {
            audioo = this.gameObject.GetComponent<AudioSource>();
            StartCoroutine(PlaySound(audioo,correctSound));
        }
        else
        {

        }
    }

IEnumerator PlaySound(AudioSource audioSource,AudioClip audioClip)
{
    //Play the sound here,then load up the next question.
    audioSource.Play(audioClip);
    yield return new WaitForSeconds(5f);
    NextQuiz.SetActive(true);
    CurrentQuiz.SetActive(false);
}

尝试类似的事情

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...