/// <summary> /// 克隆(浅克隆) /// </summary> /// <returns>一个新的BundleLoadInfo实例</returns> public BundleLoadInfo clone() { BundleLoadInfo newInfo = new BundleLoadInfo(); newInfo.fullName = fullName; newInfo.loadProgressNum = loadProgressNum; newInfo.unZipProgressNum = unZipProgressNum; newInfo.error = error; newInfo.assetBundle = assetBundle; newInfo.priority = priority; newInfo.loadType = loadType; newInfo.www = www; newInfo.assets = assets; newInfo.loadStart = loadStart; newInfo.loadProgress = loadProgress; newInfo.loadEnd = loadEnd; newInfo.loadFail = loadFail; return newInfo; }
/// <summary> /// 存储数据 /// </summary> private void store(BundleLoadInfo loadInfo) { WWW www = loadInfo.www; if (www.url.Contains(LoadConstant.CDN)) { ManifestVO vo = ManifestManager.instance.getManifestVO(loadInfo.fullName); File.WriteAllBytes(LoadConstant.localFilesPath + "/" + loadInfo.fullName + "@" + vo.crc, www.bytes); } BundleLoadInfo newInfo = new BundleLoadInfo(); newInfo.fullName = loadInfo.fullName; newInfo.loadProgressNum = 1; newInfo.assetBundle = www.assetBundle; cacheDic.Add(newInfo.fullName, newInfo); }
/// <summary> /// 加载具体的项 /// </summary> /// <param name="loadInfo"></param> private void addLoadItem(BundleLoadInfo loadInfo) { ManifestVO vo = ManifestManager.instance.getManifestVO(loadInfo.fullName); if (File.Exists(LoadConstant.localFilesPath + "/" + loadInfo.fullName + "@" + vo.crc)) { if (loadInfo.loadType == LoadType.web) { if (loadInfo.loadStart != null) { loadInfo.loadStart(LoadData.getLoadData(loadInfo.fullName)); } if (loadInfo.loadEnd != null) { loadInfo.loadEnd(LoadData.getLoadData(loadInfo.fullName, 1)); } return; } else { loadInfo.loadType = LoadType.local; } } else { if (loadInfo.loadType == LoadType.local) { Debug.LogWarning(loadInfo.fullName + "加载失败:" + LoadConstant.LOCAL_LOAD_ERROR); if (loadInfo.loadStart != null) { loadInfo.loadStart(LoadData.getLoadData(loadInfo.fullName)); } if (loadInfo.loadFail != null) { loadInfo.loadFail(LoadData.getLoadData(loadInfo.fullName, 0, LoadConstant.LOCAL_LOAD_ERROR)); } return; } else { loadInfo.loadType = LoadType.web; } } //已经在缓存中 if (cacheDic.ContainsKey(loadInfo.fullName)) { if (loadInfo.loadStart != null) { loadInfo.loadStart(LoadData.getLoadData(loadInfo.fullName)); } if (loadInfo.loadEnd != null) { loadInfo.loadEnd(LoadData.getLoadData(loadInfo.fullName, 1, null, cacheDic[loadInfo.fullName].assetBundle)); } return; } //已经在加载过程中 BundleLoadInfo info = getInLoadingList(loadInfo.fullName); if (info != null) { if (loadInfo.loadStart != null) { loadInfo.loadStart(LoadData.getLoadData(loadInfo.fullName)); } delegateAddition(info.loadProgress, loadInfo.loadProgress); delegateAddition(info.loadEnd, loadInfo.loadEnd); delegateAddition(info.loadFail, loadInfo.loadFail); return; } //已经在等待列表中 info = getInWaitList(loadInfo.fullName); if (info != null) { //如果优先级不一致,则转为优先级较高的(即数字小的) if ((int)info.priority > (int)loadInfo.priority) { waitLists[(int)loadInfo.priority].add(loadInfo.fullName, info); waitLists[(int)info.priority].remove(loadInfo.fullName); info.priority = loadInfo.priority; } delegateAddition(info.loadStart, loadInfo.loadStart); delegateAddition(info.loadProgress, loadInfo.loadProgress); delegateAddition(info.loadEnd, loadInfo.loadEnd); delegateAddition(info.loadFail, loadInfo.loadFail); return; } //添加到等待列表中 waitLists[(int)loadInfo.priority].add(loadInfo.fullName, loadInfo); loadNext(); }
/// <summary> /// 加载资源,如果资源已经加载过,则直接从缓存中获取 /// </summary> /// <param name="loadInfo">加载资源的相关信息</param> public void addLoad(BundleLoadInfo loadInfo) { if (string.IsNullOrEmpty(loadInfo.fullName)) { throw new Exception("BundleLoadManager.instance.load()的fullName不能为空"); } List<string> dependencies = ManifestManager.instance.getAllDependencies(loadInfo.fullName); if (dependencies == null) { throw new Exception("未找到" + loadInfo.fullName + "的Manifest信息"); } dependencies.Add(loadInfo.fullName); BundleLoadInfo newInfo; for (int i = 0, len = dependencies.Count; i < len; i++ ) { newInfo = loadInfo.clone(); newInfo.fullName = dependencies[i]; addLoadItem(newInfo); } }
/// <summary> /// 加载资源,如果资源已经加载过,则直接从缓存中获取 /// </summary> /// <param name="fullName">全名</param> /// <param name="priority">优先级</param> /// <param name="loadType">加载类型</param> /// <param name="loadStart">加载开始前执行的方法</param> /// <param name="loadProgress">加载开始后且在结束前每帧执行的方法</param> /// <param name="loadEnd">加载结束后执行的方法</param> /// <param name="loadFail">加载失败后执行的方法</param> public void addLoad(string fullName, LoadPriority priority = LoadPriority.two, LoadType loadType = LoadType.local, LoadFunctionDele loadStart = null, LoadFunctionDele loadProgress = null, LoadFunctionDele loadEnd = null, LoadFunctionDele loadFail = null) { BundleLoadInfo loadInfo = new BundleLoadInfo(); loadInfo.fullName = fullName; loadInfo.priority = priority; loadInfo.loadType = loadType; loadInfo.loadStart = loadStart; loadInfo.loadProgress = loadProgress; loadInfo.loadEnd = loadEnd; loadInfo.loadFail = loadFail; addLoad(loadInfo); }