123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378 |
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- namespace Engine.CLoader
- {
- /// <summary>加载资源时的参数对象</summary>
- public class CLoaderData
- {
- /// <summary>资源名称数据</summary>
- public List<string> listAssetsName = null;
- /// <summary>下载进度</summary>
- public Dictionary<string, int> DictProgress = null;
- /// <summary>加载完成回调函数</summary>
- public OnDelegateLoadComplete onComplete = null;
- /// <summary>下载进度回调</summary>
- public OnDelegateLoadProgress onProgress = null;
- /// <summary>其他参数</summary>
- public object objectParam = null;
- /// <summary>下载的资源类型</summary>
- public AssetsType assetsType = AssetsType.Assets;
- /// <summary>数据源对象</summary>
- private List<CAssetsInfo> listAssetsSource = new List<CAssetsInfo>();
- /// <summary>是否已经被释放</summary>
- private bool isDispose = false;
- /// <summary>构造函数</summary>
- /// <param name="assetsName">List<string> 需要下载的资源包名列表</param>
- /// <param name="aType">资源类型</param>
- /// <param name="completeCallBack">下载完成回调</param>
- /// <param name="progressCallBack">下载进度回调</param>
- /// <param name="param">其他参数</param>
- public CLoaderData(List<string> assetsName = null, int aType = 1, OnDelegateLoadComplete completeCallBack = null, OnDelegateLoadProgress progressCallBack = null, object param = null)
- {
- listAssetsName = (assetsName != null) ? assetsName : new List<string>();
- onComplete = completeCallBack;
- onProgress = progressCallBack;
- objectParam = param;
- assetsType = (AssetsType)aType;
- }
- /// <summary>是否需要下载</summary>
- public bool IsNeedLoad
- {
- get{ return (listAssetsName != null && listAssetsName.Count > 0) ? true : false; }
- }
- /// <summary>资源的配置数据</summary>
- private List<CAssetsConfigInfo> assetsConfigList = new List<CAssetsConfigInfo>();
- /// <summary>资源的配置数据</summary>
- public List<CAssetsConfigInfo> AssetsConfigList
- {
- get
- {
- //资源类型是资源配置的资源可能没有依赖资源
- if (assetsConfigList.Count == 0 && assetsType != AssetsType.Config)
- {
- for (int i = 0; i < listAssetsName.Count; i++)
- {
- assetsConfigList.Add(CLoaderConfig.GetInstance().GetAssetsConfig(listAssetsName[i]));
- }
- }
- return assetsConfigList;
- }
- }
- /// <summary>加载一个GameObjec</summary>
- public UnityEngine.Object LoadObject(string assetsName, string objectName)
- {
- if (string.IsNullOrEmpty (assetsName) || string.IsNullOrEmpty (objectName))
- {
- return null;
- }
- CAssetsInfo assetsInfo = null;
- for(int i=0;i<listAssetsSource.Count;i++)
- {
- if (listAssetsSource[i].assetsName == assetsName)
- {
- assetsInfo = listAssetsSource[i];
- }
- }
- if (assetsInfo != null && assetsInfo.assetsBundle != null)
- {
- return assetsInfo.assetsBundle.LoadAsset(objectName);
- }
- else
- {
- return null;
- }
- }
- public void DebugAllAssets(string assetsName)
- {
- CAssetsInfo assetsInfo = null;
- for (int i = 0; i < listAssetsSource.Count; i++)
- {
- if (listAssetsSource[i].assetsName == assetsName)
- {
- assetsInfo = listAssetsSource[i];
- }
- }
- if(assetsInfo!=null)
- {
- UnityEngine.Object [] arrObj = assetsInfo.assetsBundle.LoadAllAssets();
- foreach(UnityEngine.Object obj in arrObj)
- {
- CDebug.Log(obj.name + " " + obj.ToString());
- }
- }
- }
- /// <summary>添加资源</summary>
- public void AddAssets(string assetsName, bool isDebug = true)
- {
- if (string.IsNullOrEmpty (assetsName))
- {
- return;
- }
- for (int i = 0; i < listAssetsName.Count; i++)
- {
- if (listAssetsName[i] == assetsName)
- {
- #if UNITY_EDITOR
- if (isDebug)
- {
- CDebug.LogError("需要下载的资源已经存在:" + assetsName);
- }
- #endif
- return;
- }
- }
- listAssetsName.Add(assetsName);
- //有新资源加入,需要重新获取资源配置
- assetsConfigList.Clear();
- }
- /// <summary>释放函数</summary>
- public void Dispose()
- {
- DisposeDelegate();
- listAssetsName = null;
- int forLeng = listAssetsSource.Count;
- for (int i = 0; i < forLeng; i++)
- {
- DisposeAssets(listAssetsSource[i]);
- }
- listAssetsSource.Clear();
- isDispose = true;
- }
- /// <summary>释放下载委托</summary>
- public void DisposeDelegate()
- {
- onComplete = null;
- onProgress = null;
- objectParam = null;
- }
- /// <summary>释放一个资源</summary>
- private void DisposeAssets(CAssetsInfo assetsInfo)
- {
- //资源本身的使用计数
- assetsInfo.DelUseCount();
- //资源依赖项的实用技术
- for (int i = 0; i < assetsInfo.depAssetsList.Count; i++)
- {
- DisposeAssets(assetsInfo.depAssetsList[i]);
- }
- }
- /// <summary>修改下载进度</summary>
- public void UpdateProgress(string assetsName, int nProgress)
- {
- if (assetsType == AssetsType.Config)
- {
- return;
- }
- //这里可以根据配置文件中的size 和 CLoaderAB.LoadProgress 计算下载进度、下载速度等数值。
- this.NewDictProgress();
- //修改进度
- if (DictProgress.ContainsKey(assetsName))
- {
- DictProgress[assetsName] = nProgress;
- }
- if(onProgress!=null)
- {
- onProgress(this);
- }
- }
- /// <summary>获得下载进度</summary>
- public int LoadProgress
- {
- get
- {
- if (assetsType == AssetsType.Config)
- {
- return 0;
- }
- float loadProgress = 0;
- float assetsScale = 1.0f / DictProgress.Count;
- foreach(int progress in DictProgress.Values)
- {
- loadProgress += (float)progress * assetsScale;
- }
- return (int)loadProgress;
- }
- }
- /// <summary>实例化下载进度数据</summary>
- private void NewDictProgress()
- {
- if(this.listAssetsName==null)
- {
- return;
- }
- if (assetsType == AssetsType.Config)
- {
- return;
- }
- if (DictProgress != null)
- {
- return;
- }
- DictProgress = new Dictionary<string, int>();
- //获得需要下载的全部资源
- int forLeng = this.listAssetsName.Count;
- for (int i = 0; i < forLeng; i++)
- {
- this.AddDictProgress(listAssetsName[i]);
- }
- }
- /// <summary>添加需要计算进度的资源</summary>
- private void AddDictProgress(string assestName)
- {
- if(!DictProgress.ContainsKey(assestName))
- {
- DictProgress.Add(assestName, 0);
- }
- CAssetsConfigInfo assetsConfigInfo = CLoaderConfig.GetInstance().GetAssetsConfig(assestName);
- int forLeng = assetsConfigInfo.assetsDepNameList.Count;
- for (int i = 0; i < forLeng; i++)
- {
- AddDictProgress(assetsConfigInfo.assetsDepNameList[i]);
- }
- }
- /// <summary>资源下载完毕</summary>
- public void OnCompleteCallBack()
- {
- //如果已经被释放,就在释放一次
- if(isDispose)
- {
- Dispose();
- }
- if (onComplete != null)
- {
- onComplete(this);
- }
- }
- /// <summary>数据源对象 临时的</summary>
- private Dictionary<string, CAssetsInfo> dictAssetsSource = new Dictionary<string, CAssetsInfo>();
-
- /// <summary>检查资源是否下载完毕</summary>
- internal bool ChectLoadComplete(CAssetsInfo assetsInfo)
- {
- //添加到列表中
- if (!dictAssetsSource.ContainsKey(assetsInfo.assetsName))
- {
- dictAssetsSource.Add(assetsInfo.assetsName, assetsInfo);
- }
- else
- {
- #if UNITY_EDITOR
- CDebug.LogWarning("有资源的下载完成逻辑执行重复: assetsName=" + assetsInfo.assetsName);
- #endif
- }
- int forLeng = 0;
- if (assetsType != AssetsType.Config)
- {
- forLeng = this.AssetsConfigList.Count;
- for (int i = 0; i < forLeng; i++)
- {
- if (!this.CheckDepLoadComplete(AssetsConfigList[i], listAssetsSource))
- {
- return false;
- }
- }
- }
- else
- {
- forLeng = this.listAssetsName.Count;
- for (int i = 0; i < forLeng; i++)
- {
- //资源本身下载完毕
- if (dictAssetsSource.ContainsKey(listAssetsName[i]))
- {
- AssetsAddGoList(listAssetsSource, dictAssetsSource[listAssetsName[i]]);
- }
- else
- {
- return false;
- }
- }
- }
- //清理临时数据引用
- dictAssetsSource.Clear();
- return true;
- }
- /// <summary>检查依赖的资源是否下载完毕</summary>
- private bool CheckDepLoadComplete(CAssetsConfigInfo configInfo, List<CAssetsInfo> depAssetList)
- {
- if (configInfo==null)
- {
- #if UNITY_EDITOR
- CDebug.LogError("有检查资源是否现在完成时发生错误");
- #endif
- return true;
- }
- //资源本身下载完毕
- if(dictAssetsSource.ContainsKey(configInfo.assetsName))
- {
- AssetsAddGoList(depAssetList, dictAssetsSource[configInfo.assetsName]);
- }
- else
- {
- return false;
- }
- int depCount = configInfo.AssetsDepInfoList.Count;
- bool isComplete = true;
- for (int i = 0; i < depCount; i++)
- {
- isComplete = CheckDepLoadComplete(configInfo.AssetsDepInfoList[i], dictAssetsSource[configInfo.assetsName].depAssetsList);
- //有一个依赖的资源未下载完成就认为下载失败
- if(!isComplete)
- {
- return false;
- }
- }
- return isComplete;
- }
- /// <summary>把资源对象添加到资源对象列表中</summary>
- private void AssetsAddGoList(List<CAssetsInfo> depAssetList, CAssetsInfo assetsInfo)
- {
- if (depAssetList.IndexOf(assetsInfo) == -1)
- {
- depAssetList.Add(assetsInfo);
- }
- }
- }
- }
|