Created
February 6, 2012 07:37
-
-
Save boj/1750484 to your computer and use it in GitHub Desktop.
Audio Management
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class AudioManager : MonoBehaviour { | |
public GameObject audioChannelPrefab; | |
private AudioSource[] audioChannels = new AudioSource[12]; // 12 channels | |
void Start() { | |
for (int i = 0; i < audioChannels.Length; i++) { | |
GameObject channel = Instantiate(audioChannelPrefab) as GameObject; | |
audioChannels[i] = channel.GetComponent<AudioSource>(); | |
} | |
} | |
AudioSource GetFreeChannel() { | |
for (int i = 0; i < audioChannels.Length; i++) { | |
if (!audioChannels[i].isPlaying) { | |
return audioChannels[i]; | |
} | |
} | |
return null; | |
} | |
public void Play(AudioClip clip, Vector3 location, float pitch) { | |
AudioSource channel = GetFreeChannel(); | |
if (channel == null) return; // no available channels | |
// set transform position | |
channel.transform.position = location; | |
// play clip | |
channel.clip = clip; | |
channel.pitch = pitch; | |
channel.Play(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment