Exemple #1
0
        /// <summary>
        /// Attemps to load requested asset. Depending on the project options it will look the asset
        /// on the web, on the streaming assets folder or it will attempt to simulate it by loading
        /// it from the asset database. Asset simulation is only available on editor.
        /// </summary>
        /// <param name="bundleRequest"> Bundle to request </param>
        /// <param name="forceLoadFromStreamingAssets">Forces loading from StreamingAssets folder. Useful for when including assets with the build</param>
        /// <param name="progress"></param>
        /// <param name="cancellationToken"></param>
        /// <returns> Observable </returns>
        internal async Task <T> LoadAsset <T>(BundleRequest bundleRequest, bool forceLoadFromStreamingAssets,
                                              IProgress <float> progress = null, CancellationToken cancellationToken = default(CancellationToken)) where T : UnityEngine.Object
        {
            var bundle = await LoadBundle(bundleRequest, forceLoadFromStreamingAssets, progress, cancellationToken);

            return(await bundle.LoadAssetAsync <T>(bundleRequest.AssetName, progress, cancellationToken));
        }
Exemple #2
0
        internal async Task <UnityEngine.Object> LoadScene(BundleRequest bundleRequest, bool forceLoadFromStreamingAssets = false,
                                                           IProgress <float> progress = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            var bundle = await LoadBundle(bundleRequest, forceLoadFromStreamingAssets, progress, cancellationToken);

            return(bundle.Bundle.GetAllScenePaths().Length > 0 ? bundle.Bundle : null);
        }
        /// <summary>
        /// Gets and loads the required asset bundle
        /// </summary>
        /// <param name="assetCatRoot"></param>
        /// <param name="assetName">Bundle name and asset name are the same</param>
        /// <param name="forceLoadFromStreamingAssets">
        /// Forces loading from StreamingAssets folder. Useful for when including assets
        /// with the build
        /// </param>
        /// <param name="progress"></param>
        /// <param name="cancellationToken"></param>
        /// <returns> Observable </returns>
        public async UniTask <T> LoadAsset <T>(AssetCategoryRoot assetCatRoot, string assetName, bool forceLoadFromStreamingAssets = false,
                                               IProgress <float> progress = null, CancellationToken cancellationToken = default(CancellationToken)) where T : UnityEngine.Object
        {
            var bundleNeeded = new BundleRequest(assetCatRoot,
                                                 assetName, assetName);

            return(await LoadAsset <T>(bundleNeeded, forceLoadFromStreamingAssets, progress, cancellationToken));
        }
        internal async UniTask <LoadedBundle> LoadBundle(BundleRequest bundleRequest, bool forceLoadFromStreamingAssets,
                                                         IProgress <float> progress = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (!_loadedBundles.ContainsKey(bundleRequest.BundleName))
            {
#if UNITY_EDITOR
                if (EditorPreferences.EditorprefSimulateAssetBundles)
                {
                    var assPath = bundleRequest.AssetCategory.ToString().ToLower() + "/" + bundleRequest.BundleName;
                    var paths   = AssetDatabase.GetAssetPathsFromAssetBundle(assPath);

                    var theone = string.Empty;
                    foreach (var path in paths)
                    {
                        if (cancellationToken.IsCancellationRequested)
                        {
                            return(null);
                        }

                        if (path.ToLower().Contains(bundleRequest.AssetName))
                        {
                            theone = path;
                            break;
                        }
                    }

                    Object asset;

                    if (bundleRequest.AssetCategory == AssetCategoryRoot.Scenes)
                    {
                        asset = AssetDatabase.LoadAssetAtPath <Object>(theone);
                        _loadedBundles.Add(bundleRequest.BundleName, new LoadedBundle(asset as SceneAsset));
                    }
                    else
                    {
                        asset = AssetDatabase.LoadAssetAtPath <Object>(theone);
                        _loadedBundles.Add(bundleRequest.BundleName, new LoadedBundle(asset as GameObject));
                    }

                    return(_loadedBundles[bundleRequest.BundleName]);
                }
#endif

                AssetBundle bundle;
                if (_assetService.UseStreamingAssets || forceLoadFromStreamingAssets)
                {
                    bundle = await GetBundleFromStreamingAssetsAsync(bundleRequest, progress, cancellationToken);
                }
                else
                {
                    bundle = await GetBundleFromWebOrCacheAsync(bundleRequest, progress, cancellationToken);
                }

                _loadedBundles.Add(bundleRequest.BundleName, new LoadedBundle(bundle));
            }

            return(_loadedBundles[bundleRequest.BundleName]);
        }
Exemple #5
0
        /// <summary>
        /// Method attemps to get a bundle from the web/cloud
        /// </summary>
        /// <param name="bundleRequest">Bundle to request</param>
        /// <param name="progress"></param>
        /// <param name="cancellationToken"></param>
        private async Task <AssetBundle> GetBundleFromWebOrCacheAsync(BundleRequest bundleRequest,
                                                                      IProgress <float> progress, CancellationToken cancellationToken)
        {
            var uwr = new UnityWebRequest();

            Debug.Log($"AssetBundleLoader:  {_assetService.AssetCacheState}  | Requesting:  {bundleRequest.AssetName}  {bundleRequest.BundleName}".Colored(Colors.Aqua));
            if (_assetService.CloudBuildManifest != null && _assetService.AssetCacheState == AssetCacheState.Cache)
            {
                //cache bundles by using Unity Cloud Build manifest
                uint buildNumber = 0;
                buildNumber = Convert.ToUInt32(_assetService.CloudBuildManifest.buildNumber);
                uwr         = UnityWebRequestAssetBundle.GetAssetBundle(bundleRequest.GetAssetPath(_assetService.Configuration), buildNumber, 0);
            }
            else if (_assetService.CloudBuildManifest == null || _assetService.AssetCacheState == AssetCacheState.NoCache)
            {
                if (_assetService.AssetCacheState == AssetCacheState.Cache)
                {
                    Debug.Log("AssetBundleLoader:  Caching is enabled, but Unity Cloud Build Manifest was missing, bundle was not cached.".Colored(Colors.Aqua));
                }

                //No caching, just get the bundle
                uwr = UnityWebRequestAssetBundle.GetAssetBundle(bundleRequest.GetAssetPath(_assetService.Configuration));
            }

            //Wait until uwr is done.
            var asyncOperation = uwr.SendWebRequest();

            while (!asyncOperation.isDone)
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    return(null);
                }

                await UniTask.Yield(cancellationToken : cancellationToken);

                progress?.Report(asyncOperation.progress);
                Debug.Log($"GetBundleFromWebOrCacheAsync {bundleRequest.BundleName} progress: {asyncOperation.progress * 100f}%".Colored(Colors.LightSalmon));
            }

            //get bundle
            var bundle = DownloadHandlerAssetBundle.GetContent(uwr);

            if (uwr.isNetworkError)
            {
                throw new System.Exception($"AssetBundleLoader:  {uwr.error}");
            }

            uwr.Dispose();

            return(bundle);
        }
        internal async UniTask <UnityEngine.Object> LoadScene(BundleRequest bundleRequest, bool forceLoadFromStreamingAssets = false,
                                                              IProgress <float> progress = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            var bundle = await LoadBundle(bundleRequest, forceLoadFromStreamingAssets, progress, cancellationToken);

#if UNITY_EDITOR
            if (EditorPreferences.EditorprefSimulateAssetBundles)
            {
                return(bundle.SceneAsset);
            }
#endif
            if (bundle.Bundle.GetAllScenePaths().Length == 0)
            {
                throw new Exception($"AssetBundleLoader: Tried to load a scene from {bundleRequest.BundleName}, and this bundle does not contains a scene.");
            }

            return(bundle.Bundle); //.GetAllScenePaths().Length > 0 ? bundle.Bundle : null;
        }
Exemple #7
0
        /// <summary>
        /// Gets bundle from streaming assets directory
        /// </summary>
        /// <param name="bundleRequest"> Bundle to request </param>
        /// <param name="progress"></param>
        /// <param name="cancellationToken"></param>
        private async Task <AssetBundle> GetBundleFromStreamingAssetsAsync(BundleRequest bundleRequest,
                                                                           IProgress <float> progress, CancellationToken cancellationToken)
        {
            Debug.Log($"AssetBundleLoader: Using StreamingAssets -  Requesting: {bundleRequest.AssetCategory}  {bundleRequest.BundleName}".Colored(Colors.Aqua));
            var path = Path.Combine(Application.streamingAssetsPath, bundleRequest.AssetPathFromLocalStreamingAssets);

            var asyncOperation = AssetBundle.LoadFromFileAsync(path);

            while (!asyncOperation.isDone)
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    return(null);
                }

                await UniTask.Yield(cancellationToken : cancellationToken);

                progress?.Report(asyncOperation.progress);
                Debug.Log($"GetBundleFromStreamingAssetsAsync {bundleRequest.BundleName} progress: {asyncOperation.progress * 100f}%".Colored(Colors.LightSalmon));
            }

            return(asyncOperation.assetBundle);
        }
 private async UniTask <T> LoadAsset <T>(BundleRequest bundleRequest, bool forceLoadFromStreamingAssets = false,
                                         IProgress <float> progress = null, CancellationToken cancellationToken = default(CancellationToken)) where T : UnityEngine.Object
 {
     return(await _assetBundlebundleLoader.LoadAsset <T>(bundleRequest, forceLoadFromStreamingAssets, progress, cancellationToken));
 }
 public async UniTask <UnityEngine.Object> GetScene(BundleRequest bundleRequest, bool forceLoadFromStreamingAssets = false,
                                                    IProgress <float> progress = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     return(await _assetBundlebundleLoader.LoadScene(bundleRequest, forceLoadFromStreamingAssets, progress, cancellationToken));
 }