Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using UnityEngine;
- namespace Game.Common.Audio
- {
- public class AudioPooling : MonoBehaviour
- {
- /// <summary>
- /// Init Count of Audio Sources (how much audioclips can be played simultaneously)
- /// </summary>
- [SerializeField] private int _initCountOfAudioSources;
- private List<AudioSource> _audioSources;
- private AudioRolloffMode _defaultRolloffMode;
- private bool _defaultLoop;
- private int _defaultPriority;
- #region Singleton staff
- private static AudioPooling _instance;
- public static AudioPooling Instance
- {
- get
- {
- if (_instance == null)
- {
- _instance = FindObjectOfType<AudioPooling>();
- if (_instance == null)
- {
- // Create gameObject and add component
- _instance = (new GameObject("AudioPooling")).AddComponent<AudioPooling>();
- }
- }
- return _instance;
- }
- }
- #endregion
- private void Start()
- {
- _defaultRolloffMode = AudioRolloffMode.Linear;
- _defaultLoop = false;
- _defaultPriority = 128;
- InitAudioSources();
- }
- /// <summary>
- /// Initialize list of Audio Sources
- /// </summary>
- private void InitAudioSources()
- {
- for (int i = 0; i < _initCountOfAudioSources; i++)
- {
- CreateAudioSource();
- }
- }
- /// <summary>
- /// Create new AudioSource
- /// </summary>
- /// <returns>new AudioSource</returns>
- private AudioSource CreateAudioSource()
- {
- // create new GameObject
- var audioSourceGameObject = new GameObject();
- // add to new GameObject AudioSource component
- var audioSource = audioSourceGameObject.AddComponent<AudioSource>();
- // set parent
- audioSourceGameObject.transform.parent = Instance.gameObject.transform;
- _audioSources.Add(audioSource);
- return audioSource;
- }
- /// <summary>
- /// Set AudioSource properties
- /// </summary>
- /// <param name="position">Position for AudioSource</param>
- /// <param name="maxDistance">Max Distance</param>
- /// <param name="volume">Volume</param>
- /// <returns>AudioSource</returns>
- public AudioSource SetAudioSourceProperties(Vector3 position, float maxDistance, float volume = 1f)
- {
- var audioSource = GetAudioSourceFromPool();
- audioSource.transform.position = position;
- audioSource.maxDistance = maxDistance;
- audioSource.volume = Mathf.Clamp01(volume);
- audioSource.rolloffMode = _defaultRolloffMode;
- // ?? may be the following properties will already be by default
- audioSource.priority = _defaultPriority;
- audioSource.loop = _defaultLoop;
- audioSource.playOnAwake = false;
- return audioSource;
- }
- /// <summary>
- /// Get AudioSource
- /// </summary>
- /// <remarks>if there is not free AudioSource, return a new AudioSource</remarks>
- /// <returns>AudioSource</returns>
- private AudioSource GetAudioSourceFromPool()
- {
- for (int i = 0; i < _audioSources.Count; i++)
- {
- if(_audioSources[i].isPlaying == false)
- {
- return _audioSources[i];
- }
- }
- return CreateAudioSource();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement