using UnityEngine;
using System.Collections;
using System.Collections.Generic;
namespace Engine.CLoader
{
/// 加载资源时的参数对象
public class CLoaderData
{
/// 资源名称数据
public List listAssetsName = null;
/// 下载进度
public Dictionary DictProgress = null;
/// 加载完成回调函数
public OnDelegateLoadComplete onComplete = null;
/// 下载进度回调
public OnDelegateLoadProgress onProgress = null;
/// 其他参数
public object objectParam = null;
/// 下载的资源类型
public AssetsType assetsType = AssetsType.Assets;
/// 数据源对象
private List listAssetsSource = new List();
/// 是否已经被释放
private bool isDispose = false;
/// 构造函数
/// List 需要下载的资源包名列表
/// 资源类型
/// 下载完成回调
/// 下载进度回调
/// 其他参数
public CLoaderData(List assetsName = null, int aType = 1, OnDelegateLoadComplete completeCallBack = null, OnDelegateLoadProgress progressCallBack = null, object param = null)
{
listAssetsName = (assetsName != null) ? assetsName : new List();
onComplete = completeCallBack;
onProgress = progressCallBack;
objectParam = param;
assetsType = (AssetsType)aType;
}
/// 是否需要下载
public bool IsNeedLoad
{
get{ return (listAssetsName != null && listAssetsName.Count > 0) ? true : false; }
}
/// 资源的配置数据
private List assetsConfigList = new List();
/// 资源的配置数据
public List 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;
}
}
/// 加载一个GameObjec
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添加资源
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();
}
/// 释放函数
public void Dispose()
{
DisposeDelegate();
listAssetsName = null;
int forLeng = listAssetsSource.Count;
for (int i = 0; i < forLeng; i++)
{
DisposeAssets(listAssetsSource[i]);
}
listAssetsSource.Clear();
isDispose = true;
}
/// 释放下载委托
public void DisposeDelegate()
{
onComplete = null;
onProgress = null;
objectParam = null;
}
/// 释放一个资源
private void DisposeAssets(CAssetsInfo assetsInfo)
{
//资源本身的使用计数
assetsInfo.DelUseCount();
//资源依赖项的实用技术
for (int i = 0; i < assetsInfo.depAssetsList.Count; i++)
{
DisposeAssets(assetsInfo.depAssetsList[i]);
}
}
/// 修改下载进度
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);
}
}
/// 获得下载进度
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;
}
}
/// 实例化下载进度数据
private void NewDictProgress()
{
if(this.listAssetsName==null)
{
return;
}
if (assetsType == AssetsType.Config)
{
return;
}
if (DictProgress != null)
{
return;
}
DictProgress = new Dictionary();
//获得需要下载的全部资源
int forLeng = this.listAssetsName.Count;
for (int i = 0; i < forLeng; i++)
{
this.AddDictProgress(listAssetsName[i]);
}
}
/// 添加需要计算进度的资源
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]);
}
}
/// 资源下载完毕
public void OnCompleteCallBack()
{
//如果已经被释放,就在释放一次
if(isDispose)
{
Dispose();
}
if (onComplete != null)
{
onComplete(this);
}
}
/// 数据源对象 临时的
private Dictionary dictAssetsSource = new Dictionary();
/// 检查资源是否下载完毕
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;
}
/// 检查依赖的资源是否下载完毕
private bool CheckDepLoadComplete(CAssetsConfigInfo configInfo, List 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;
}
/// 把资源对象添加到资源对象列表中
private void AssetsAddGoList(List depAssetList, CAssetsInfo assetsInfo)
{
if (depAssetList.IndexOf(assetsInfo) == -1)
{
depAssetList.Add(assetsInfo);
}
}
}
}