private void OnClipFinished(object sender, AudioEventArgs e) { // Store the source in the pool ManagedSource source = (ManagedSource)sender; sourcePool.Put(source); }
public ManagedSource PlayClip(Vector3 position, AudioClip clip, float volume, bool looped) { // Create the source and start it playing ManagedSource source = sourcePool.Get(); source.Play(position, clip, volume, looped); return(source); }
private ManagedSource CreateNewAudioSource() { ManagedSource instance = GameObject.Instantiate <ManagedSource>(sourcePrefab); // Self-register for events instance.clipFinishedHandler += OnClipFinished; return(instance); }
/// <summary> /// /// </summary> public void StopAllSources() { List <ManagedSource> sources = activeSources.GetRawList(); int len = sources.Count - 1; // Iterate and stop all sources for (int i = len; i >= 0; i--) { ManagedSource source = sources[i]; source.Stop(); } }
private void OnSourceStored(ManagedSource obj) { activeSources.Remove(obj); }
private void OnSourceReleased(ManagedSource obj) { activeSources.Add(obj); }
public AudioController(SimulationController controller) : base(controller) { sourcePrefab = Resources.Load <ManagedSource>("AudioSourcePrefab"); activeSources = new Store <ManagedSource>(); sourcePool = new Pool <ManagedSource>(CreateNewAudioSource, OnSourceStored, OnSourceReleased); }