我刚开始在Unity中编程,我想做的是在游戏开始后5秒钟延迟播放音频片段。
我已经在线搜索并在此处找到了此代码,但是当我执行它时没有声音。
using UnityEngine;
using System.Collections;
public class WizardVoice : MonoBehaviour {
public AudioSource myAudio;
// Use this for initialization
void Start () {
StartCoroutine(PlaySoundAfterDelay(myAudio, 300.0f));
}
// Update is called once per frame
void Update () {
}
IEnumerator PlaySoundAfterDelay(AudioSource audioSource, float delay)
{
if (audioSource == null)
yield break;
yield return new WaitForSeconds(delay);
audioSource.Play();
}
}
我试图找出我做错了什么,但没有运气。任何帮助表示赞赏!
谢谢!
答案 0 :(得分:0)
将300.0f
更改为5000f
,因为300.0f
为300毫秒而非5秒。 1000f
= 1
秒。 5000f
秒使用5
。这就是WaitForSeconds();
工作的方式。