示例#1
0
        public async ETTask 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);
                }

                abInfo        = new ABInfo(assetBundleName, null);
                abInfo.Parent = this;
                this.bundles[assetBundleName] = abInfo;
#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);
                }
            }

            abInfo        = new ABInfo(assetBundleName, assetBundle);
            abInfo.Parent = this;
            this.bundles[assetBundleName] = abInfo;
        }