zoukankan      html  css  js  c++  java
  • 资源管理类

    using System;
    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;

    /// <summary>
    /// 资源管理类
    /// </summary>
    public class ResMgr : MonoBehaviour
    {

    //ResMgr的实例引用
    private static ResMgr mInstance;
    //cpu数量
    private static int mProcessCount = 0;

    //正在加载的资源队列
    private List<ResLoadRequest> mLoadList = new List<ResLoadRequest>();
    //等待加载的资源队列
    private Queue<ResLoadRequest> mWaitLoads = new Queue<ResLoadRequest>();
    //加载完成的资源存储字典,方便后期清理
    private Dictionary<string, ResLoadRequest> mAssetPackDic = new Dictionary<string, ResLoadRequest>();

    public static ResMgr Instance
    {
    get
    {
    return mInstance;
    }
    }

    //游戏物体缓存
    private GameObject mCachedGameObject;

    public GameObject CachedGameObject
    {
    get
    {
    if(!mCachedGameObject)
    {
    mCachedGameObject = this.gameObject;
    }
    return mCachedGameObject;
    }
    }

    void Awake()
    {
    mInstance = this;

    mProcessCount = SystemInfo.processorCount;
    mProcessCount = mProcessCount < 1 ? 1 : mProcessCount;
    mProcessCount = mProcessCount > 8 ? 8 : mProcessCount;
    DontDestroyOnLoad(CachedGameObject);
    }

    void Update()
    {

    //等待队列出队一个并进行Load,之后放入正在加载队列
    if (mLoadList.Count > 0)
    {
    for (int i = mLoadList.Count - 1; i >= 0; i--)
    {
    if (mLoadList[i].isDone)
    {
    LoadFinish(mLoadList[i]);
    mLoadList.RemoveAt(i);
    }
    }
    }

    //等待队列只要进来请求,且cpu有空闲线程,则放入加载队列进行加载
    while (mLoadList.Count < mProcessCount && mWaitLoads.Count > 0)
    {
    ResLoadRequest re = mWaitLoads.Dequeue();
    mLoadList.Add(re);
    re.Load();
    }
    }

    //外部加载资源直接通过这个接口,把参数传给AssetPack资源加载封装包
    public void LoadAssetAsync(string prefabName, ILoadListent callBack, Type type = null, bool isKeepInMemory = false)
    {
    _LoadAssetAsync(prefabName, callBack, type, isKeepInMemory);
    }

    private void _LoadAssetAsync(string prefabName, ILoadListent callBack, Type type, bool isKeepInMemory)
    {
    if(string .IsNullOrEmpty(prefabName))
    {
    if (callBack != null) callBack.Failure();
    return;
    }

    if(mAssetPackDic.ContainsKey(prefabName))
    {
    if (mAssetPackDic[prefabName].asset == null)
    {
    if (null != callBack)
    {
    callBack.Failure();
    }
    }
    else
    {
    callBack.Succeed(mAssetPackDic[prefabName].asset);
    }
    return;
    }

    //遍历查询是否在正在加载队列中,在的话
    for (int i = 0; i < mLoadList.Count; i++)
    {
    ResLoadRequest re = mLoadList[i];
    if(re.assetName.Equals(prefabName))
    {
    re.AddListent(callBack);
    return;
    }
    }

    //等待加载队列中
    foreach (ResLoadRequest item in mWaitLoads)
    {
    if(item.assetName.Equals(prefabName))
    {
    item.AddListent(callBack);
    return;
    }
    }

    ResLoadRequest loadRequest = new ResLoadRequest(prefabName, isKeepInMemory, type);
    loadRequest.listents.Add(callBack);
    mWaitLoads.Enqueue(loadRequest);
    }

    //资源加载完毕类,进行加载完毕后的处理
    void LoadFinish(ResLoadRequest Re)
    {
    if (Re != null)
    {
    for (int i = 0; i < Re.listents.Count; ++i)
    {
    ILoadListent listen = Re.listents[i];
    if (listen != null)
    {
    if (Re.request != null && Re.request.asset != null)
    {
    mAssetPackDic.Add(Re.assetName, Re);
    listen.Succeed(Re.request.asset);
    }
    else
    {
    listen.Failure();
    }
    }
    }
    }
    }

    //资源加载完成后的监听
    public interface ILoadListent
    {
    void Succeed(UnityEngine.Object asset);

    void Failure();
    }

    //资源加载类,也就是对资源的封装
    public class ResLoadRequest
    {
    //资源名字
    public string assetName;
    //加载回调
    public List<ILoadListent> listents = new List<ILoadListent>();
    //加载到的资源
    public ResourceRequest request;
    //资源类型
    public Type type;
    //是否保存在内存
    public bool isKeepInMemorry;
    //添加一个回调到回调列表
    public void AddListent(ILoadListent listen)
    {
    if(!listents.Contains(listen))
    {
    listents.Add(listen);
    }
    }
    //加载到的资源
    public UnityEngine.Object asset
    {
    get
    {
    if (request != null && request.asset != null)
    {
    return request.asset;
    }
    else
    {
    return null;
    }
    }
    }

    //当前是否已经加载完成
    public bool isDone
    {
    get
    {
    if(request != null)
    {
    return true;
    }
    return false;
    }
    }

    //构造函数
    public ResLoadRequest(string _assetName, bool _isKeepIn, Type _type)
    {
    this.assetName = _assetName;
    this.isKeepInMemorry = _isKeepIn;
    this.type = _type;
    }

    //开启加载
    public void Load()
    {
    if(type == null)
    {
    type = typeof(GameObject);
    }
    request = Resources.LoadAsync(assetName, type);
    }
    }


    //清除没用的资源
    public void RemoveAsset(string assetName, bool canRemove)
    {
    if(!mAssetPackDic.ContainsKey(assetName))
    {
    return;
    }

    if (mAssetPackDic[assetName].isKeepInMemorry)
    {
    if (canRemove)
    {
    mAssetPackDic[assetName] = null;
    mAssetPackDic.Remove(assetName);
    }
    }
    else
    {
    mAssetPackDic[assetName] = null;
    mAssetPackDic.Remove(assetName);
    }
    Resources.UnloadUnusedAssets();
    }

    //强制清楚所有资源
    public void RemoveAll()
    {
    foreach (KeyValuePair<string, ResLoadRequest> pair in mAssetPackDic)
    {
    mAssetPackDic[pair.Key] = null;
    }
    mAssetPackDic.Clear();
    Resources.UnloadUnusedAssets();
    }
    }

  • 相关阅读:
    cogs 826. [Tyvj Feb11] GF打dota 次短路详细原创讲解! dijkstra
    cogs 2450. 距离 树链剖分求LCA最近公共祖先 快速求树上两点距离 详细讲解 带注释!
    cogs 647. [Youdao2010] 有道搜索框 Trie树 字典树
    cogs 293. [NOI 2000] 单词查找树 Trie树字典树
    P4550 收集邮票
    P1850 换教室 期望dp
    洛谷 UVA11021 Tribles
    P3802 小魔女帕琪 概率与期望
    P3369 【模板】普通平衡树 01Trie树
    P2765 魔术球问题
  • 原文地址:https://www.cnblogs.com/xwwFrank/p/4654389.html
Copyright © 2011-2022 走看看