/// <summary>
        /// Creates a new <see cref="AssetBundleVariant"/>. Evaluates some conditions and may set <see cref="Errors"/>.
        /// </summary>
        /// <param name="assetBundleName">Name of the AssetBundle represented by this variant.</param>
        /// <param name="fileSizeBytes">Size of the file in bytes, or <see cref="FileSizeIfMissing"/>.</param>
        /// <param name="directDependencies">AssetBundles that are direct dependencies of this one.</param>
        /// <param name="allDependencies">AssetBundles that are direct or transitive dependencies of this one.</param>
        /// <param name="path">The path to the AssetBundle represented by this variant.</param>
        public static AssetBundleVariant CreateVariant(
            string assetBundleName,
            long fileSizeBytes,
            string[] directDependencies,
            string[] allDependencies,
            string path)
        {
            var variant = new AssetBundleVariant
            {
                DirectDependencies = directDependencies ?? EmptyStringArray,
                AllDependencies    = allDependencies ?? EmptyStringArray,
                FileSizeBytes      = fileSizeBytes,
                Path = path,
            };

            if (fileSizeBytes == FileSizeIfMissing)
            {
                variant.Errors.Add(AssetPackError.FileMissing);
            }

            if (!AndroidAppBundle.IsValidModuleName(assetBundleName))
            {
                variant.Errors.Add(AssetPackError.InvalidName);
            }

            return(variant);
        }
        private static void RenderVariant(AssetBundleVariant variant, TextureCompressionFormat targeting)
        {
            EditorGUILayout.BeginHorizontal();

            // TODO: revisit TextureCompressionFormat enum extension methods.
            var targetingDescription = targeting == TextureCompressionFormat.Default
                ? targeting.ToString()
                : targeting.ToString().ToUpper();

            EditorGUILayout.PrefixLabel(targetingDescription);

            EditorGUILayout.LabelField(variant.FileSizeText, GUILayout.ExpandWidth(false));

            var errors = variant.Errors;

            if (errors.Count == 0)
            {
                EditorGUILayout.LabelField("No errors", GUILayout.ExpandWidth(true));
            }
            else
            {
                if (GUILayout.Button(variant.ErrorSummary + "...", GUILayout.ExpandWidth(true)))
                {
                    var errorDialogTitle   = "AssetBundle Error" + (errors.Count == 1 ? string.Empty : "s");
                    var errorDialogMessage = string.Join("\n\n",
                                                         errors.Select(e => NameAndDescriptionAttribute.GetAttribute(e).Description).ToArray());
                    EditorUtility.DisplayDialog(errorDialogTitle, errorDialogMessage, WindowUtils.OkButtonText);
                }
            }

            EditorGUILayout.EndHorizontal();
        }
Beispiel #3
0
        /// <summary>
        /// Adds an <see cref="AssetBundleVariant"/> to this asset pack.
        /// <returns>true if the AssetBundle was added, false if another AssetBundle with the same compression
        /// is already present.</returns>
        /// </summary>
        public bool Add(TextureCompressionFormat textureCompressionFormat, AssetBundleVariant variant)
        {
            AssetBundleVariant existingVariant;

            if (Variants.TryGetValue(textureCompressionFormat, out existingVariant))
            {
                Debug.LogErrorFormat("Multiple AssetBundles for texture format {0} are used for {1}.",
                                     textureCompressionFormat.ToString(), Name);
                existingVariant.Errors.Add(AssetPackError.DuplicateName);
                return(false);
            }

            Variants.Add(textureCompressionFormat, variant);
            return(true);
        }
Beispiel #4
0
        /// <summary>
        /// Scans and returns every <see cref="AssetBundleVariant"/> found in this folder.
        /// </summary>
        public IDictionary <string, AssetBundleVariant> ExtractAssetBundleVariant()
        {
            AssetBundleCount = 0;
            var assetBundleVariants = new Dictionary <string, AssetBundleVariant>();

            if (!Refresh())
            {
                return(assetBundleVariants);
            }

            var directoryInfo = new DirectoryInfo(FolderPath);
            var files         = directoryInfo.GetFiles();

            var fileDictionary = files.ToDictionary(file => file.Name, file => file);

            // Look for an AssetBundle file with the same name as the directory that contains it.
            // This AssetBundle file contains a single asset, which is of type AssetBundleManifest.
            // See https://unity3d.com/learn/tutorials/topics/best-practices/assetbundle-fundamentals
            FileInfo manifestFileInfo;

            if (!fileDictionary.TryGetValue(_searchedManifestFileName, out manifestFileInfo))
            {
                State = AssetPackFolderState.ManifestFileMissing;
                return(assetBundleVariants);
            }

            var         manifestFilePath = manifestFileInfo.FullName;
            AssetBundle manifestAssetBundle;

            try
            {
                manifestAssetBundle = AssetBundle.LoadFromFile(manifestFilePath);
            }
            catch (Exception ex)
            {
                Debug.LogErrorFormat("Exception loading AssetBundle file containing manifest ({0}): {1}",
                                     manifestFilePath, ex);
                State = AssetPackFolderState.ManifestFileLoadError;
                return(assetBundleVariants);
            }

            try
            {
                var manifest = manifestAssetBundle.LoadAsset <AssetBundleManifest>("AssetBundleManifest");
                if (manifest == null)
                {
                    State = AssetPackFolderState.ManifestAssetMissing;
                    return(assetBundleVariants);
                }

                var allAssetBundles = manifest.GetAllAssetBundles();
                if (allAssetBundles.Length == 0)
                {
                    State = AssetPackFolderState.AssetBundlesMissing;
                    return(assetBundleVariants);
                }

                foreach (var assetBundleName in allAssetBundles)
                {
                    FileInfo assetBundleFileInfo;
                    var      fileSizeBytes = fileDictionary.TryGetValue(assetBundleName, out assetBundleFileInfo)
                        ? assetBundleFileInfo.Length
                        : AssetBundleVariant.FileSizeIfMissing;

                    assetBundleVariants.Add(
                        assetBundleName,
                        AssetBundleVariant.CreateVariant(
                            assetBundleName,
                            fileSizeBytes,
                            manifest.GetDirectDependencies(assetBundleName),
                            manifest.GetAllDependencies(assetBundleName),
                            Path.Combine(FolderPath, assetBundleName)));
                }

                AssetBundleCount = assetBundleVariants.Count;
                return(assetBundleVariants);
            }
            catch (Exception ex)
            {
                Debug.LogErrorFormat(
                    "Exception loading AssetBundleManifest from AssetBundle ({0}): {1}", manifestFilePath, ex);
                State = AssetPackFolderState.ManifestAssetLoadError;
                return(assetBundleVariants);
            }
            finally
            {
                // If an AssetBundle isn't unloaded, the Editor will have to be restarted to load it again.
                manifestAssetBundle.Unload(true);
            }
        }