I have seen many questions about using multiple AudioSources but in my case, I want to use just one AudioSource, and one script controlling it. I have assigned 3 sound loops and I want to loop them without gaps all at once from one AudioSource, as well as stop and play individual sound loop with different individual volumes. Here’s where I am right now:
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(AudioSource))]
public class Sound : MonoBehaviour {
AudioSource audioSource;
public AudioClip audioClip1;
public AudioClip audioClip2;
public AudioClip audioClip3;
[Range (0, 1)] public float audioClipVolume1;
[Range (0, 1)] public float audioClipVolume2;
[Range (0, 1)] public float audioClipVolume3;
void Start () {
audioSource = GetComponent<AudioSource> ();
audioSource.loop = true;
}
void Update () {
//How do I loop all 3 audio clips without gaps?
//Tried using PlayOneShot
//Checking if it is playing and playing it again creates gaps
//Also, if one sound loop is shorter than the other soundloops, will wait untill
//other sound loops finished to start playing again...
if (!audioSource.isPlaying) {
audioSource.PlayOneShot (audioClip1, audioClipVolume1);
audioSource.PlayOneShot (audioClip2, audioClipVolume2);
audioSource.PlayOneShot (audioClip3, audioClipVolume3);
}
//Tried changing clip
//AudioSource can load only one clip, but allows for gapless looping
audioSource.clip = audioClip2;
audioSource.volume = audioClipVolume2;
if (!audioSource.isPlaying) {
audioSource.Play ();
}
}
}