我想只在启用特定脚本的情况下每秒播放一次AudioSource:
public int wait = 1;
bool keepPlaying = true;
void Start ()
{
StartCoroutine(SoundOut());
}
IEnumerator SoundOut()
{
while (keepPlaying)
{
if (GameObject.FindWithTag ("GameObject").GetComponent<SpecificScript> ().enabled == true)
{
GameObject.FindWithTag ("GameObject").GetComponent<AudioSource> ().Play ();
yield return new WaitForSeconds (wait);
}
}
}
启用特定脚本时,此脚本不会每秒播放声音。有什么问题?
答案 0 :(得分:0)
尝试使用PlayOneShot
float wait = 1;
bool keepPlaying = true;
void Start()
{
StartCoroutine(SoundOut());
}
IEnumerator SoundOut()
{
while (keepPlaying)
{
if (GameObject.FindWithTag("GameObject").GetComponent<SpecificScript>().enabled == true)
{
AudioSource myAudioSource = GameObject.FindWithTag("GameObject").GetComponent<AudioSource>();
myAudioSource.PlayOneShot(myAudioSource.clip);
yield return new WaitForSeconds(wait);
}
}
}
使用此技术,如果你的剪辑是&gt; 1秒钟,它们将重叠。
如果您不想要,请执行myAudioSource.Stop();在再次播放之前。