Ejemplo n.º 1
0
        private void GetManifest(string bundleName, bool getFreshManifest, Action <AssetBundle> onComplete)
        {
            DownloadInProgressContainer inProgress;

            if (downloadsInProgress.TryGetValue(MANIFEST_DOWNLOAD_IN_PROGRESS_KEY, out inProgress))
            {
                inProgress.References++;
                inProgress.OnComplete += onComplete;
                return;
            }

            downloadsInProgress.Add(MANIFEST_DOWNLOAD_IN_PROGRESS_KEY, new DownloadInProgressContainer(onComplete));
            PrimaryManifest = PrimaryManifestType.Remote;

            uint manifestVersion = 1;

            if (getFreshManifest)
            {
                // Find the first cached version and then get the "next" one.
                manifestVersion = (uint)PlayerPrefs.GetInt(MANIFEST_PLAYERPREFS_KEY, 0) + 1;

                // The PlayerPrefs value may have been wiped so we have to calculate what the next uncached manifest version is.
                while (Caching.IsVersionCached(bundleName, new Hash128(0, 0, 0, manifestVersion)))
                {
                    manifestVersion++;
                }
            }

            GetManifestInternal(bundleName, manifestVersion, 0);
        }
        private void GetManifest(string bundleName, Action <AssetBundle> onComplete)
        {
            DownloadInProgressContainer inProgress;

            if (downloadsInProgress.TryGetValue(MANIFEST_DOWNLOAD_IN_PROGRESS_KEY, out inProgress))
            {
                inProgress.References++;
                inProgress.OnComplete += onComplete;
                return;
            }

            downloadsInProgress.Add(MANIFEST_DOWNLOAD_IN_PROGRESS_KEY, new DownloadInProgressContainer(onComplete));

#if !UNITY_EDITOR && !UNITY_WEBGL
            handler = new StreamingAssetsBundleDownloadDecorator(handler, defaultPrioritizationStrategy);
#else
            handler = new AssetBundleDownloader(baseUri);
#endif

            PrimaryManifest = PrimaryManifestType.Remote;

            // The first attempt for the manifest should always be uncached.  The PlayerPrefs value may have been wiped so we have to calculate what the next uncached manifest version is.
            var manifestVersion = (uint)PlayerPrefs.GetInt(MANIFEST_PLAYERPREFS_KEY, 0) + 1;
            while (Caching.IsVersionCached(bundleName, new Hash128(0, 0, 0, manifestVersion)))
            {
                manifestVersion++;
            }

            GetManifestInternal(bundleName, manifestVersion, 1);
        }
        private void GetManifestInternal(string bundleName, uint version, int attemptCount)
        {
            handler = new AssetBundleDownloader(baseUri);

            if (Application.isEditor == false)
            {
                handler = new StreamingAssetsBundleDownloadDecorator(handler, this.defaultPrioritizationStrategy);
            }

            handler.Handle(new AssetBundleDownloadCommand {
                BundleName = bundleName,
                Version    = version,
                OnComplete = manifest => {
                    if (manifest == null && attemptCount == 1 && version > 1)
                    {
                        PrimaryManifest = PrimaryManifestType.RemoteCached;
                        Debug.Log("Unable to download manifest, attempting to use one previously downloaded.");
                        GetManifestInternal(bundleName, version - 1, attemptCount + 1);
                    }
                    else
                    {
                        OnInitializationComplete(manifest, bundleName, version);
                    }
                }
            });
        }
        private void GetManifestInternal(string manifestName, uint version, int uriIndex)
        {
            handler = new AssetBundleDownloader(baseUri[uriIndex]);

            if (Application.isEditor == false)
            {
                handler = new StreamingAssetsBundleDownloadDecorator(manifestName, platformName, handler, defaultPrioritizationStrategy);
            }

            handler.Handle(new AssetBundleDownloadCommand {
                BundleName = manifestName,
                Version    = version,
                OnComplete = manifest => {
                    var maxIndex = baseUri.Length - 1;
                    if (manifest == null && uriIndex < maxIndex && version > 1)
                    {
                        Debug.LogFormat("Unable to download manifest from [{0}], attempting [{1}]", baseUri[uriIndex], baseUri[uriIndex + 1]);
                        GetManifestInternal(manifestName, version, uriIndex + 1);
                    }
                    else if (manifest == null && uriIndex >= maxIndex && version > 1 && PrimaryManifest != PrimaryManifestType.RemoteCached)
                    {
                        PrimaryManifest = PrimaryManifestType.RemoteCached;
                        Debug.LogFormat("Unable to download manifest, attempting to use one previously downloaded (version [{0}]).", version);
                        GetManifestInternal(manifestName, version - 1, uriIndex);
                    }
                    else
                    {
                        OnInitializationComplete(manifest, manifestName, version);
                    }
                }
            });
        }
Ejemplo n.º 5
0
        private void OnInitializationComplete(AssetBundle manifestBundle, string bundleName, uint version)
        {
            if (manifestBundle == null)
            {
                Debug.LogError("AssetBundleManifest not found.");

                var streamingAssetsDecorator = handler as StreamingAssetsBundleDownloadDecorator;
                if (streamingAssetsDecorator != null)
                {
                    PrimaryManifest = PrimaryManifestType.StreamingAssets;
                    Manifest        = streamingAssetsDecorator.GetManifest();

                    if (Manifest != null)
                    {
                        Debug.LogWarning("Falling back to streaming assets for bundle information.");
                    }
                }
            }
            else
            {
                Manifest = manifestBundle.LoadAsset <AssetBundleManifest>("assetbundlemanifest");
                PlayerPrefs.SetInt(MANIFEST_PLAYERPREFS_KEY, (int)version);

#if UNITY_2017_1_OR_NEWER
                Caching.ClearOtherCachedVersions(bundleName, new Hash128(0, 0, 0, version));
#endif
            }

            if (Manifest == null)
            {
                PrimaryManifest = PrimaryManifestType.None;
            }
            else
            {
                Initialized = true;

                if (useHash)
                {
                    GenerateNameHashMaps(Manifest);
                }
            }

            var inProgress = downloadsInProgress[MANIFEST_DOWNLOAD_IN_PROGRESS_KEY];
            downloadsInProgress.Remove(MANIFEST_DOWNLOAD_IN_PROGRESS_KEY);
            inProgress.OnComplete(manifestBundle);

            // Need to do this after OnComplete, otherwise the bundle will always be null
            if (manifestBundle != null)
            {
                manifestBundle.Unload(false);
            }
        }