GaelVanhalst

Linked: OneShotAudio

Jan 25th, 2016
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.07 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. //This One shot audio is meant as an alternative on the default unity One Shot Audio.
  5. //With this version you can use different pitches and a reference to the oneshot can be kept, so that volume can be altered while playing.
  6. public class OneShotAudio : MonoBehaviour {
  7.  
  8.     [SerializeField]
  9.     private AudioSource _gameAudio = null;
  10.  
  11.     private bool _playing;
  12.  
  13.     private Coroutine _destructionCoroutine;
  14.  
  15.     //The script that has called the one shot.
  16.     private IOneShotAudioOwner _owner;
  17.  
  18.     public float Volume
  19.     {
  20.         get { return _gameAudio.volume; }
  21.         set { _gameAudio.volume = value; }
  22.     }
  23.    
  24.     public void StartPlayingSound(AudioClip audioClip,float volume,IOneShotAudioOwner owner,float pitch=1f)
  25.     {
  26.         if (!CheckIfValid())
  27.         {
  28.             Destroy(gameObject);
  29.             return;
  30.         }
  31.         if (audioClip == null)
  32.         {
  33.             DestroyAudio();
  34.             return;
  35.         }
  36.         _gameAudio.clip = audioClip;
  37.         _gameAudio.volume = volume;
  38.         _gameAudio.pitch = pitch;
  39.         _owner = owner;
  40.         SetPlaying(true);
  41.     }
  42.  
  43.     public void StartPlayingSound(AudioClip audioClip, float unscaledVolume, float pitch = 1f)
  44.     {
  45.         StartPlayingSound(audioClip, unscaledVolume, null,pitch);
  46.     }
  47.  
  48.     //Check if there is an audiosource.
  49.     private bool CheckIfValid()
  50.     {
  51.         if (_gameAudio == null)
  52.         {
  53.             Debug.LogError(gameObject.name+" lacks an Audio source!");
  54.             return false;
  55.         }
  56.         return true;
  57.     }
  58.  
  59.     IEnumerator StartTimedDestroy()
  60.     {
  61.         //Wait until the clip is done with playing to destroy
  62.         while (_gameAudio.isPlaying||_gameAudio.timeSamples>0)
  63.         {
  64.             yield return null;
  65.         }
  66.         DestroyAudio();
  67.     }
  68.  
  69.     void DestroyAudio()
  70.     {
  71.         SetPlaying(false);
  72.         if (_owner != null) _owner.OneShotAudioGetsDestroyed(this);
  73.         AudioManager.PutBackOnOneShotAudioPool(this);
  74.        
  75.     }
  76.  
  77.     public void Reset()
  78.     {
  79.         SetPlaying(false);
  80.     }
  81.  
  82.     private void SetPlaying(bool playing)
  83.     {
  84.         if(playing==_playing)return;
  85.         _playing = playing;
  86.         if (_playing)
  87.         {
  88.             _gameAudio.Play();
  89.  
  90.             //Prepare for destruction when done
  91.             if (_destructionCoroutine != null) StopCoroutine(_destructionCoroutine);
  92.             _destructionCoroutine=StartCoroutine(StartTimedDestroy());
  93.  
  94.             AudioManager.AddGameAudio(_gameAudio);
  95.         }
  96.         else
  97.         {
  98.             AudioManager.RemoveGameAudio(_gameAudio);
  99.  
  100.             //Stop destruction
  101.             if (_destructionCoroutine != null)
  102.             {
  103.                 _gameAudio.Stop();
  104.                 StopCoroutine(_destructionCoroutine);
  105.                 _destructionCoroutine = null;
  106.             }
  107.         }
  108.     }
  109. }
  110.  
  111. public interface IOneShotAudioOwner
  112. {
  113.     //This is so that the owner knows the oneshot is done. So that by example references can get cleaned
  114.     void OneShotAudioGetsDestroyed(OneShotAudio audio);
  115. }
Add Comment
Please, Sign In to add comment