/// <summary>
        ///     Caches the downloaded bundle and pushes it to the onComplete callback.
        /// </summary>
        private void OnDownloadComplete(string bundleName, AssetBundle bundle)
        {
            var inProgress = downloadsInProgress[bundleName];

            downloadsInProgress.Remove(bundleName);

            activeBundles.Add(bundleName, new AssetBundleContainer {
                AssetBundle  = bundle,
                References   = inProgress.References,
                Dependencies = Manifest.GetDirectDependencies(bundleName)
            });

            inProgress.OnComplete(bundle);
        }
예제 #2
0
        /// <summary>
        ///     Caches the downloaded bundle and pushes it to the onComplete callback.
        /// </summary>
        private void OnDownloadComplete(string bundleName, AssetBundle bundle)
        {
            var inProgress = downloadsInProgress[bundleName];

            downloadsInProgress.Remove(bundleName);

            try {
                activeBundles.Add(bundleName, new AssetBundleContainer {
                    AssetBundle  = bundle,
                    References   = inProgress.References,
                    Dependencies = Manifest.GetDirectDependencies(bundleName)
                });
            } catch (ArgumentException) {
                Debug.LogWarning("Attempted to activate a bundle that was already active.  Not sure how this happened, attempting to fail gracefully.");
                activeBundles[bundleName].References++;
            }

            inProgress.OnComplete(bundle);
        }
예제 #3
0
        /// <summary>
        ///     Downloads an AssetBundle or returns a cached AssetBundle if it has already been downloaded.
        ///     Remember to call <see cref="UnloadBundle(UnityEngine.AssetBundle,bool)" /> for every bundle you download once you
        ///     are done with it.
        /// </summary>
        /// <param name="bundleName">Name of the bundle to download.</param>
        /// <param name="onComplete">Action to perform when the bundle has been successfully downloaded.</param>
        /// <param name="downloadSettings">
        ///     Tell the function to use a previously downloaded version of the bundle if available.
        ///     Important!  If the bundle is currently "active" (it has not been unloaded) then the active bundle will be used
        ///     regardless of this setting.  If it's important that a new version is downloaded then be sure it isn't active.
        /// </param>
        public void GetBundle(string bundleName, Action <AssetBundle> onComplete, DownloadSettings downloadSettings)
        {
            if (Initialized == false)
            {
                Debug.LogError("AssetBundleManager must be initialized before you can get a bundle.");
                onComplete(null);
                return;
            }

            if (useHash)
            {
                bundleName = GetHashedBundleName(bundleName);
            }

            AssetBundleContainer active;

            if (activeBundles.TryGetValue(bundleName, out active))
            {
                active.References++;
                onComplete(active.AssetBundle);
                return;
            }

            DownloadInProgressContainer inProgress;

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

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

            var mainBundle = new AssetBundleDownloadCommand {
                BundleName = bundleName,
                Hash       = downloadSettings == DownloadSettings.UseCacheIfAvailable ? Manifest.GetAssetBundleHash(bundleName) : default(Hash128),
                OnComplete = bundle => OnDownloadComplete(bundleName, bundle)
            };

            var dependencies           = Manifest.GetDirectDependencies(bundleName);
            var dependenciesToDownload = new List <string>();

            for (int i = 0; i < dependencies.Length; i++)
            {
                if (activeBundles.TryGetValue(dependencies[i], out active))
                {
                    active.References++;
                }
                else
                {
                    dependenciesToDownload.Add(dependencies[i]);
                }
            }

            if (dependenciesToDownload.Count > 0)
            {
                var dependencyCount = dependenciesToDownload.Count;
                Action <AssetBundle> onDependenciesComplete = dependency => {
                    if (--dependencyCount == 0)
                    {
                        handler.Handle(mainBundle);
                    }
                };

                for (int i = 0; i < dependenciesToDownload.Count; i++)
                {
                    var dependencyName = dependenciesToDownload[i];
                    if (useHash)
                    {
                        dependencyName = GetUnhashedBundleName(dependencyName);
                    }
                    GetBundle(dependencyName, onDependenciesComplete);
                }
            }
            else
            {
                handler.Handle(mainBundle);
            }
        }