示例#1
0
        public async Task <AssetBundle> LoadAssetBundleAsync(string assetBundleName)
        {
            string p = Path.Combine(PathHelper.AppHotfixResPath, assetBundleName);

            if (!File.Exists(p))
            {
                p = Path.Combine(PathHelper.AppResPath, assetBundleName);
            }

            AssetBundle assetBundle = null;

            if (assetBundles.ContainsKey(assetBundleName))
            {
                assetBundle = assetBundles[assetBundleName];
            }
            else
            {
                using (AssetBundleLoaderAsync assetsBundleLoaderAsync = new AssetBundleLoaderAsync())
                {
                    assetBundle = await assetsBundleLoaderAsync.LoadAsync(p);
                }
            }

            if (assetBundle == null)
            {
                Log.Error($"assetBundle not found: {assetBundleName}");
                return(null);
            }

            return(assetBundle);
        }
示例#2
0
        public async Task <Object> LoadAssetAsync(string assetBundleName, string assetName)
        {
            if (!Define.IsAsync)
            {
                return(LoadEditorAsset(assetBundleName, assetName));
            }

            string p = Path.Combine(PathHelper.AppHotfixResPath, assetBundleName);

            if (!File.Exists(p))
            {
                p = Path.Combine(PathHelper.AppResPath, assetBundleName);
            }

            AssetBundle assetBundle = null;

            if (assetBundles.ContainsKey(assetBundleName))
            {
                if (!assets.ContainsKey(assetBundleName) || !assets[assetBundleName].ContainsKey(assetName))
                {
                    assetBundle = assetBundles[assetBundleName];
                }
            }
            else
            {
                using (AssetBundleLoaderAsync assetsBundleLoaderAsync = new AssetBundleLoaderAsync())
                {
                    assetBundle = await assetsBundleLoaderAsync.LoadAsync(p);
                }
            }

            if (assetBundle == null)
            {
                Log.Error($"assetBundle not found: {assetBundleName}");
                return(null);
            }

            assetBundles[assetBundleName] = assetBundle;

            if (!assetBundle.isStreamedSceneAssetBundle)
            {
                using (AssetLoaderAsync assetsLoaderAsync = new AssetLoaderAsync(assetBundle))
                {
                    var result = await assetsLoaderAsync.LoadAllAssetsAsync();

                    if (!assets.ContainsKey(assetBundleName))
                    {
                        assets[assetBundleName] = new Dictionary <string, Object>();
                    }
                    else
                    {
                        assets[assetBundleName].Clear();
                    }

                    if (result != null)
                    {
                        foreach (var asset in result)
                        {
                            if (asset)
                            {
                                assets[assetBundleName][asset.name] = asset;
                            }
                        }
                    }
                }
            }

            if (assets.ContainsKey(assetBundleName) && assets[assetBundleName].ContainsKey(assetName))
            {
                return(assets[assetBundleName][assetName]);
            }

            return(null);
        }