public virtual void OnGroupAtlases(BuildTarget target, PackerJob job, int[] textureImporterInstanceIDs) { atlasDict.Clear(); foreach (int instanceId in textureImporterInstanceIDs) { TextureImporter ti = EditorUtility.InstanceIDToObject(instanceId) as TextureImporter; if (ti == true) { ti.mipmapEnabled = false; AssetDatabase.ImportAsset(ti.assetPath); } if (!atlasDict.ContainsKey(ti.spritePackingTag)) { atlasDict[ti.spritePackingTag] = new List <string>(); } atlasDict[ti.spritePackingTag].Add(ti.assetPath); } foreach (string atlasName in atlasDict.Keys) { job.AddAtlas(atlasName, GetAtlasSetting(target)); foreach (string path in atlasDict[atlasName]) { Sprite sprite = AssetDatabase.LoadAssetAtPath <Sprite>(path); if (sprite == null) { continue; } job.AssignToAtlas(atlasName, sprite, SpritePackingMode.Rectangle, SpritePackingRotation.None); } } }
public void OnGroupAtlases(BuildTarget target, PackerJob job, int[] textureImporterInstanceIDs) { List <Entry> entries = new List <Entry>(); foreach (int instanceID in textureImporterInstanceIDs) { TextureImporter ti = EditorUtility.InstanceIDToObject(instanceID) as TextureImporter; TextureFormat textureFormat; ColorSpace colorSpace; int compressionQuality; ti.ReadTextureImportInstructions(target, out textureFormat, out colorSpace, out compressionQuality); TextureImporterSettings tis = new TextureImporterSettings(); ti.ReadTextureSettings(tis); Sprite[] sprites = AssetDatabase.LoadAllAssetRepresentationsAtPath(ti.assetPath).Select(x => x as Sprite).Where(x => x != null).ToArray(); foreach (Sprite sprite in sprites) { //在这里设置每个图集的参数 Entry entry = new Entry(); entry.sprite = sprite; entry.settings.format = textureFormat; entry.settings.colorSpace = colorSpace; entry.settings.compressionQuality = compressionQuality; entry.settings.filterMode = Enum.IsDefined(typeof(FilterMode), ti.filterMode) ? ti.filterMode : FilterMode.Bilinear; entry.settings.maxWidth = ti.maxTextureSize; entry.settings.maxHeight = ti.maxTextureSize; entry.atlasName = ParseAtlasName(ti.spritePackingTag); entry.packingMode = GetPackingMode(ti.spritePackingTag, tis.spriteMeshType); entries.Add(entry); } Resources.UnloadAsset(ti); } // First split sprites into groups based on atlas name var atlasGroups = from e in entries group e by e.atlasName; foreach (var atlasGroup in atlasGroups) { int page = 0; // Then split those groups into smaller groups based on texture settings var settingsGroups = from t in atlasGroup group t by t.settings; foreach (var settingsGroup in settingsGroups) { string atlasName = atlasGroup.Key; if (settingsGroups.Count() > 1) { atlasName += string.Format(" (Group {0})", page); } job.AddAtlas(atlasName, settingsGroup.Key); foreach (Entry entry in settingsGroup) { job.AssignToAtlas(atlasName, entry.sprite, entry.packingMode, SpritePackingRotation.None); } ++page; } } }
} // end IsCompressedFormat public void OnGroupAtlases(BuildTarget target, PackerJob job, int[] textureImporterInstanceIDs) { List <Entry> entries = new List <Entry>(); foreach (int instanceID in textureImporterInstanceIDs) { TextureImporter ti = EditorUtility.InstanceIDToObject(instanceID) as TextureImporter; TextureFormat desiredFormat; ColorSpace colorSpace; int compressionQuality; ti.ReadTextureImportInstructions(target, out desiredFormat, out colorSpace, out compressionQuality); TextureImporterSettings tis = new TextureImporterSettings(); ti.ReadTextureSettings(tis); Sprite[] sprites = AssetDatabase.LoadAllAssetRepresentationsAtPath(ti.assetPath) .Select(x => x as Sprite) .Where(x => x != null) .ToArray(); foreach (Sprite sprite in sprites) { Entry entry = new Entry(); entry.sprite = sprite; entry.settings.format = desiredFormat; entry.settings.colorSpace = colorSpace; // Use Compression Quality for Grouping later only for Compressed Formats. Otherwise leave it Empty. entry.settings.compressionQuality = IsCompressedFormat(desiredFormat) ? compressionQuality : 0; entry.settings.filterMode = Enum.IsDefined(typeof(FilterMode), ti.filterMode) ? ti.filterMode : FilterMode.Bilinear; entry.settings.maxWidth = 1024; entry.settings.maxHeight = 1024; entry.settings.generateMipMaps = ti.mipmapEnabled; entry.settings.enableRotation = AllowRotationFlipping; if (ti.mipmapEnabled) { entry.settings.paddingPower = kDefaultPaddingPower; } else { entry.settings.paddingPower = (uint)EditorSettings.spritePackerPaddingPower; } // end if #if ENABLE_ANDROID_ATLAS_ETC1_COMPRESSION entry.settings.allowsAlphaSplitting = ti.GetAllowsAlphaSplitting(); #endif //ENABLE_ANDROID_ATLAS_ETC1_COMPRESSION entry.atlasName = ParseAtlasName(ti.spritePackingTag); entry.packingMode = GetPackingMode(ti.spritePackingTag, tis.spriteMeshType); entry.anisoLevel = ti.anisoLevel; entries.Add(entry); } // end foreach Resources.UnloadAsset(ti); } // end foreach // First split sprites into groups based on atlas name var atlasGroups = from e in entries group e by e.atlasName; foreach (var atlasGroup in atlasGroups) { int page = 0; // Then split those groups into smaller groups based on texture settings var settingsGroups = from t in atlasGroup group t by t.settings; foreach (var settingsGroup in settingsGroups) { string atlasName = atlasGroup.Key; if (settingsGroups.Count() > 1) { atlasName += string.Format(" (Group {0})", page); } // end if AtlasSettings settings = settingsGroup.Key; settings.anisoLevel = 1; // Use the highest aniso level from all entries in this atlas if (settings.generateMipMaps) { foreach (Entry entry in settingsGroup) { if (entry.anisoLevel > settings.anisoLevel) { settings.anisoLevel = entry.anisoLevel; } // end if } // end foreach } // end if job.AddAtlas(atlasName, settings); foreach (Entry entry in settingsGroup) { job.AssignToAtlas(atlasName, entry.sprite, entry.packingMode, SpritePackingRotation.None); } // end foreach ++page; } // end foreach } // end foreach } // end OnGroupAtlases
public void OnGroupAtlases(BuildTarget target, PackerJob job, int[] textureImporterInstanceIDs) { List <Entry> entries = new List <Entry>(); foreach (int instanceID in textureImporterInstanceIDs) { TextureImporter ti = EditorUtility.InstanceIDToObject(instanceID) as TextureImporter; TextureFormat textureFormat; ColorSpace colorSpace; int compressionQuality; ti.ReadTextureImportInstructions(target, out textureFormat, out colorSpace, out compressionQuality); TextureImporterSettings tis = new TextureImporterSettings(); ti.ReadTextureSettings(tis); Sprite[] sprites = AssetDatabase.LoadAllAssetRepresentationsAtPath(ti.assetPath).Select(x => x as Sprite).Where(x => x != null).ToArray(); foreach (Sprite sprite in sprites) { Entry entry = new Entry(); entry.sprite = sprite; entry.settings.format = textureFormat; entry.settings.colorSpace = colorSpace; entry.settings.compressionQuality = compressionQuality; entry.settings.filterMode = Enum.IsDefined(typeof(FilterMode), ti.filterMode) ? ti.filterMode : FilterMode.Bilinear; entry.settings.maxWidth = 2048; entry.settings.maxHeight = 2048; entry.settings.generateMipMaps = ti.mipmapEnabled; if (ti.mipmapEnabled) { entry.settings.paddingPower = kDefaultPaddingPower; } entry.atlasName = ParseAtlasName(ti.spritePackingTag); entry.packingMode = GetPackingMode(ti.spritePackingTag, tis.spriteMeshType); entry.anisoLevel = ti.anisoLevel; if (sprite.texture.width > 2048 || sprite.texture.height > 2048) { Debug.LogWarningFormat("Unable to pack texture '{0}': size {1}x{2} exceeds maximum 1024x1024", sprite.texture.name, sprite.texture.width, sprite.texture.height); } else { entries.Add(entry); } } Resources.UnloadAsset(ti); } // First split sprites into groups based on atlas name var atlasGroups = from e in entries group e by e.atlasName; foreach (var atlasGroup in atlasGroups) { int page = 0; // Then split those groups into smaller groups based on texture settings var settingsGroups = from t in atlasGroup group t by t.settings; foreach (var settingsGroup in settingsGroups) { string atlasName = atlasGroup.Key; if (settingsGroups.Count() > 1) { atlasName += string.Format(" (Group {0})", page); } AtlasSettings settings = settingsGroup.Key; settings.anisoLevel = 1; // Use the highest aniso level from all entries in this atlas if (settings.generateMipMaps) { foreach (Entry entry in settingsGroup) { if (entry.anisoLevel > settings.anisoLevel) { settings.anisoLevel = entry.anisoLevel; } } } job.AddAtlas(atlasName, settings); foreach (Entry entry in settingsGroup) { job.AssignToAtlas(atlasName, entry.sprite, entry.packingMode, SpritePackingRotation.None); } ++page; } } }
public void OnGroupAtlases(BuildTarget target, PackerJob job, int[] textureImporterInstanceIDs) { List<Entry> entries = new List<Entry>(); foreach (int instanceID in textureImporterInstanceIDs) { TextureImporter ti = EditorUtility.InstanceIDToObject(instanceID) as TextureImporter; var texture = AssetDatabase.LoadAssetAtPath(ti.assetPath, typeof(Texture2D)) as Texture2D; int paddingPower = ComputePaddingPower(texture); #if UNITY_4_6 || UNITY_4_7 TextureImportInstructions ins = new TextureImportInstructions(); ti.ReadTextureImportInstructions(ins, target); #else // Unity 5 ColorSpace colorSpace; int compression; TextureFormat format; ti.ReadTextureImportInstructions(target, out format, out colorSpace, out compression); TextureImporterSettings tis = new TextureImporterSettings(); ti.ReadTextureSettings(tis); #endif Sprite[] sprites = AssetDatabase.LoadAllAssetRepresentationsAtPath(ti.assetPath).Select(x => x as Sprite).Where(x => x != null).ToArray(); foreach (Sprite sprite in sprites) { Entry entry = new Entry(); entry.sprite = sprite; #if UNITY_4_6 || UNITY_4_7 entry.settings.format = ins.desiredFormat; entry.settings.usageMode = TextureUsageMode.Default; entry.settings.colorSpace = ins.colorSpace; entry.settings.compressionQuality = ins.compressionQuality; #else // Unity 5 entry.settings.format = format; entry.settings.colorSpace = colorSpace; entry.settings.compressionQuality = compression; #endif entry.settings.filterMode = Enum.IsDefined(typeof(FilterMode), ti.filterMode) ? ti.filterMode : FilterMode.Bilinear; entry.settings.maxWidth = 2048; entry.settings.maxHeight = 2048; entry.settings.generateMipMaps = ti.mipmapEnabled; //if (ti.mipmapEnabled) entry.paddingPower = (uint)paddingPower; // using padding power group as a hack to group sprites with similar padding power entry.settings.paddingPower = PaddingPowerGroup(paddingPower); Debug.Log("Groupping " + paddingPower + " to " + entry.settings.paddingPower); entry.atlasName = ParseAtlasName(ti.spritePackingTag); entry.packingMode = SpritePackingMode.Rectangle; entry.anisoLevel = ti.anisoLevel; entries.Add(entry); } Resources.UnloadAsset(ti); Resources.UnloadAsset(texture); } // First split sprites into groups based on atlas name var atlasGroups = from e in entries group e by e.atlasName; foreach (var atlasGroup in atlasGroups) { int page = 0; // Then split those groups into smaller groups based on texture settings var settingsGroup = from t in atlasGroup group t by t.settings; foreach (var settingGroup in settingsGroup) { string atlasName = atlasGroup.Key; if (page > 0) atlasName += string.Format(" (Group {0})", page); AtlasSettings settings = settingGroup.Key; settings.anisoLevel = 1; // Use the highest aniso level from all entries in this atlas if (settings.generateMipMaps) foreach (Entry entry in settingGroup) if (entry.anisoLevel > settings.anisoLevel) settings.anisoLevel = entry.anisoLevel; // apply now the padding power because grouping would fail earlier uint biggest = BiggestPadding(settingGroup); settings.paddingPower = biggest; job.AddAtlas(atlasName, settings); foreach (Entry entry in settingGroup) { job.AssignToAtlas(atlasName, entry.sprite, SpritePackingMode.Rectangle, SpritePackingRotation.None); } ++page; } } }