简体   繁体   English

如何在 Unity 中依次播放一系列音频?

[英]How do I play an array of audio in turn in Unity?

I have a fairly large array of 60 tracks.我有相当大的 60 条轨道。 I need them to play one by one.我需要他们一一演奏。 When the scene changes, the track is also interrupted and the next track is played in turn.当场景发生变化时,曲目也会中断,依次播放下一首曲目。 That is, in one scene can play 0, 1, 2 items.也就是说,在一个场景中可以播放 0、1、2 个项目。 After switching scenes, 3 should play, and so on.切换场景后,应该播放 3,依此类推。 On the Internet I found a function that seems to work the way I want.在互联网上,我发现了一个 function 似乎可以按我想要的方式工作。 But I do not quite understand how to call function correctly.但我不太明白如何正确调用 function。

public AudioClip[] clipArray;
public AudioSource effectSource;
private int clipIndex;

void PlayRoundRobin() {

if (clipIndex < clipArray.Length)
{
effectSource.PlayOneShot(clipArray[clipIndex]);
clipIndex++;
}

else
{
clipIndex = 0;
effectSource.PlayOneShot(clipArray[clipIndex]);
clipIndex++;
}

Put your method into your scene manager.将您的方法放入场景管理器中。 If you load the scene, call the method.如果加载场景,请调用该方法。 After that, call the method whenever you want to play the next track.之后,只要您想播放下一首曲目,就调用该方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM