├── NewAtlasMaker.cs └── README /NewAtlasMaker.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | using UnityEngine.Events; 4 | 5 | using UnityEngine.UI; 6 | 7 | using UnityEditor; 8 | using UnityEditor.AnimatedValues; 9 | using UnityEditor.UI; 10 | using System.IO; 11 | using System.Linq; 12 | using UnityEngine.U2D; 13 | using UnityEditor.U2D; 14 | using System.Collections.Generic; 15 | using System.Reflection; 16 | using System.Text; 17 | using UnityEditor.PackageManager; 18 | 19 | namespace Tools 20 | { 21 | public class NewAtlasMaker : EditorWindow 22 | { 23 | private static string sptDesDir = Application.dataPath + "/Resources"; 24 | private static string sptSrcDir = Application.dataPath + "/Art"; 25 | 26 | [MenuItem("Tools/NewAtlasMaker By Folders")] 27 | 28 | public static void CreateAtlasByFolders() 29 | { 30 | DirectoryInfo rootDirInfo = new DirectoryInfo(sptSrcDir); 31 | //add folders 32 | List folders = new List(); 33 | foreach (DirectoryInfo dirInfo in rootDirInfo.GetDirectories()) 34 | { 35 | folders.Clear(); 36 | if (dirInfo != null) 37 | { 38 | string assetPath = dirInfo.FullName.Substring(dirInfo.FullName.IndexOf("Assets")); 39 | var o = AssetDatabase.LoadAssetAtPath(assetPath); 40 | if (IsPackable(o)) 41 | folders.Add(o); 42 | } 43 | string atlasName = dirInfo.Name + ".spriteatlas"; 44 | CreateAtlas(atlasName); 45 | SpriteAtlas sptAtlas = Resources.Load(dirInfo.Name); 46 | Debug.Log(sptAtlas.tag); 47 | AddPackAtlas(sptAtlas, folders.ToArray()); 48 | } 49 | 50 | //add texture by your self 51 | } 52 | 53 | [MenuItem("Tools/NewAtlasMaker By Sprite")] 54 | public static void CreateAtlasBySprite() 55 | { 56 | DirectoryInfo rootDirInfo = new DirectoryInfo(sptSrcDir); 57 | 58 | //add sprite 59 | 60 | List spts = new List(); 61 | foreach (DirectoryInfo dirInfo in rootDirInfo.GetDirectories()) 62 | { 63 | spts.Clear(); 64 | foreach (FileInfo pngFile in dirInfo.GetFiles("*.png", SearchOption.AllDirectories)) 65 | { 66 | string allPath = pngFile.FullName; 67 | string assetPath = allPath.Substring(allPath.IndexOf("Assets")); 68 | Sprite sprite = AssetDatabase.LoadAssetAtPath(assetPath); 69 | if (IsPackable(sprite)) 70 | spts.Add(sprite); 71 | } 72 | string atlasName = dirInfo.Name + ".spriteatlas"; 73 | CreateAtlas(atlasName); 74 | SpriteAtlas sptAtlas = Resources.Load(dirInfo.Name); 75 | Debug.Log(sptAtlas.tag); 76 | AddPackAtlas(sptAtlas, spts.ToArray()); 77 | } 78 | 79 | 80 | //add texture by your self 81 | } 82 | static bool IsPackable(Object o) 83 | { 84 | return o != null && (o.GetType() == typeof(Sprite) || o.GetType() == typeof(Texture2D) || (o.GetType() == typeof(DefaultAsset) && ProjectWindowUtil.IsFolder(o.GetInstanceID()))); 85 | } 86 | 87 | static void AddPackAtlas(SpriteAtlas atlas, Object[] spt) 88 | { 89 | MethodInfo methodInfo = System.Type 90 | .GetType("UnityEditor.U2D.SpriteAtlasExtensions, UnityEditor") 91 | .GetMethod("Add", BindingFlags.Public | BindingFlags.Static); 92 | if (methodInfo != null) 93 | methodInfo.Invoke(null, new object[] { atlas, spt }); 94 | else 95 | Debug.Log("methodInfo is null"); 96 | PackAtlas(atlas); 97 | } 98 | 99 | static void PackAtlas(SpriteAtlas atlas) 100 | { 101 | System.Type 102 | .GetType("UnityEditor.U2D.SpriteAtlasUtility, UnityEditor") 103 | .GetMethod("PackAtlases", BindingFlags.NonPublic | BindingFlags.Static) 104 | .Invoke(null, new object[] { new[] { atlas }, EditorUserBuildSettings.activeBuildTarget }); 105 | } 106 | 107 | public static void CreateAtlas(string atlasName) 108 | { 109 | string yaml = @"%YAML 1.1 110 | %TAG !u! tag:unity3d.com,2011: 111 | --- !u!687078895 &4343727234628468602 112 | SpriteAtlas: 113 | m_ObjectHideFlags: 0 114 | m_PrefabParentObject: {fileID: 0} 115 | m_PrefabInternal: {fileID: 0} 116 | m_Name: New Sprite Atlas 117 | m_EditorData: 118 | textureSettings: 119 | serializedVersion: 2 120 | anisoLevel: 1 121 | compressionQuality: 50 122 | maxTextureSize: 2048 123 | textureCompression: 0 124 | filterMode: 1 125 | generateMipMaps: 0 126 | readable: 0 127 | crunchedCompression: 0 128 | sRGB: 1 129 | platformSettings: [] 130 | packingParameters: 131 | serializedVersion: 2 132 | padding: 4 133 | blockOffset: 1 134 | allowAlphaSplitting: 0 135 | enableRotation: 1 136 | enableTightPacking: 1 137 | variantMultiplier: 1 138 | packables: [] 139 | bindAsDefault: 1 140 | m_MasterAtlas: {fileID: 0} 141 | m_PackedSprites: [] 142 | m_PackedSpriteNamesToIndex: [] 143 | m_Tag: New Sprite Atlas 144 | m_IsVariant: 0 145 | "; 146 | AssetDatabase.Refresh(); 147 | 148 | if (!Directory.Exists(sptDesDir )) 149 | { 150 | Directory.CreateDirectory(sptDesDir ); 151 | AssetDatabase.Refresh(); 152 | } 153 | string filePath = sptDesDir + "/" + atlasName; 154 | if (File.Exists(filePath)) 155 | { 156 | File.Delete(filePath); 157 | AssetDatabase.Refresh(); 158 | } 159 | FileStream fs = new FileStream(filePath, FileMode.CreateNew); 160 | byte[] bytes = new UTF8Encoding().GetBytes(yaml); 161 | fs.Write(bytes, 0, bytes.Length); 162 | fs.Close(); 163 | AssetDatabase.Refresh(); 164 | } 165 | 166 | } 167 | 168 | } 169 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | 网上没找到,只好自己写了。版本不同可以只更换yaml文本就可以了。推荐编辑器采用yaml来实现。yaml解析可以参考https://github.com/hanbim520/UnityYaml.git --------------------------------------------------------------------------------