private static void PackedAuto() { string uipath = Application.dataPath + "/UI/"; SpriteAtlasPackingSettings setting = new SpriteAtlasPackingSettings(); setting.enableRotation = false; setting.enableTightPacking = false; //SpriteAtlasTextureSettings texSetting = new SpriteAtlasTextureSettings(); TextureImporterPlatformSettings texPlatSetting = new TextureImporterPlatformSettings(); texPlatSetting.maxTextureSize = 2048; texPlatSetting.crunchedCompression = true; texPlatSetting.compressionQuality = 100; string[] dirs = Directory.GetDirectories(uipath); foreach (string dir in dirs) { string folderName = dir.Replace(uipath, ""); string sourcePath = dir.Replace(Application.dataPath, "Assets"); string[] guids = AssetDatabase.FindAssets("t:Sprite", new string[] { sourcePath }); if (guids.Length > 0) { List <Sprite> listSpite = new List <Sprite>(); foreach (string guid in guids) { Sprite s = AssetDatabase.LoadAssetAtPath <Sprite>(AssetDatabase.GUIDToAssetPath(guid)) as Sprite; listSpite.Add(s); } string dest = string.Format("Assets/UIAtlas/{0}.spriteatlas", folderName.ToLower()); SpriteAtlas atlas = AssetDatabase.LoadAssetAtPath <SpriteAtlas>(dest); if (atlas) { SpriteAtlasExtensions.Remove(atlas, atlas.GetPackables()); } else { atlas = new SpriteAtlas(); atlas.SetPackingSettings(setting); atlas.SetPlatformSettings(texPlatSetting); AssetDatabase.CreateAsset(atlas, dest); } SpriteAtlasExtensions.Add(atlas, listSpite.ToArray()); EditorUtility.SetDirty(atlas); } } EditorUtility.DisplayDialog("提示", "创建图集成功", "确认"); //ICON ATLAS 重icon collection來 //找 iconid to sprite collection /*string[] iconcollectons = AssetDatabase.FindAssets("t:IconIdToSpriteCollection", new string[]{ "Assets/MainAssets/IconCollection/Auto"}); * foreach (var collectionGuid in iconcollectons) * { * IconIdToSpriteCollection iconCollection = AssetDatabase.LoadAssetAtPath<IconIdToSpriteCollection>(AssetDatabase.GUIDToAssetPath(collectionGuid)); * * if (iconcollectons != null) * { * string dest = string.Format("Assets/MainAssets/ArtAssets/BundleAssets/IconCollectionAtlas/atlas_iconcollection_{0}.spriteatlas", iconCollection.name.ToLower()); * SpriteAtlas atlas = AssetDatabase.LoadAssetAtPath<SpriteAtlas>(dest); * if (atlas) * { * SpriteAtlasExtensions.Remove(atlas, atlas.GetPackables()); * } * else * { * atlas = new SpriteAtlas(); * atlas.SetPackingSettings(setting); * AssetDatabase.CreateAsset(atlas, dest); * } * * Object[] t = iconCollection.GetAllSprites().ToArray(); * SpriteAtlasExtensions.Add(atlas, t); * * * EditorUtility.SetDirty(atlas); * } * }*/ AssetDatabase.SaveAssets(); }
static void HandleSpriteAtlasFormat(HashSet <string> atlasFiles) { string[] atlasFileArray = new string[ATLAS_TYPE_NUMBER]; SpriteAtlas[] spriteAtlasArray = new SpriteAtlas[ATLAS_TYPE_NUMBER]; SpriteAtlasPackingSettings[] packingSettingsArray = new SpriteAtlasPackingSettings[ATLAS_TYPE_NUMBER]; SpriteAtlasTextureSettings[] textureSettingsArray = new SpriteAtlasTextureSettings[ATLAS_TYPE_NUMBER]; Dictionary <string, TextureImporterPlatformSettings>[] platformSettingsDictArray = new Dictionary <string, TextureImporterPlatformSettings> [ATLAS_TYPE_NUMBER]; platformSettingsDictArray[0] = new Dictionary <string, TextureImporterPlatformSettings>(); platformSettingsDictArray[1] = new Dictionary <string, TextureImporterPlatformSettings>(); foreach (string file in atlasFiles) { if (file.EndsWith(LOW_ATLAS_SUFFIX)) { continue; } atlasFileArray[0] = file; atlasFileArray[1] = file.Replace(EditorResourcePaths.ATLAS_SUFFIX, LOW_ATLAS_SUFFIX); for (int i = 0; i < ATLAS_TYPE_NUMBER; i++) { spriteAtlasArray[i] = AssetDatabase.LoadAssetAtPath <SpriteAtlas>(atlasFileArray[i]); packingSettingsArray[i] = SpriteAtlasExtensions.GetPackingSettings(spriteAtlasArray[i]); textureSettingsArray[i] = SpriteAtlasExtensions.GetTextureSettings(spriteAtlasArray[i]); foreach (string platform in PLATFORMS) { platformSettingsDictArray[i][platform] = SpriteAtlasExtensions.GetPlatformSettings(spriteAtlasArray[i], platform); } } HashSet <string> spriteSet = EditorResourcePaths.GetDependencies(atlasFileArray, EditorResourcePaths.PNG_REGEX); bool containAlpha = false; foreach (string spriteFile in spriteSet) { Texture2D texture = EditorCommonUtils.ReadTexture(spriteFile); if (ContainAlpha(texture)) { containAlpha = true; break; } } for (int i = 0; i < ATLAS_TYPE_NUMBER; i++) { packingSettingsArray[i].enableRotation = false; packingSettingsArray[i].enableTightPacking = false; SetDefaultTextureSettings(ref textureSettingsArray[i]); } if (containAlpha) { SetPlatformTextureSettings(platformSettingsDictArray[0]["Default"], "Default", TextureImporterFormat.RGBA32); SetPlatformTextureSettings(platformSettingsDictArray[0]["Standalone"], "Standalone", TextureImporterFormat.DXT5); SetPlatformTextureSettings(platformSettingsDictArray[0]["Android"], "Android", TextureImporterFormat.ETC2_RGBA8); SetPlatformTextureSettings(platformSettingsDictArray[0]["iPhone"], "iPhone", IOS_NEW_RGBA); SetPlatformTextureSettings(platformSettingsDictArray[1]["Default"], "Default", TextureImporterFormat.RGBA32); SetPlatformTextureSettings(platformSettingsDictArray[1]["Standalone"], "Standalone", TextureImporterFormat.DXT5Crunched); SetPlatformTextureSettings(platformSettingsDictArray[1]["Android"], "Android", TextureImporterFormat.ETC2_RGBA8Crunched); SetPlatformTextureSettings(platformSettingsDictArray[1]["iPhone"], "iPhone", TextureImporterFormat.PVRTC_RGBA4); } else { SetPlatformTextureSettings(platformSettingsDictArray[0]["Default"], "Default", TextureImporterFormat.RGB24); SetPlatformTextureSettings(platformSettingsDictArray[0]["Standalone"], "Standalone", TextureImporterFormat.DXT1); SetPlatformTextureSettings(platformSettingsDictArray[0]["Android"], "Android", TextureImporterFormat.ETC2_RGB4); SetPlatformTextureSettings(platformSettingsDictArray[0]["iPhone"], "iPhone", TextureImporterFormat.PVRTC_RGB4); SetPlatformTextureSettings(platformSettingsDictArray[1]["Default"], "Default", TextureImporterFormat.RGB24); SetPlatformTextureSettings(platformSettingsDictArray[1]["Standalone"], "Standalone", TextureImporterFormat.DXT1Crunched); SetPlatformTextureSettings(platformSettingsDictArray[1]["Android"], "Android", TextureImporterFormat.ETC_RGB4Crunched); SetPlatformTextureSettings(platformSettingsDictArray[1]["iPhone"], "iPhone", TextureImporterFormat.PVRTC_RGB4); } for (int i = 0; i < ATLAS_TYPE_NUMBER; i++) { SpriteAtlasExtensions.SetIncludeInBuild(spriteAtlasArray[i], false); SpriteAtlasExtensions.SetPackingSettings(spriteAtlasArray[i], packingSettingsArray[i]); SpriteAtlasExtensions.SetTextureSettings(spriteAtlasArray[i], textureSettingsArray[i]); foreach (string platform in PLATFORMS) { SpriteAtlasExtensions.SetPlatformSettings(spriteAtlasArray[i], platformSettingsDictArray[i][platform]); } } SpriteAtlasExtensions.SetIsVariant(spriteAtlasArray[1], false); SpriteAtlasExtensions.Remove(spriteAtlasArray[1], SpriteAtlasExtensions.GetPackables(spriteAtlasArray[1])); SpriteAtlasExtensions.Add(spriteAtlasArray[1], SpriteAtlasExtensions.GetPackables(spriteAtlasArray[0])); } AssetDatabase.Refresh(); }