IRCSS

Unity Oneshot with Audiosource

Aug 1st, 2019
188
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public static class AudioClipExtention
  6. {
  7.     static List<PooledAudioSource> sourcesPool = new List<PooledAudioSource>();
  8.     public static int totalNumberOfAudioSources;
  9.     public static Transform parent;
  10.     public static int maxSourcesCount = 32;
  11.  
  12.     public static void PlayWithSource(this AudioClip aClip, object caller)
  13.     {
  14.         PooledAudioSource p = ReturnSource();
  15.         if (p == null) { Debug.LogWarning("Could not play the clip: " + aClip.name + " because there are more than " + maxSourcesCount + " sources in the scene."); return; }
  16.  
  17.         p.Play( caller, aClip);
  18.     }
  19.  
  20.     public static void PlayWithSource(this AudioClip aClip, object caller, float volume, float pitch)
  21.     {
  22.         PooledAudioSource p = ReturnSource();
  23.         if (p == null) { Debug.LogWarning("Could not play the clip: " + aClip.name + " because there are more than " + maxSourcesCount + " sources in the scene."); return; }
  24.  
  25.         p.Play(caller, aClip, volume, pitch);
  26.     }
  27.  
  28.     public static void SourceFinishedPlaying(PooledAudioSource ps)
  29.     {
  30.         sourcesPool.Add(ps);
  31.     }
  32.  
  33.     static PooledAudioSource ReturnSource()
  34.     {
  35.    
  36.         if(sourcesPool.Count >0)
  37.         {
  38.             PooledAudioSource toReturn = sourcesPool[sourcesPool.Count - 1];
  39.             sourcesPool.RemoveAt(sourcesPool.Count - 1);
  40.                 return toReturn;
  41.         }
  42.  
  43.         if (totalNumberOfAudioSources >= maxSourcesCount) return null;
  44.  
  45.         return new PooledAudioSource();
  46.     }
  47. }
  48.  
  49. public class PooledAudioSource
  50. {
  51.  
  52.     AudioSource source;
  53.  
  54.     public PooledAudioSource()
  55.     {
  56.         if(source == null)
  57.         {
  58.             source = new GameObject().AddComponent<AudioSource>();
  59.             AudioClipExtention.totalNumberOfAudioSources++;
  60.             source.gameObject.name = "Pooled_AudioSources_" + AudioClipExtention.totalNumberOfAudioSources;
  61.             if (AudioClipExtention.parent == null) AudioClipExtention.parent = new GameObject("PooledAudios").transform;
  62.             source.gameObject.transform.parent = AudioClipExtention.parent;
  63.  
  64.             source.playOnAwake = false;
  65.             source.loop = false;
  66.         }
  67.        
  68.     }
  69.  
  70.     public void Play(object caller, AudioClip c)
  71.     {
  72.         source.enabled = true;
  73.         source.clip = c;
  74.         source.Play();
  75.  
  76.         ((MonoBehaviour)caller).StartCoroutine(TrackProgress(c.length));
  77.     }
  78.  
  79.     public void Play(object caller, AudioClip c, float volume, float pitch)
  80.     {
  81.         source.enabled = true;
  82.         source.clip = c;
  83.         source.volume = volume;
  84.         source.pitch = pitch;
  85.  
  86.         source.Play();
  87.        
  88.         ((MonoBehaviour)caller).StartCoroutine(TrackProgress(c.length));
  89.     }
  90.  
  91.     IEnumerator TrackProgress(float length)
  92.     {
  93.         yield return new WaitForSeconds(length+0.1f);
  94.         FinishedPlaying();
  95.  
  96.     }
  97.     void FinishedPlaying()
  98.     {
  99.         source.clip = null;
  100.         source.enabled = false;
  101.         AudioClipExtention.SourceFinishedPlaying(this);
  102.     }
  103.  
  104. }
RAW Paste Data