• QQ
  • nahooten@sina.com
  • 常州市九洲新世界花苑15-2

游戏开发

U3D 客户端框架:资源管理器

原创内容,转载请注明原文网址:http://homeqin.cn/a/wenzhangboke/jishutiandi/youxikaifa/2019/0309/408.html

 

常州游戏开发-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();
        }
 
 
 
    }
 

上篇:上一篇:常州手游开发培训-Unity3d资源管理分析
下篇:下一篇:常州手游开发-U3D客户端框架:UI框架