简体   繁体   中英

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

I have a fairly large array of 60 tracks. 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. After switching scenes, 3 should play, and so on. On the Internet I found a function that seems to work the way I want. But I do not quite understand how to call function correctly.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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