// [UNITY-SKILL:SPRITEATLAS] using UnityEditor; using UnityEditor.U2D; using UnityEngine; /// /// Demonstrates setting includeInBuild for SpriteAtlases. /// Controls whether atlases are included in the build or loaded externally. /// public static class SetIncludeInBuildExample { /// /// Includes an atlas in the build (built-in data). /// Atlases with includeInBuild=true are embedded in the game build. /// public static void IncludeInBuild(string atlasPath) { SpriteAtlasImporter importer = AssetImporter.GetAtPath(atlasPath) as SpriteAtlasImporter; if (importer == null) return; importer.includeInBuild = true; importer.SaveAndReimport(); Debug.Log($"Included {atlasPath} in build"); } /// /// Excludes an atlas from the build (for Addressables or AssetBundles). /// Atlases with includeInBuild=false are not embedded and must be loaded separately. /// public static void ExcludeFromBuild(string atlasPath) { SpriteAtlasImporter importer = AssetImporter.GetAtPath(atlasPath) as SpriteAtlasImporter; if (importer == null) return; importer.includeInBuild = false; importer.SaveAndReimport(); Debug.Log($"Excluded {atlasPath} from build"); } /// /// Configures all atlases in a folder for built-in delivery. /// Use this when you want all atlases included in the game build. /// public static void IncludeAllInBuild(string folderPath) { string[] guids = AssetDatabase.FindAssets("t:SpriteAtlasAsset", new[] { folderPath }); foreach (string guid in guids) { string path = AssetDatabase.GUIDToAssetPath(guid); SpriteAtlasImporter importer = AssetImporter.GetAtPath(path) as SpriteAtlasImporter; if (importer != null) { importer.includeInBuild = true; importer.SaveAndReimport(); } } Debug.Log($"Configured {guids.Length} atlases for built-in delivery"); } /// /// Configures all atlases in a folder for Addressables delivery. /// Use this when using Addressables for late-binding atlas loading. /// public static void ExcludeAllForAddressables(string folderPath) { string[] guids = AssetDatabase.FindAssets("t:SpriteAtlasAsset", new[] { folderPath }); foreach (string guid in guids) { string path = AssetDatabase.GUIDToAssetPath(guid); SpriteAtlasImporter importer = AssetImporter.GetAtPath(path) as SpriteAtlasImporter; if (importer != null) { importer.includeInBuild = false; // Exclude from build for Addressables importer.SaveAndReimport(); } } Debug.Log($"Configured {guids.Length} atlases for Addressables delivery"); } /// /// Checks if an atlas is configured to be included in the build. /// public static bool IsIncludedInBuild(string atlasPath) { SpriteAtlasImporter importer = AssetImporter.GetAtPath(atlasPath) as SpriteAtlasImporter; if (importer == null) return false; return importer.includeInBuild; } /// /// Toggles the includeInBuild setting for an atlas. /// public static void ToggleIncludeInBuild(string atlasPath) { SpriteAtlasImporter importer = AssetImporter.GetAtPath(atlasPath) as SpriteAtlasImporter; if (importer == null) return; importer.includeInBuild = !importer.includeInBuild; importer.SaveAndReimport(); Debug.Log($"Set {atlasPath} includeInBuild to {importer.includeInBuild}"); } } /// /// Editor window for batch configuring includeInBuild settings. /// public class IncludeInBuildWindow : EditorWindow { private string _atlasFolder = "Assets/Atlases"; private bool _includeInBuild = true; [MenuItem("Tools/Configure Include In Build")] public static void ShowWindow() { GetWindow("Include In Build"); } private void OnGUI() { EditorGUILayout.LabelField("Batch Configure Include In Build", EditorStyles.boldLabel); _atlasFolder = EditorGUILayout.TextField("Atlas Folder", _atlasFolder); _includeInBuild = EditorGUILayout.Toggle("Include in Build", _includeInBuild); if (GUILayout.Button("Configure Atlases")) { BatchConfigure(); } } private void BatchConfigure() { string[] guids = AssetDatabase.FindAssets("t:SpriteAtlasAsset", new[] { _atlasFolder }); foreach (string guid in guids) { string path = AssetDatabase.GUIDToAssetPath(guid); SpriteAtlasImporter importer = AssetImporter.GetAtPath(path) as SpriteAtlasImporter; if (importer != null) { importer.includeInBuild = _includeInBuild; importer.SaveAndReimport(); } } Debug.Log($"Configured {guids.Length} atlases (includeInBuild={_includeInBuild})"); AssetDatabase.Refresh(); } }