using System.Collections.Generic; using UnityEditor.AddressableAssets.Settings.GroupSchemas; using UnityEditor.Build.Pipeline.Utilities; using UnityEditor.Compilation; using UnityEditor.SceneManagement; using UnityEngine.SceneManagement; namespace UnityEditor.AddressableAssets.Build { /// /// Utility class for the Addressables Build Content process. /// public class BuildUtility { static HashSet s_EditorAssemblies = null; static HashSet editorAssemblies { get { if (s_EditorAssemblies == null) { s_EditorAssemblies = new HashSet(); foreach (var assembly in CompilationPipeline.GetAssemblies()) { if ((assembly.flags & AssemblyFlags.EditorAssembly) != 0) s_EditorAssemblies.Add(assembly.name); } } return s_EditorAssemblies; } } /// /// Determines if the given assembly is an editor assembly. /// /// The assembly. /// Returns true if the assembly is an editor assembly. Returns false otherwise. public static bool IsEditorAssembly(System.Reflection.Assembly assembly) { var splitName = assembly.FullName.Split(','); return splitName.Length > 0 && editorAssemblies.Contains(splitName[0]); } /// /// Creates a new bundle name using its hash and a given naming style. /// /// The bundle naming style. /// The bundle hash. /// The original bundle name. /// Returns the new bundle name. public static string GetNameWithHashNaming(BundledAssetGroupSchema.BundleNamingStyle schemaBundleNaming, string hash, string sourceBundleName) { string result = sourceBundleName; switch (schemaBundleNaming) { case BundledAssetGroupSchema.BundleNamingStyle.AppendHash: result = sourceBundleName.Replace(".bundle", "_" + hash + ".bundle"); break; case BundledAssetGroupSchema.BundleNamingStyle.NoHash: break; case BundledAssetGroupSchema.BundleNamingStyle.OnlyHash: result = hash + ".bundle"; break; case BundledAssetGroupSchema.BundleNamingStyle.FileNameHash: result = HashingMethods.Calculate(result) + ".bundle"; break; } return result; } /// /// Used during the build to check for unsaved scenes and provide a user popup if there are any. /// /// True if there were no unsaved scenes, or if user hits "Save and Continue" on popup. /// False if any scenes were unsaved, and user hits "Cancel" on popup. public static bool CheckModifiedScenesAndAskToSave() { var dirtyScenes = new List(); for (int i = 0; i < SceneManager.sceneCount; ++i) { var scene = SceneManager.GetSceneAt(i); if (scene.isDirty) { dirtyScenes.Add(scene); } } if (dirtyScenes.Count > 0) { if (EditorUtility.DisplayDialog( "Unsaved Scenes", "Modified Scenes must be saved to continue.", "Save and Continue", "Cancel")) { EditorSceneManager.SaveScenes(dirtyScenes.ToArray()); } else { return false; } } return true; } } }