Unity3d AudioManager

ResourcesManager from https://www.suntabu.com/page.html?d=1552226655668


using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;
using UnityEngine.Assertions;

public class AudioManager : MonoBehaviour
{
    /// <summary>
    /// cached audiosource componets list. 
<!-- more -->

    /// </summary>
    private List<AudioSource> sources = new List<AudioSource>(4);

    private static AudioManager _instance;

    public static AudioManager Instance
    {
        get
        {
            if (_instance == null)
            {
                var go = new GameObject("[AudioManager]");
                _instance = go.AddComponent<AudioManager>();
            }

            return _instance;
        }
    }

    /// <summary>
    /// play an audioclip by its RESOURCES path.
    /// </summary>
    /// <param name="clipPath">path in RESOURCES folder</param>
    /// <param name="loop">looping play</param>
    public void Play(string clipPath, bool loop = false)
    {
        var clip = ResourcesManager.Instance.Load<AudioClip>(clipPath);
        Assert.IsNotNull(clip, "clip load FAILED from " + clipPath);
        Play(clip, loop);
    }

    public void Play(AudioClip clip, bool loop = false)
    {
        Assert.IsNotNull(clip, "clip == null");
        var source = GetAvailableAudioSource();
        source.clip = clip;
        source.loop = loop;
        source.Play();
    }

    /// <summary>
    /// stop playing audioclip, like BGM.
    /// </summary>
    /// <param name="clip"></param>
    public void Stop(AudioClip clip)
    {
        for (int i = 0; i < sources.Count; i++)
        {
            var s = sources[i];
            if (s.clip == clip)
            {
                s.Stop();
            }
        }
    }


    /// <summary>
    /// get audiosource to play audioclip, if no available audiosource in cached list, a new audiosource would be
    /// created and cached.
    /// </summary>
    /// <returns>the available audiosource</returns>
    AudioSource GetAvailableAudioSource()
    {
        AudioSource source = null;
        for (int i = 0; i < sources.Count; i++)
        {
            var s = sources[i];
            if (s.clip == null || !s.isPlaying)
            {
                source = s;
                break;
            }
        }

        if (source == null)
        {
            var go = new GameObject("[AudioSource]");
            go.transform.SetParent(this.transform);
            source = go.AddComponent<AudioSource>();
            sources.Add(source);
        }

        return source;
    }

    void Start()
    {
        DontDestroyOnLoad(this.gameObject);
    }

    void Update()
    {
        for (int i = 0; i < sources.Count; i++)
        {
            var s = sources[i];
            if (!s.isPlaying)
            {
                // unload unused audioclip.
                ResourcesManager.Instance.Unload(s.clip);
                s.clip = null;
            }
        }
    }

    private void OnDestroy()
    {
        sources.Clear();
        foreach (Transform child in transform)
        {
            Destroy(child.gameObject);
        }
    }
}
WRITTEN BY:    Suntabu