using System.Collections.Generic; using System.IO; using Superlazy; using UnityEditor; using UnityEditor.U2D; using UnityEngine; using UnityEngine.U2D; public interface IPostProcessorSpriteAtlas { void OnAddSprite(SpriteAtlasAsset atlas); } public static class SLAssetPostprocessorSprite { private static readonly HashSet targetPaths = new HashSet(); public static void OnPreprocessTexture(TextureImporter importer) { importer.textureType = TextureImporterType.Sprite; importer.spriteImportMode = SpriteImportMode.Single; importer.sRGBTexture = true; importer.isReadable = false; importer.mipmapEnabled = false; importer.streamingMipmaps = false; importer.wrapMode = TextureWrapMode.Clamp; importer.filterMode = FilterMode.Bilinear; importer.textureCompression = TextureImporterCompression.Uncompressed; importer.crunchedCompression = false; importer.spritePixelsPerUnit = 100; var textureSettings = new TextureImporterSettings(); importer.ReadTextureSettings(textureSettings); textureSettings.spriteMeshType = SpriteMeshType.FullRect; textureSettings.spriteExtrude = 2; importer.SetTextureSettings(textureSettings); } public static void OnRemove(string path, string movePath) { var upperPath = path.ToUpper(); if (upperPath.Contains("ASSETS/RAW/SPRITES/") == false || upperPath.Contains(".PNG") == false) return; if (targetPaths.Contains(SLFileUtility.FolderPath(path)) == false) { targetPaths.Add(SLFileUtility.FolderPath(path)); } } public static void OnAdd(string path) { var upperPath = path.ToUpper(); if (upperPath.Contains("ASSETS/RAW/SPRITES/") == false || upperPath.Contains(".PNG") == false) return; if (targetPaths.Contains(SLFileUtility.FolderPath(path)) == false) { targetPaths.Add(SLFileUtility.FolderPath(path)); } } public static void CreateAtlas(string path, string destPath) { var oldAtlas = AssetDatabase.LoadAssetAtPath(destPath); if (oldAtlas != null) { AssetDatabase.DeleteAsset(destPath); } var di = new DirectoryInfo(path); if (di.Exists == false) return; var objects = new List(); foreach (var file in di.GetFiles()) { if (file.Name.ToUpper().IsRight(".PNG") == false) continue; var filePath = path + "/" + file.Name; var fileName = file.Name.Substring(0, file.Name.Length - ".png".Length); var sprite = AssetDatabase.LoadAssetAtPath(filePath); var maxSize = sprite.rect.size.x > sprite.rect.size.y ? sprite.rect.size.x : sprite.rect.size.y; if (maxSize > 1024) { CreateSingleAtlas(filePath, path.Replace("/Raw/", "/Addressables/") + $"_{fileName}.spriteatlasv2"); continue; } objects.Add(sprite); } if (objects.Count == 0) return; SLFileUtility.MakeFolderFromFilePath(destPath); var atlas = new SpriteAtlasAsset(); var spriteAtlasComponents = new List(); spriteAtlasComponents.CreateInstanceList(); foreach (var component in spriteAtlasComponents) { component.OnAddSprite(atlas); } atlas.Add(objects.ToArray()); SpriteAtlasAsset.Save(atlas, destPath); AssetDatabase.Refresh(); var sai = (SpriteAtlasImporter)AssetImporter.GetAtPath(destPath); sai.packingSettings = new SpriteAtlasPackingSettings { enableRotation = false, enableTightPacking = false, enableAlphaDilation = false, padding = 4, blockOffset = 0 }; sai.textureSettings = new SpriteAtlasTextureSettings { filterMode = FilterMode.Bilinear, sRGB = true, generateMipMaps = false }; } public static void CreateSingleAtlas(string path, string destPath) { var oldAtlas = AssetDatabase.LoadAssetAtPath(destPath); if (oldAtlas != null) { AssetDatabase.DeleteAsset(destPath); } SLFileUtility.MakeFolderFromFilePath(destPath); var atlas = new SpriteAtlasAsset(); var sprite = AssetDatabase.LoadAssetAtPath(path); atlas.Add(new Object[] { sprite }); var spriteAtlasComponents = new List(); spriteAtlasComponents.CreateInstanceList(); foreach (var component in spriteAtlasComponents) { component.OnAddSprite(atlas); } SpriteAtlasAsset.Save(atlas, destPath); AssetDatabase.Refresh(); var sai = (SpriteAtlasImporter)AssetImporter.GetAtPath(destPath); sai.packingSettings = new SpriteAtlasPackingSettings { enableRotation = false, enableTightPacking = false, enableAlphaDilation = false, padding = 4, blockOffset = 0 }; sai.textureSettings = new SpriteAtlasTextureSettings { filterMode = FilterMode.Bilinear, sRGB = true, generateMipMaps = false }; } public static void BuildTarget() { foreach (var path in targetPaths) { CreateAtlas(path, path.Replace("/Raw/", "/Addressables/") + ".spriteatlasv2"); } targetPaths.Clear(); } }