Advertisement
EdemShukurov

Untitled

Apr 9th, 2020
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.79 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3.  
  4. namespace Game.Common.Audio
  5. {
  6.     public class AudioPooling : MonoBehaviour
  7.     {
  8.         /// <summary>
  9.         /// Init Count of Audio Sources (how much audioclips can be played simultaneously)
  10.         /// </summary>
  11.         [SerializeField] private int _initCountOfAudioSources;
  12.  
  13.         private List<AudioSource> _audioSources;
  14.  
  15.         private AudioRolloffMode _defaultRolloffMode;
  16.         private bool _defaultLoop;
  17.         private int _defaultPriority;
  18.  
  19.         #region Singleton staff
  20.  
  21.         private static AudioPooling _instance;
  22.  
  23.         public static AudioPooling Instance
  24.         {
  25.             get
  26.             {
  27.                 if (_instance == null)
  28.                 {
  29.                     _instance = FindObjectOfType<AudioPooling>();
  30.  
  31.                     if (_instance == null)
  32.                     {
  33.                         // Create gameObject and add component
  34.                         _instance = (new GameObject("AudioPooling")).AddComponent<AudioPooling>();
  35.                     }
  36.                 }
  37.  
  38.                 return _instance;
  39.             }
  40.         }
  41.  
  42.         #endregion
  43.  
  44.         private void Start()
  45.         {
  46.             _defaultRolloffMode = AudioRolloffMode.Linear;
  47.             _defaultLoop = false;
  48.             _defaultPriority = 128;
  49.  
  50.             InitAudioSources();
  51.         }
  52.  
  53.         /// <summary>
  54.         /// Initialize list of Audio Sources
  55.         /// </summary>
  56.         private void InitAudioSources()
  57.         {
  58.             for (int i = 0; i < _initCountOfAudioSources; i++)
  59.             {
  60.                 CreateAudioSource();
  61.             }
  62.         }
  63.  
  64.         /// <summary>
  65.         /// Create new AudioSource
  66.         /// </summary>
  67.         /// <returns>new AudioSource</returns>
  68.         private AudioSource CreateAudioSource()
  69.         {
  70.             // create new GameObject
  71.             var audioSourceGameObject = new GameObject();
  72.  
  73.             // add to new GameObject AudioSource component
  74.             var audioSource = audioSourceGameObject.AddComponent<AudioSource>();
  75.  
  76.             // set parent
  77.             audioSourceGameObject.transform.parent = Instance.gameObject.transform;
  78.  
  79.             _audioSources.Add(audioSource);
  80.  
  81.             return audioSource;
  82.         }
  83.  
  84.         /// <summary>
  85.         /// Set AudioSource properties
  86.         /// </summary>
  87.         /// <param name="position">Position for AudioSource</param>
  88.         /// <param name="maxDistance">Max Distance</param>
  89.         /// <param name="volume">Volume</param>
  90.         /// <returns>AudioSource</returns>
  91.         public AudioSource SetAudioSourceProperties(Vector3 position, float maxDistance, float volume = 1f)
  92.         {
  93.             var audioSource = GetAudioSourceFromPool();
  94.  
  95.             audioSource.transform.position = position;
  96.             audioSource.maxDistance = maxDistance;
  97.             audioSource.volume = Mathf.Clamp01(volume);
  98.             audioSource.rolloffMode = _defaultRolloffMode;
  99.  
  100. // ?? may be the following properties will already be by default
  101.             audioSource.priority = _defaultPriority;
  102.             audioSource.loop = _defaultLoop;
  103.             audioSource.playOnAwake = false;
  104.  
  105.             return audioSource;
  106.         }
  107.  
  108.         /// <summary>
  109.         /// Get AudioSource
  110.         /// </summary>
  111.         /// <remarks>if there is not free AudioSource, return a new AudioSource</remarks>
  112.         /// <returns>AudioSource</returns>
  113.         private AudioSource GetAudioSourceFromPool()
  114.         {
  115.             for (int i = 0; i < _audioSources.Count; i++)
  116.             {
  117.                 if(_audioSources[i].isPlaying == false)
  118.                 {
  119.                     return _audioSources[i];
  120.                 }
  121.             }
  122.  
  123.             return CreateAudioSource();
  124.         }
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement