public void RemovePackage(string type) { if (RemovePackageRef(type)) { var p = UIPackage.GetByName(type); if (p != null) { UIPackage.RemovePackage(type); } } /*UIPackage package; * * if(packages.TryGetValue(type, out package)) * { * var p = UIPackage.GetByName(package.name); * * if (p != null) * { * UIPackage.RemovePackage(package.name); * } * * packages.Remove(package.name); * }*/ if (!Define.IsEditorMode) { string uiBundleDesName = AssetBundleHelper.StringToAB($"{type}_fui"); string uiBundleResName = AssetBundleHelper.StringToAB(type); Game.Scene.GetComponent <ResourcesComponent>().UnloadBundle(uiBundleDesName); Game.Scene.GetComponent <ResourcesComponent>().UnloadBundle(uiBundleResName); } }
// 加载ab包中的all assets private static async ETTask LoadOneBundleAllAssets(this ResourcesComponent self, ABInfo abInfo) { CoroutineLock coroutineLock = null; try { coroutineLock = await CoroutineLockComponent.Instance.Wait(CoroutineLockType.Resources, abInfo.Name.GetHashCode()); if (abInfo.IsDisposed || abInfo.AlreadyLoadAssets) { return; } if (abInfo.AssetBundle != null && !abInfo.AssetBundle.isStreamedSceneAssetBundle) { // 异步load资源到内存cache住 var assets = await AssetBundleHelper.UnityLoadAssetAsync(abInfo.AssetBundle); foreach (UnityEngine.Object asset in assets) { self.AddResource(abInfo.Name, asset.name, asset); } } abInfo.AlreadyLoadAssets = true; } finally { coroutineLock?.Dispose(); } }
public async ETTask AddPackageAsync(string type) { if (Define.IsEditorMode) { await ETTask.CompletedTask; if (AddPackageRef(type)) { UIPackage.AddPackage($"{FGUI_PACKAGE_DIR}/{type}"); } //UIPackage uiPackage = UIPackage.AddPackage($"{FGUI_PACKAGE_DIR}/{type}"); //packages.Add(type, uiPackage); } else { if (AddPackageRef(type)) { string uiBundleDesName = AssetBundleHelper.StringToAB($"{type}_fui"); string uiBundleResName = AssetBundleHelper.StringToAB(type); ResourcesComponent resourcesComponent = Game.Scene.GetComponent <ResourcesComponent>(); await resourcesComponent.LoadBundleAsync(uiBundleDesName); await resourcesComponent.LoadBundleAsync(uiBundleResName); AssetBundle desAssetBundle = resourcesComponent.GetAssetBundle(uiBundleDesName); AssetBundle resAssetBundle = resourcesComponent.GetAssetBundle(uiBundleResName); UIPackage.AddPackage(desAssetBundle, resAssetBundle); } //UIPackage uiPackage = UIPackage.AddPackage(desAssetBundle, resAssetBundle); //packages.Add(type, uiPackage); } }
public AssetBundle GetAssetBundle(string abName) { ABInfo abInfo; if (!bundles.TryGetValue(AssetBundleHelper.BundleNameToLower(abName), out abInfo)) { throw new Exception($"not found bundle: {abName}"); } return(abInfo.AssetBundle); }
public void UnloadBundle(string assetBundleName) { assetBundleName = assetBundleName.BundleNameToLower(); string[] dependencies = AssetBundleHelper.GetSortedDependencies(assetBundleName); //Log.Debug($"-----------dep unload {assetBundleName} dep: {dependencies.ToList().ListToString()}"); foreach (string dependency in dependencies) { this.UnloadOneBundle(dependency); } }
/// <summary> /// 异步加载assetbundle /// </summary> /// <param name="assetBundleName"></param> /// <returns></returns> public async ETTask LoadBundleAsync(string assetBundleName) { assetBundleName = assetBundleName.ToLower(); string[] dependencies = AssetBundleHelper.GetSortedDependencies(assetBundleName); // Log.Debug($"-----------dep load {assetBundleName} dep: {dependencies.ToList().ListToString()}"); foreach (string dependency in dependencies) { if (string.IsNullOrEmpty(dependency)) { continue; } await this.LoadOneBundleAsync(dependency); } }
// 加载ab包中的all assets private async ETTask LoadOneBundleAllAssets(ABInfo abInfo) { using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.Resources, abInfo.Name.GetHashCode())) { if (abInfo.IsDisposed || abInfo.AlreadyLoadAssets) { return; } if (abInfo.AssetBundle != null && !abInfo.AssetBundle.isStreamedSceneAssetBundle) { // 异步load资源到内存cache住 var assets = await AssetBundleHelper.UnityLoadAssetAsync(abInfo.AssetBundle); foreach (UnityEngine.Object asset in assets) { AddResource(abInfo.Name, asset.name, asset); } } abInfo.AlreadyLoadAssets = true; } }
private static async ETTask <ABInfo> LoadOneBundleAsync(this ResourcesComponent self, string assetBundleName) { assetBundleName = assetBundleName.BundleNameToLower(); ABInfo abInfo; if (self.bundles.TryGetValue(assetBundleName, out abInfo)) { ++abInfo.RefCount; //Log.Debug($"---------------load one bundle {assetBundleName} refcount: {abInfo.RefCount}"); return(null); } string p = ""; AssetBundle assetBundle = null; if (!Define.IsAsync) { if (Define.IsEditor) { string[] realPath = Define.GetAssetPathsFromAssetBundle(assetBundleName); foreach (string s in realPath) { string assetName = Path.GetFileNameWithoutExtension(s); UnityEngine.Object resource = Define.LoadAssetAtPath(s); self.AddResource(assetBundleName, assetName, resource); } if (realPath.Length > 0) { abInfo = self.AddChild <ABInfo, string, AssetBundle>(assetBundleName, null); self.bundles[assetBundleName] = abInfo; //Log.Debug($"---------------load one bundle {assetBundleName} refcount: {abInfo.RefCount}"); } else { Log.Error("Bundle not exist! BundleName: " + assetBundleName); } // 编辑器模式也不能同步加载 await TimerComponent.Instance.WaitAsync(100); return(abInfo); } } p = Path.Combine(PathHelper.AppHotfixResPath, assetBundleName); if (!File.Exists(p)) { p = Path.Combine(PathHelper.AppResPath, assetBundleName); } Log.Debug("Async load bundle BundleName : " + p); // if (!File.Exists(p)) // { // Log.Error("Async load bundle not exist! BundleName : " + p); // return null; // } assetBundle = await AssetBundleHelper.UnityLoadBundleAsync(p); if (assetBundle == null) { // 获取资源的时候会抛异常,这个地方不直接抛异常,因为有些地方需要Load之后判断是否Load成功 Log.Warning($"assets bundle not found: {assetBundleName}"); return(null); } abInfo = self.AddChild <ABInfo, string, AssetBundle>(assetBundleName, assetBundle); self.bundles[assetBundleName] = abInfo; return(abInfo); //Log.Debug($"---------------load one bundle {assetBundleName} refcount: {abInfo.RefCount}"); }
private async ETTask <ABInfo> LoadOneBundleAsync(string assetBundleName) { assetBundleName = assetBundleName.BundleNameToLower(); ABInfo abInfo; if (this.bundles.TryGetValue(assetBundleName, out abInfo)) { ++abInfo.RefCount; //Log.Debug($"---------------load one bundle {assetBundleName} refcount: {abInfo.RefCount}"); return(null); } string p = ""; AssetBundle assetBundle = null; if (!Define.IsAsync) { #if UNITY_EDITOR string[] realPath = AssetDatabase.GetAssetPathsFromAssetBundle(assetBundleName); foreach (string s in realPath) { string assetName = Path.GetFileNameWithoutExtension(s); UnityEngine.Object resource = AssetDatabase.LoadAssetAtPath <UnityEngine.Object>(s); AddResource(assetBundleName, assetName, resource); } if (realPath.Length > 0) { abInfo = EntityFactory.CreateWithParent <ABInfo, string, AssetBundle>(this, assetBundleName, null); this.bundles[assetBundleName] = abInfo; //Log.Debug($"---------------load one bundle {assetBundleName} refcount: {abInfo.RefCount}"); } else { Log.Error("Bundle not exist! BundleName: " + assetBundleName); } // 编辑器模式也不能同步加载 await TimerComponent.Instance.WaitAsync(100); #endif return(abInfo); } p = Path.Combine(PathHelper.AppHotfixResPath, assetBundleName); if (!File.Exists(p)) { p = Path.Combine(PathHelper.AppResPath, assetBundleName); } if (!File.Exists(p)) { Log.Error("Async load bundle not exist! BundleName : " + p); return(null); } assetBundle = await AssetBundleHelper.UnityLoadBundleAsync(p); if (assetBundle == null) { // 获取资源的时候会抛异常,这个地方不直接抛异常,因为有些地方需要Load之后判断是否Load成功 Log.Warning($"assets bundle not found: {assetBundleName}"); return(null); } abInfo = EntityFactory.CreateWithParent <ABInfo, string, AssetBundle>(this, assetBundleName, assetBundle); this.bundles[assetBundleName] = abInfo; return(abInfo); //Log.Debug($"---------------load one bundle {assetBundleName} refcount: {abInfo.RefCount}"); }