private static void StartLoad(LoadTask task) { string abName = task.abName; //先加载依赖AB string[] dependenciesAB = _manifest.GetAllDependencies(abName); foreach (string dpdAB in dependenciesAB) { if (_abDic.ContainsKey(dpdAB)) { continue; } task.dependencies.Add(dpdAB); if (_loadingABCountDic.ContainsKey(dpdAB)) { _loadingABCountDic[dpdAB]++; continue; } _loadingABCountDic.Add(dpdAB, 1); string relativePath = AssetPathUtil.PlatformString + "/" + dpdAB; string dpdUrl; if (VersionManager.HasCache(relativePath)) { dpdUrl = VersionManager.GetPath(relativePath); } else { dpdUrl = AssetPathUtil.GetAssetBundlePath(dpdAB); } LoaderManager.Load(dpdUrl, EnumResouceType.ASSETBUNDLE, OnABLoadComplete); } if (task.IsDependenciesLoadComplete()) { if (!_loadingABCountDic.ContainsKey(abName)) { _loadingABCountDic.Add(abName, 1); string relativePath = AssetPathUtil.PlatformString + "/" + abName; string abUrl; if (VersionManager.HasCache(relativePath)) { abUrl = VersionManager.GetPath(relativePath); } else { abUrl = AssetPathUtil.GetAssetBundlePath(abName); } LoaderManager.Load(abUrl, EnumResouceType.ASSETBUNDLE, OnABLoadComplete); } else { _loadingABCountDic[task.abName]++; } } }
private static void UnloadLoadingAB(string abName) { if (_loadingABCountDic.ContainsKey(abName)) { _loadingABCountDic[abName]--; if (_loadingABCountDic[abName] == 0) { _loadingABCountDic.Remove(abName); LoaderManager.Unload(AssetPathUtil.GetAssetBundlePath(abName), OnABLoadComplete); } } }
/// <summary> /// 开始预加载assetbundle /// </summary> /// <param name="abName"></param> /// <param name="OnLoadComplete">参数为AssetBundle</param> private static void PreloadAssetBundle(string abName) { if (_abDic.ContainsKey(abName)) { PreloadNext(); return; } string relativePath = AssetPathUtil.PlatformString + "/" + abName; if (VersionManager.HasCache(relativePath)) { LoaderManager.Load(VersionManager.GetPath(relativePath), EnumResouceType.ASSETBUNDLE, OnPreloadOne); } else { LoaderManager.Load(AssetPathUtil.GetAssetBundlePath(abName), EnumResouceType.ASSETBUNDLE, OnPreloadOne); } }
private static void OnABLoadComplete(object content) { AssetBundle ab = (AssetBundle)content; _abDic.Add(ab.name, ab); _loadingABCountDic.Remove(ab.name); foreach (LoadTask task in _taskDic.Values) { if (task.IsDependenciesLoadComplete() && ab.name == task.abName) { _loadedTask.Add(task); } else if (task.dependencies.Contains(ab.name)) { task.dependencies.Remove(ab.name); if (task.IsDependenciesLoadComplete()) { if (!_loadingABCountDic.ContainsKey(task.abName)) { _loadingABCountDic.Add(task.abName, 1); string relativePath = AssetPathUtil.PlatformString + "/" + task.abName; string abUrl; if (VersionManager.HasCache(relativePath)) { abUrl = VersionManager.GetPath(relativePath); } else { abUrl = AssetPathUtil.GetAssetBundlePath(task.abName); } LoaderManager.Load(abUrl, EnumResouceType.ASSETBUNDLE, OnABLoadComplete); } else { _loadingABCountDic[task.abName]++; } } } } foreach (LoadTask task in _loadedTask) { _taskDic.Remove(task.resCallback); object obj;// = ab.LoadAsset(StringUtil.GetFileName(task.resPath)); if (!_assetDic.ContainsKey(task.resPath)) { obj = ab.LoadAsset(Path.GetFileNameWithoutExtension(task.resPath)); _assetDic.Add(task.resPath, obj); if (task.isFinishUnload) { string[] names = ab.GetAllAssetNames(); foreach (string assName in names) { string resPath = FileUtil.GetRelativeResourcePath(assName); if (_assetDic.ContainsKey(resPath)) { continue; } _assetDic.Add(resPath, ab.LoadAsset(Path.GetFileNameWithoutExtension(resPath))); } _abDic.Remove(ab.name); ab.Unload(false); } } else { obj = _assetDic[task.resPath]; } task.resCallback.Invoke(obj); task.Dispose(); obj = null; } _loadedTask.Clear(); ab = null; }