Example #1
0
        public async Task LoadOneBundleAsync(string 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 (!Define.IsAsync)
            {
#if UNITY_EDITOR
                string[] 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);
                return;
#endif
            }

            AssetBundle assetBundle;
            using (AssetsBundleLoaderAsync assetsBundleLoaderAsync = ComponentFactory.Create <AssetsBundleLoaderAsync>())
            {
                assetBundle = await assetsBundleLoaderAsync.LoadAsync(assetBundleName);
            }

            if (!assetBundle.isStreamedSceneAssetBundle)
            {
                // 异步load资源到内存cache住
                UnityEngine.Object[] assets;
                using (AssetsLoaderAsync assetsLoaderAsync = ComponentFactory.Create <AssetsLoaderAsync, AssetBundle>(assetBundle))
                {
                    assets = await assetsLoaderAsync.LoadAllAssetsAsync();
                }
                foreach (UnityEngine.Object asset in assets)
                {
                    string path = $"{assetBundleName}/{asset.name}".ToLower();
                    this.resourceCache[path] = asset;
                }
            }

            this.bundles[assetBundleName] = new ABInfo(assetBundleName, assetBundle);
        }
Example #2
0
        public async Task LoadOneBundleAsync(string assetBundleName)
        {
            ABInfo abInfo;

            if (this.bundles.TryGetValue(assetBundleName, out abInfo))
            {
                ++abInfo.RefCount;
                return;
            }

            //Log.Debug($"---------------load one bundle {assetBundleName}");
            if (!Define.IsAsync)
            {
                string[] realPath = null;
#if UNITY_EDITOR
                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);
                }

                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);
            }

            using (AssetsBundleLoaderAsync assetsBundleLoaderAsync = ComponentFactory.Create <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();
                }
                foreach (UnityEngine.Object asset in assets)
                {
                    AddResource(assetBundleName, asset.name, asset);
                }
            }

            this.bundles[assetBundleName] = new ABInfo(assetBundleName, assetBundle);
        }
Example #3
0
        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 (!Define.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);
            }

            using (AssetsBundleLoaderAsync assetsBundleLoaderAsync = ComponentFactory.Create <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);
        }