using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor; using System.Linq; using UnityEditor.Sprites; using System;
//判断图片是否为压缩格式 publicstaticboolIsCompressedFormat(TextureFormat fmt) { if (fmt >= TextureFormat.DXT1 && fmt <= TextureFormat.DXT5) returntrue; if (fmt >= TextureFormat.DXT1Crunched && fmt <= TextureFormat.DXT5Crunched) returntrue; if (fmt >= TextureFormat.PVRTC_RGB2 && fmt <= TextureFormat.PVRTC_RGBA4) returntrue; if (fmt == TextureFormat.ETC_RGB4) returntrue; if (fmt >= TextureFormat.ETC_RGB4 && fmt <= TextureFormat.ETC2_RGBA8) returntrue; if (fmt >= TextureFormat.EAC_R && fmt <= TextureFormat.EAC_RG_SIGNED) returntrue; if (fmt >= TextureFormat.ETC2_RGB && fmt <= TextureFormat.ETC2_RGBA8) returntrue; if (fmt >= TextureFormat.ASTC_4x4 && fmt <= TextureFormat.ASTC_HDR_12x12) returntrue; returnfalse; } publicintGetVersion() { thrownew System.NotImplementedException(); }
publicvoidOnGroupAtlases(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();
Resources.UnloadAsset(ti); } //将相同图集的名称划分成不同图集 var atlasGroups = from e in entries group e by e.atlasName; foreach (var atlasGroup in atlasGroups) { int page = 0; //相同的图集中压缩格式不同的图划分成不同的组 var settingGroups = from t in atlasGroup group t by t.settings; foreach (var settingGroup in settingGroups) { //已经设置了非压缩的图,不做成图集,避免图片变大 //非压缩的图有唯一的PackingTag并且不在CompressedFormat内,所以需要如此判断 bool skipAtlas = (settingGroup.Count() == 1 && !IsCompressedFormat(settingGroup.ElementAt(0).settings.format)); if (skipAtlas) continue;
string atlasName = atlasGroup.Key;
if (settingGroups.Count() > 1) atlasName += string.Format("(Group {0})", page);
AtlasSettings settings = settingGroup.Key; settings.anisoLevel = 1; if (settings.generateMipMaps) foreach (Entry entry in settingGroup) if (entry.anisoLevel > settings.anisoLevel) settings.anisoLevel = entry.anisoLevel;
角色、特效和场景等图片并不属于UI图片,如果美术人员并没有提供2的幂次方或者正方形图,就无法压缩了。在图片的导入面板——Advance下拉面板中选择合适的Non Power of 2(直译过来的意思是不是2的幂次方)可以强制设置图片为2的幂次方。其中ToNearest、ToLarger、ToSmaller表示自动将图片拉伸成对应的规则大小,这样图片就可以进行压缩了。