Unity 客户端框架(四):资源管理器

资源管理器,顾名思义就是加载资源用的

作用:

当一个项目非常庞大的时候,我们不可能每次都手动去加载释放,这时候需要一个管理器来帮我们实现并管理他们


目前并不完整

//资源类
    public class AssetInfo
    {
        //资源对象
        private UnityEngine.Object _Object;
        //资源类型
        public Type AssetType { get; set; }
        //路径
        public string Path { get; set; }
        //读取次数
        public int RefCount { get; set; }

        public bool IsLoaded
        {
            get
            {
                return null != _Object;
            }
        }

        public UnityEngine.Object AssetObject
        {
            get
            {
                if (null == _Object)
                {
                    _ResourcesLoad();
                }
                return _Object;
            }
        }

        //协程加载
        public IEnumerator GetCoroutineObject(Action<UnityEngine.Object> _loaded)
        {
            while(true)
            {
                yield return null;
                if(!_Object)
                {
                    _ResourcesLoad();
                    yield return null;
                }
                else
                {
                    if (_loaded != null)
                        _loaded(_Object);
                }
                yield break;
            }
        }


      
        //加载
        private void _ResourcesLoad()
        {
            try
            {
                _Object = Resources.Load(Path);
                if (!_Object)
                    Debug.Log("Resources load Failue : " + Path);
            }
            catch(Exception e)
            {
                Debug.Log(e.ToString());
            }
        }


        //异步加载
        public IEnumerator GetAsyncObject(Action<UnityEngine.Object> _loaded,Action<float> _progress = null)
        {
            if(null != _Object)
            {
                _loaded(_Object);
                yield break;
            }

            ResourceRequest _resRequest = Resources.LoadAsync(Path);
            
            while(_resRequest.isDone)
            {
                if (_progress != null)
                    _progress(_resRequest.progress);
                yield return null;
            }

            _Object = _resRequest.asset;
            if(null != _loaded)
            {
                _loaded(_Object);
            }

            yield return _resRequest;

        }

    }

    //资源管理器
    public class ResManager : Singleton<ResManager>
    {
        //已经加载的资源字典
        private Dictionary<string, AssetInfo> dicAssetInfo = null;

        public override void Init()
        {
            dicAssetInfo = new Dictionary<string, AssetInfo>();
        }

        //加载并生成对象 同步 协程 异步
        #region Load Resources & Instantiate Object

        public UnityEngine.Object LoadInstance(string _path)
        {
            UnityEngine.Object _obj = Load(_path);
            return Instantiate(_obj);
        }

        public void LoadCoroutineInstance(string _path, Action<UnityEngine.Object> _loaded = null)
        {
            LoadCoroutine(_path, (_obj) => { Instantiate(_obj, _loaded); });
        }

        public void LoadAsyncInstance(string _path, Action<UnityEngine.Object> _loaded = null, Action<float> _progress = null)
        {
            LoadAsync(_path, (_obj) => { Instantiate(_obj, _loaded); }, _progress);
        }
        #endregion

        //加载不生成对象
        #region Load Resources
        /// <summary>
        /// Load the specified _path.
        /// </summary>
        /// <param name="_path">_path.</param>
        public UnityEngine.Object Load(string _path)
        {
            AssetInfo _assetInfo = GetAssetInfo(_path);
            if (null != _assetInfo)
                return _assetInfo.AssetObject;
            return null;
        }
        #endregion

        //协程加载
        #region Load Coroutine Resources
        
        //_loaded为加载完成后回调
        public void LoadCoroutine(string _path, Action<UnityEngine.Object> _loaded = null)
        {
            AssetInfo _assetInfo = GetAssetInfo(_path, _loaded);
            if (null != _assetInfo)
                CoroutineInstance.Instance.StartCoroutine(_assetInfo.GetCoroutineObject(_loaded));
        }
        #endregion

        //异步加载
        #region Load Async Resources

        //_progress为进度回调
        public void LoadAsync(string _path, Action<UnityEngine.Object> _loaded = null, Action<float> _progress = null)
        {
            AssetInfo _assetInfo = GetAssetInfo(_path, _loaded);
            if (null != _assetInfo)
                CoroutineInstance.Instance.StartCoroutine(_assetInfo.GetAsyncObject(_loaded, _progress));
        }
        #endregion

        //获取资源和生成对象
        #region GetAssetInfo & Instantiate Object

        private AssetInfo GetAssetInfo(string _path,Action<UnityEngine.Object> _loaded = null)
        {

            if(string.IsNullOrEmpty(_path))
            {
                Debug.Log("Error: null _path");
                if (_loaded != null)
                    _loaded(null);
            }

            AssetInfo _assetInfo = null;
            if(!dicAssetInfo.TryGetValue(_path,out _assetInfo))
            {
                _assetInfo = new AssetInfo();
                _assetInfo.Path = _path;
                dicAssetInfo.Add(_path, _assetInfo);
            }
            _assetInfo.RefCount++;
            return _assetInfo;

        }

        private UnityEngine.Object Instantiate(UnityEngine.Object _obj, Action<UnityEngine.Object> _loaded = null)
        {
            UnityEngine.Object _retObj = null;
            if (null != _obj)
            {
                _retObj = MonoBehaviour.Instantiate(_obj);
                if (null != _retObj)
                {
                    if (null != _loaded)
                    {
                        _loaded(_retObj);
                        return null;
                    }
                    return _retObj;
                }
                else
                {
                    Debug.LogError("Error: null Instantiate _retObj.");
                }
            }
            else
            {
                Debug.LogError("Error: null Resources Load return _obj.");
            }
            return null;
        }

        #endregion

        
        void Destroy()
        {
            Resources.UnloadUnusedAssets();
            GC.Collect();
        }



    }


  • 5
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值