public bool AreAllSpritesOnSameTexture() { Texture2D mainTex = null; foreach (var dataObj in featureData) { if (dataObj.sprite == null) { continue; } string atlasName; Texture2D atlasTex; Packer.GetAtlasDataForSprite(dataObj.sprite, out atlasName, out atlasTex); if (mainTex == null) { mainTex = atlasTex; } else { if (mainTex != atlasTex) { return(false); } } } return(true); }
public static void PackAlphaAltasToAssetBoundles() { EditorSettings.spritePackerMode = SpritePackerMode.AlwaysOn; Packer.SelectedPolicy = typeof(CustomPackerPolicy).Name; CustomPackerPolicy.forceIOSOpaque = true; Packer.RebuildAtlasCacheIfNeeded(BuildTarget.iOS, true, Packer.Execution.ForceRegroup); List <UnityEngine.Object> objects = new List <UnityEngine.Object>(); foreach (string path in AssetDatabase.FindAssets("t:sprite")) { objects.AddRange(AssetDatabase.LoadAllAssetRepresentationsAtPath(AssetDatabase.GUIDToAssetPath(path))); } Sprite[] sprites = objects.Distinct() .Select(x => x as Sprite) .Where(x => x != null && x.packed) .ToArray(); List <SerializedObject> sos = new List <SerializedObject>(); foreach (Sprite sprite in sprites) { Texture2D atlasTexture; string atlasName; Packer.GetAtlasDataForSprite(sprite, out atlasName, out atlasTexture); if (atlasTexture != null) { SerializedObject so = new SerializedObject(sprite); so.FindProperty("m_RD.textureRect").rectValue = GetAltasTextureRect(sprite, atlasTexture); so.FindProperty("m_RD.texture").objectReferenceValue = atlasTexture; Texture2D alphaTexture = AssetDatabase.LoadAssetAtPath <Texture2D>("Assets/" + AlphaAtlasManager.TEXTURE_ALPHA_ATLAS_PATH + atlasTexture.name + "_alpha.png"); so.FindProperty("m_RD.alphaTexture").objectReferenceValue = alphaTexture; so.ApplyModifiedProperties(); sos.Add(so); } } EditorSettings.spritePackerMode = SpritePackerMode.Disabled; BuildPipeline.BuildAssetBundles(Application.dataPath + "/AssetBundles", BuildAssetBundleOptions.ChunkBasedCompression, BuildTarget.iOS); EditorSettings.spritePackerMode = SpritePackerMode.AlwaysOn; foreach (SerializedObject so in sos) { Sprite sprite = so.targetObject as Sprite; so.FindProperty("m_RD.textureRect").rectValue = sprite.textureRect; so.FindProperty("m_RD.texture").objectReferenceValue = SpriteUtility.GetSpriteTexture(sprite, false); so.FindProperty("m_RD.alphaTexture").objectReferenceValue = null; so.ApplyModifiedProperties(); } AssetDatabase.Refresh(); }
void OnSelectionChange() { if (Selection.activeObject == null) { return; } Sprite selectedSprite = Selection.activeObject as Sprite; if (selectedSprite != m_SelectedSprite) { if (selectedSprite != null) { string selAtlasName; Texture2D selAtlasTexture; Packer.GetAtlasDataForSprite(selectedSprite, out selAtlasName, out selAtlasTexture); int selAtlasIndex = m_AtlasNames.ToList().FindIndex(delegate(string s) { return(selAtlasName == s); }); if (selAtlasIndex == -1) { return; } int selAtlasPage = Packer.GetTexturesForAtlas(selAtlasName).ToList().FindIndex(delegate(Texture2D t) { return(selAtlasTexture == t); }); if (selAtlasPage == -1) { return; } m_SelectedAtlas = selAtlasIndex; m_SelectedPage = selAtlasPage; RefreshAtlasPageList(); } m_SelectedSprite = selectedSprite; Repaint(); } }
public static void ExportAtlasPfb() { if (!Directory.Exists(SpritesDir)) { EditorUtility.DisplayDialog("提示", "图集图片所在目录不存在", "知道了"); return; } EditorUtility.DisplayProgressBar("", "删除之前的AtlasCollection文件", 0f); Dictionary <string, AtlasCollection> pres = new Dictionary <string, AtlasCollection>(); if (Directory.Exists(AtlasCollectionsDir)) { Directory.Delete(AtlasCollectionsDir, true); AssetDatabase.SaveAssets(); } Directory.CreateDirectory(AtlasCollectionsDir); EditorUtility.DisplayProgressBar("", "Rebuild图集", 0.2f); //Rebuild Packer.RebuildAtlasCacheIfNeeded(EditorUserBuildSettings.activeBuildTarget, false); EditorUtility.DisplayProgressBar("", "重新生成图集的AtlasCollection文件", 0.5f); //收集数据 #region c#方法获得文件 /* * DirectoryInfo rootDirInfo = new DirectoryInfo(atlasDir); * foreach (FileInfo pngFile in rootDirInfo.GetFiles(".png", SearchOption.AllDirectories)) * { * string allPath = pngFile.FullName; * string assetPath = allPath.Substring(allPath.IndexOf("Assets")); */ #endregion string[] atlasSpritePath = new string[] { SpritesDir.Substring(SpritesDir.IndexOf("Assets")) }; string[] result = AssetDatabase.FindAssets(format, atlasSpritePath); for (int i = 0; i < result.Length; ++i) { string assetPath = AssetDatabase.GUIDToAssetPath(result[i]); Sprite sprite = AssetDatabase.LoadAssetAtPath <Sprite>(assetPath); if (sprite == null || !sprite.packed) { continue; } Texture2D tmp; string atlasName; Packer.GetAtlasDataForSprite(sprite, out atlasName, out tmp); //没有相应的图集问价AtlasCollection,则新建一个 if (!pres.ContainsKey(atlasName)) { AtlasCollection atl = createAsset(atlasName); pres.Add(atlasName, atl); } pres[atlasName].sprites.Add(sprite); } EditorUtility.DisplayProgressBar("", "保存资源", 0.8f); //保存 foreach (AtlasCollection obj in pres.Values) { EditorUtility.SetDirty(obj); } AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); EditorUtility.ClearProgressBar(); }
public static Dictionary <string, Texture2D> CreateAlphaAtlasTexture() { Dictionary <string, Texture2D> result = new Dictionary <string, Texture2D>(); List <Entry> entries = new List <Entry>(); Material mat = new Material(Shader.Find("Unlit/Transparent")); List <UnityEngine.Object> objects = new List <UnityEngine.Object>(); foreach (string path in AssetDatabase.FindAssets("t:sprite")) { objects.AddRange(AssetDatabase.LoadAllAssetRepresentationsAtPath(AssetDatabase.GUIDToAssetPath(path))); } Sprite[] sprites = objects.Distinct() .Select(x => x as Sprite) .Where(x => x != null && x.packed) .ToArray(); foreach (Sprite sprite in sprites) { string atlasName; Texture2D atlasTexture; Packer.GetAtlasDataForSprite(sprite, out atlasName, out atlasTexture); Texture2D texture = SpriteUtility.GetSpriteTexture(sprite, false); if (atlasTexture != null && texture != null && texture.format == TextureFormat.RGBA32) { entries.Add(new Entry() { sprite = sprite, atlasName = atlasName, texture = texture, atlasTexture = atlasTexture, uvs = SpriteUtility.GetSpriteUVs(sprite, false), atlasUvs = SpriteUtility.GetSpriteUVs(sprite, true), }); } } var atlasGroups = from e in entries group e by e.atlasTexture; foreach (var atlasGroup in atlasGroups) { Texture tex = atlasGroup.Key; RenderTexture rt = RenderTexture.GetTemporary(tex.width, tex.height, 0, RenderTextureFormat.ARGB32); Graphics.SetRenderTarget(rt); GL.Clear(true, true, Color.clear); GL.PushMatrix(); GL.LoadOrtho(); foreach (var entry in atlasGroup) { mat.mainTexture = entry.texture; mat.SetPass(0); GL.Begin(GL.TRIANGLES); var tris = entry.sprite.triangles; foreach (int index in tris) { GL.TexCoord(entry.uvs[index]); GL.Vertex(entry.atlasUvs[index]); } GL.End(); } GL.PopMatrix(); Texture2D tex2 = new Texture2D(tex.width, tex.height, TextureFormat.RGBA32, false); tex2.ReadPixels(new Rect(0, 0, tex.width, tex.height), 0, 0); var colors = tex2.GetPixels32(); int count = colors.Length; Color32[] newColors = new Color32[count]; for (int i = 0; i < count; i++) { byte alpha = colors[i].a; newColors[i] = new Color32(alpha, alpha, alpha, 255); } tex2.SetPixels32(newColors); tex2.Apply(); string texName = tex.name; texName = tex.name.Substring(0, texName.LastIndexOf("-")) + "-fmt32"; result.Add(texName, tex2); RenderTexture.ReleaseTemporary(rt); } return(result); }
private void FindAllEntries(BuildTarget buildTarget, List <SpriteEntry> spriteEntries, List <AtlasEntry> atlasEntries) { var platformString = GetPlatformString(buildTarget); foreach (var guid in AssetDatabase.FindAssets("t:Texture")) { var path = AssetDatabase.GUIDToAssetPath(guid); var importer = AssetImporter.GetAtPath(path) as TextureImporter; if (importer == null) { continue; } // 获取sprite列表 var sprites = AssetDatabase.LoadAllAssetRepresentationsAtPath(path) .Distinct() .OfType <Sprite>() .Where(x => x.packed) .ToArray(); for (var i = 0; i < sprites.Length; ++i) { var sprite = sprites[i]; Texture2D atlasTexture; string atlasName; Packer.GetAtlasDataForSprite(sprite, out atlasName, out atlasTexture); if (atlasTexture != null) { var entry = new SpriteEntry { Path = path, Sprite = sprite, Importer = importer, Texture = SpriteUtility.GetSpriteTexture(sprite, false), AtlasName = atlasName, Uvs = SpriteUtility.GetSpriteUVs(sprite, false), AtlasUvs = SpriteUtility.GetSpriteUVs(sprite, true), AtlasTexture = atlasTexture, }; spriteEntries.Add(entry); } } } // 获取atlas列表 var atlasGroups = from e in spriteEntries group e by e.AtlasTexture; foreach (var atlasGroup in atlasGroups) { var tex = atlasGroup.Key; var texName = tex.name; // 检查是否需要分离alpha通道 var atlasName = string.Empty; var needSeparateAlpha = false; foreach (var spriteEntry in atlasGroup) { var importer = spriteEntry.Importer; atlasName = importer.spritePackingTag; if (!string.IsNullOrEmpty(atlasName)) { var settings = importer.GetPlatformTextureSettings(platformString); var format = settings.format; if (format == TextureImporterFormat.Automatic) { format = importer.GetAutomaticFormat(platformString); } needSeparateAlpha = TextureUtility.IsTransparent(format); } } if (CustomAtlasConfig.ShouldKeepAlpha(atlasName)) { needSeparateAlpha = false; } var entry = new AtlasEntry { Name = texName, Texture = tex, SpriteEntries = atlasGroup.ToList(), NeedSeparateAlpha = needSeparateAlpha, }; atlasEntries.Add(entry); } }