Unity3d ResourcesManager

Singleton from https://www.suntabu.com/page/?d=1512300646985



using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Assertions;
using UnityToolbag.Singleton;

public class ResourcesManager : Singleton<ResourcesManager>
{
    private ResourcesManager()
<!-- more -->

    {
    }

    /// <summary>
    /// all loaded UnityEngine Object cached in this dictionary for avoiding repeatly loading an asset from disk.
    /// </summary>
    private Dictionary<string, Object> cachedRes = new Dictionary<string, Object>();

    /// <summary>
    /// load or get an asset
    /// </summary>
    /// <param name="path">the asset path, used as a key</param>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    public T Load<T>(string path) where T : Object
    {
        if (cachedRes.ContainsKey(path)&&cachedRes[path]!=null)
        {
            return cachedRes[path] as T;
        }

        var obj = Resources.Load<T>(path);
        Assert.IsNotNull(obj, "FAILED loading from " + path);
        cachedRes[path] = obj;

        return obj;
    }

    public void Unload(string path)
    {
        if (cachedRes.ContainsKey(path))
        {
            Assert.IsNotNull(cachedRes[path], "cached null for :" + path);

            Unload(cachedRes[path]);

            cachedRes.Remove(path);
        }
    }

    public void Unload(Object cachedRe)
    {
        Resources.UnloadAsset(cachedRe);
    }
}

WRITTEN BY:    Suntabu