Right now I have a script called PlayerAudioManager, when I use the function play audioclip, it only can only play one clip, so if a sound is playing and then it is called to play another sound, the sound it was playing is canceled. How do I make a framerate-friendly system that has the capabilty of playing multiple audio clips at once?
Code:
using UnityEngine;
using System.Collections;
//This needs to be added to the ROOT player object.
public class PlayerAudioManager : MonoBehaviour {
public Transform Global_AudioObject;
public Transform Local_AudioObject;
public void PlayAudioClip(AudioClip toPlay,AudioPlayType playType,bool randomPitch, float volume){
if(networkView.isMine){
float pitch = 1;
if(randomPitch){
pitch = Random.Range(0.90f,1.10f);
//Debug.Log("Pitch: " + pitch);
}
if (playType == AudioPlayType.All || playType == AudioPlayType.Global) {
//Play the audio with RPC
networkView.RPC("Network_PlayAudioClip",RPCMode.Others,toPlay.name,pitch,volume);
}
if (playType == AudioPlayType.All || playType == AudioPlayType.Local) {
//Play the audio with Local Functions
Local_PlayAudioClip(toPlay,pitch,volume);
}
}
}
public void PlayAudioClip(AudioClip toPlay,AudioPlayType playType,float minPitch, float maxPitch, float volume){
if(networkView.isMine){
float pitch = Random.Range(minPitch,maxPitch);
// Debug.Log("Pitch: " + pitch);
if (playType == AudioPlayType.All || playType == AudioPlayType.Global) {
//Play the audio with RPC
networkView.RPC("Network_PlayAudioClip",RPCMode.Others,toPlay.name,pitch,volume);
}
if (playType == AudioPlayType.All || playType == AudioPlayType.Local) {
//Play the audio with Local Functions
Local_PlayAudioClip(toPlay,pitch,volume);
}
}
}
[RPC]
void Network_PlayAudioClip(string ResourceAudio, float pitch, float volume){
AudioClip ac = (AudioClip)Resources.Load ("Audio/" + ResourceAudio);
Global_AudioObject.audio.volume = volume;
Global_AudioObject.audio.pitch = pitch;
Global_AudioObject.audio.PlayOneShot (ac);
}
private void Local_PlayAudioClip(AudioClip audio, float pitch, float volume){
Local_AudioObject.audio.volume = volume;
Local_AudioObject.audio.pitch = pitch;
Local_AudioObject.audio.PlayOneShot (audio);
}
public enum AudioPlayType{
Local,
Global,
All
}
}