private async Task MyLoadOneBundleAsync(string assetBundleName, string path) { ABInfo abInfo; if (this.bundles.TryGetValue(assetBundleName, out abInfo)) { return; } AssetBundle assetBundle; TestTaskList.Add(assetBundleName); Debug.Log(assetBundleName); AssetsBundleLoaderAsync bundleLoaderAsync = new AssetsBundleLoaderAsync(); mAssetsBundleLoaderAsync.Add(bundleLoaderAsync); assetBundle = await bundleLoaderAsync.LoadAsync(assetBundleName, path); this.bundles[assetBundleName] = new ABInfo(assetBundleName, assetBundle); TestTaskList.Remove(assetBundleName); }
async Task LoadOneBundleAsync <T>(string assetBundleName) where T : UnityEngine.Object { //Log.Debug($"---------------load one bundle {assetBundleName}"); ABInfo abInfo; if (this.bundles.TryGetValue(assetBundleName, out abInfo)) { ++abInfo.RefCount; return; } if (this.cacheDictionary.ContainsKey(assetBundleName)) { abInfo = this.cacheDictionary[assetBundleName]; ++abInfo.RefCount; this.bundles[assetBundleName] = abInfo; this.cacheDictionary.Remove(assetBundleName); return; } if (!StaticData.isUseAssetBundle) { #if UNITY_EDITOR //string[] realPath = null; //realPath = AssetDatabase.GetAssetPathsFromAssetBundle(assetBundleName); //foreach (string s in realPath) //{ // string assetName = Path.GetFileNameWithoutExtension(s); // string path = $"{assetBundleName}/{assetName}".ToLower(); // T resource = AssetDatabase.LoadAssetAtPath<T>(s); // this.resourceCache[path] = resource; //} this.bundles[assetBundleName] = new ABInfo(assetBundleName, null); #endif return; } string p = Path.Combine(PathHelper.AppHotfixResPath, assetBundleName); AssetBundle assetBundle = null; if (!File.Exists(p)) { p = Path.Combine(PathHelper.AppResPath, assetBundleName); } AssetsBundleLoaderAsync assetsBundleLoaderAsync = MonoBehaviourHelper.CreateTempComponent <AssetsBundleLoaderAsync>(); assetBundle = await assetsBundleLoaderAsync.LoadAsync(p); if (assetBundle == null) { throw new Exception($"assets bundle not found: {assetBundleName}"); } //if (!assetBundle.isStreamedSceneAssetBundle) //{ // // 异步load资源到内存cache住 // UnityEngine.Object[] assets; // using (AssetsLoaderAsync assetsLoaderAsync = ComponentFactory.Create<AssetsLoaderAsync, AssetBundle>(assetBundle)) // { // assets = await assetsLoaderAsync.LoadAllAssetsAsync<T>(); // } // foreach (UnityEngine.Object asset in assets) // { // string path = $"{assetBundleName}/{asset.name}".ToLower(); // this.resourceCache[path] = asset; // } //} this.bundles[assetBundleName] = new ABInfo(assetBundleName, assetBundle); }
public async Task LoadOneBundleAsync(string assetBundleName, Action <float> actionProgress = null) { //Log.Debug($"---------------load one bundle {assetBundleName}"); ABInfo abInfo; if (this.bundles.TryGetValue(assetBundleName, out abInfo)) { //Log.Debug($"读取到已经有的Bundle: {assetBundleName}"); ++abInfo.RefCount; return; } if (this.cacheDictionary.ContainsKey(assetBundleName)) { abInfo = this.cacheDictionary[assetBundleName]; ++abInfo.RefCount; this.bundles[assetBundleName] = abInfo; this.cacheDictionary.Remove(assetBundleName); return; } if (!StaticData.isUseAssetBundle) { #if UNITY_EDITOR //ZLog.Info("UNITY_EDITOR LoadOneBundleAsync"); string[] realPath = null; realPath = AssetDatabase.GetAssetPathsFromAssetBundle(assetBundleName); //foreach (string s in realPath) //{ // string assetName = Path.GetFileNameWithoutExtension(s); // string path = $"{assetBundleName}/{assetName}".ToLower(); // UnityEngine.Object resource = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(s); // this.resourceCache[path] = resource; //} this.bundles[assetBundleName] = new ABInfo(assetBundleName, null); #endif return; } string p = Path.Combine(PathHelper.AppHotfixResPath, assetBundleName); AssetBundle assetBundle = null; if (!File.Exists(p)) { p = Path.Combine(PathHelper.AppResPath, assetBundleName); } AssetsBundleLoaderAsync assetsBundleLoaderAsync = MonoBehaviourHelper.CreateTempComponent <AssetsBundleLoaderAsync>(); DontDestroyOnLoad(assetsBundleLoaderAsync.gameObject); assetBundle = await assetsBundleLoaderAsync.LoadAsync(p, actionProgress); Destroy(assetsBundleLoaderAsync.gameObject); if (assetBundle == null) { throw new Exception($"assets bundle not found: {p}"); } if (!assetBundle.isStreamedSceneAssetBundle && assetBundleName == "StreamingAssets") { await LoadAssetAsync(assetBundleName, assetBundle, actionProgress); } this.bundles[assetBundleName] = new ABInfo(assetBundleName, assetBundle); }