11using System . Collections ;
22using System . Collections . Generic ;
3+ using System . IO ;
34using System . Reflection ;
45using UnityEditor ;
56using UnityEditor . U2D ;
@@ -11,42 +12,59 @@ namespace Wanderer.GameFramework
1112
1213 public class PrefabSpritePacker
1314 {
15+ private static SpriteAtlasPackingSettings _packingSettings = new SpriteAtlasPackingSettings ( ) ;
16+ private static TextureImporterPlatformSettings _androidSettings = new TextureImporterPlatformSettings ( ) ;
17+ private static TextureImporterPlatformSettings _iOSSettings = new TextureImporterPlatformSettings ( ) ;
18+ 1419 [ MenuItem ( "Assets/Prefab To SpriteAtlas" ) ]
1520 private static void PrefabToSpriteAtlas ( )
1621 {
17- string selectPath = AssetDatabase . GetAssetPath ( Selection . activeObject ) ;
18- if ( AssetDatabase . IsValidFolder ( selectPath ) )
19- {
20- var assets = AssetDatabase . FindAssets ( "t:Prefab" , new string [ ] { selectPath } ) ;
21- if ( assets != null )
22- {
23- HashSet < Object > objects = new HashSet < Object > ( ) ;
24- foreach ( var item in assets )
22+ if ( Selection . objects == null )
23+ return ;
24+ 25+ bool selectFolder = false ;
26+ 27+ if ( Selection . assetGUIDs . Length == 1 )
28+ {
29+ string selectPath = AssetDatabase . GUIDToAssetPath ( Selection . assetGUIDs [ 0 ] ) ;
30+ if ( AssetDatabase . IsValidFolder ( selectPath ) )
31+ {
32+ selectFolder = true ;
33+ 34+ var assets = AssetDatabase . FindAssets ( "t:Prefab" , new string [ ] { selectPath } ) ;
35+ if ( assets != null )
2536 {
26- string assetPath = AssetDatabase . GUIDToAssetPath ( item ) ;
27- foreach ( var item02 in GameObjectGetDependencies ( AssetDatabase . LoadAssetAtPath < GameObject > ( assetPath ) ) )
37+ HashSet < Object > objects = new HashSet < Object > ( ) ;
38+ foreach ( var item in assets )
2839 {
29- objects . Add ( item02 ) ;
30- }
40+ string assetPath = AssetDatabase . GUIDToAssetPath ( item ) ;
41+ Debug . Log ( assetPath ) ;
42+ foreach ( var item02 in GameObjectGetDependencies ( AssetDatabase . LoadAssetAtPath < GameObject > ( assetPath ) ) )
43+ {
44+ objects . Add ( item02 ) ;
45+ }
46+ }
47+ string fileName = Path . GetFileNameWithoutExtension ( selectPath ) ;
48+ SaveSpriteAtlas ( objects , $ "{ selectPath } /{ fileName } _spriteatlas.spriteatlas") ;
3149 }
32- SaveSpriteAtlas ( objects , $ "{ selectPath } /SpriteAtlas.spriteatlas") ;
33- }
34- }
35- else
36- {
37- GameObject [ ] go = Selection . gameObjects ;
38- if ( go == null )
39- return ;
40- foreach ( GameObject g in go )
41- {
42- GameObjectToSpriteAtlas ( g ) ;
50+ 4351 }
4452 }
53+ //没有选择文件夹
54+ if ( ! selectFolder )
55+ {
56+ GameObject [ ] go = Selection . gameObjects ;
57+ if ( go == null )
58+ return ;
59+ foreach ( GameObject g in go )
60+ {
61+ GameObjectToSpriteAtlas ( g ) ;
62+ }
63+ }
4564 }
4665
47- 48- [ MenuItem ( "Assets/SpriteAtlas Not In Build" ) ]
49- private static void SpriteAtlasNotInBuild ( )
66+ [ MenuItem ( "Assets/SpriteAtlas Global Setting" ) ]
67+ private static void SpriteAtlasGlobalSetting ( )
5068 {
5169 var assets = AssetDatabase . FindAssets ( "t:SpriteAtlas" ) ;
5270 if ( assets != null )
@@ -57,14 +75,17 @@ private static void SpriteAtlasNotInBuild()
5775 SpriteAtlas spriteAtlas = AssetDatabase . LoadAssetAtPath < SpriteAtlas > ( assetPath ) ;
5876 if ( spriteAtlas != null )
5977 {
60- spriteAtlas . SetIncludeInBuild ( false ) ;
78+ SetSpriteAtlas ( spriteAtlas ) ;
6179 }
6280 }
6381 AssetDatabase . Refresh ( ) ;
6482 }
6583 }
6684
67- 85+ /// <summary>
86+ /// GameObject设置SpriteAtlas
87+ /// </summary>
88+ /// <param name="go"></param>
6889 private static void GameObjectToSpriteAtlas ( GameObject go )
6990 {
7091 string assetPath = AssetDatabase . GetAssetPath ( go ) ;
@@ -73,49 +94,48 @@ private static void GameObjectToSpriteAtlas(GameObject go)
7394 {
7495 objects . Add ( item ) ;
7596 }
76- SaveSpriteAtlas ( objects , $ "{ assetPath } .spriteatlas") ;
97+ SaveSpriteAtlas ( objects , $ "{ assetPath } _spriteatlas .spriteatlas") ;
7798 }
7899
100+ /// <summary>
101+ /// 获取所有的引用
102+ /// </summary>
103+ /// <param name="go"></param>
104+ /// <returns></returns>
79105 private static List < Object > GameObjectGetDependencies ( GameObject go )
80106 {
81107 string assetPath = AssetDatabase . GetAssetPath ( go ) ;
82108 string [ ] dependencies = AssetDatabase . GetDependencies ( assetPath ) ;
83109 if ( dependencies != null )
84110 {
85- List < Object > depSprites = new List < Object > ( ) ;
111+ List < Object > depObjects = new List < Object > ( ) ;
86112 foreach ( var item in dependencies )
87113 {
88114 // string _otherGUID = AssetDatabase.AssetPathToGUID(item);
89- var itemSprite = AssetDatabase . LoadAssetAtPath < Object > ( item ) ;
90- if ( itemSprite is Sprite || itemSprite is Texture2D )
115+ var itemObject = AssetDatabase . LoadAssetAtPath < Object > ( item ) ;
116+ if ( itemObject is Sprite || itemObject is Texture2D )
91117 {
92- depSprites . Add ( itemSprite ) ;
118+ if ( itemObject != null )
119+ {
120+ depObjects . Add ( itemObject ) ;
121+ }
93122 }
94123 }
95- return depSprites ;
124+ return depObjects ;
96125 }
97126 return new List < Object > ( ) ;
98127 }
99128
100129
130+ /// <summary>
131+ /// 保存SpriteAtlas
132+ /// </summary>
133+ /// <param name="objs"></param>
134+ /// <param name="saPath"></param>
101135 public static void SaveSpriteAtlas ( HashSet < Object > objs , string saPath )
102136 {
103137 if ( objs == null || objs . Count == 0 )
104138 return ;
105- 106- TextureImporterPlatformSettings androidSettings = new TextureImporterPlatformSettings ( ) ;
107- androidSettings . name = "Android" ;
108- androidSettings . overridden = true ;
109- androidSettings . maxTextureSize = 4096 ;
110- androidSettings . resizeAlgorithm = TextureResizeAlgorithm . Mitchell ;
111- androidSettings . format = TextureImporterFormat . ETC2_RGBA8 ;
112- 113- TextureImporterPlatformSettings iOSSettings = new TextureImporterPlatformSettings ( ) ;
114- iOSSettings . name = "iOS" ;
115- iOSSettings . overridden = true ;
116- iOSSettings . maxTextureSize = 4096 ;
117- iOSSettings . resizeAlgorithm = TextureResizeAlgorithm . Mitchell ;
118- iOSSettings . format = TextureImporterFormat . PVRTC_RGBA4 ;
119139
120140 // SpriteAtlasUtility.
121141 Object [ ] objArray = new Object [ objs . Count ] ;
@@ -125,42 +145,42 @@ public static void SaveSpriteAtlas(HashSet<Object> objs, string saPath)
125145 objArray [ index ] = item ;
126146 index ++ ;
127147 }
128- SpriteAtlas spriteAtlas = new SpriteAtlas ( ) ;
129- spriteAtlas . SetIncludeInBuild ( false ) ;
130- spriteAtlas . Add ( objArray ) ;
131- 132- spriteAtlas . SetPlatformSettings ( androidSettings ) ;
133- // spriteAtlas.SetPlatformSettings(iOSSettings);
134- 135- AssetDatabase . CreateAsset ( spriteAtlas , saPath ) ;
136- AssetDatabase . Refresh ( ) ;
137- 138- spriteAtlas . SetPlatformSettings ( iOSSettings ) ;
139- AssetDatabase . Refresh ( ) ;
140- // int result = spriteAtlas.GetSprites(depSprites.ToArray());
141- // Debug.Log($"GetSprites:{result}");
148+ SpriteAtlas spriteAtlas = new SpriteAtlas ( ) ;
149+ spriteAtlas . Add ( objArray ) ;
150+ SetSpriteAtlas ( spriteAtlas ) ;
151+ AssetDatabase . CreateAsset ( spriteAtlas , saPath ) ;
152+ AssetDatabase . Refresh ( ) ;
142153 }
143154
155+ 144156
145- //static void AddPackAtlas(SpriteAtlas atlas, Object[] spt)
146- //{
147- // MethodInfo methodInfo = System.Type
148- // .GetType("UnityEditor.U2D.SpriteAtlasExtensions, UnityEditor")
149- // .GetMethod("Add", BindingFlags.Public | BindingFlags.Static);
150- // if (methodInfo != null)
151- // methodInfo.Invoke(null, new object[] { atlas, spt });
152- // else
153- // Debug.Log("methodInfo is null");
154- // // PackAtlas(atlas);
155- //}
156- 157- //static void PackAtlas(SpriteAtlas atlas)
158- //{
159- // System.Type
160- // .GetType("UnityEditor.U2D.SpriteAtlasUtility, UnityEditor")
161- // .GetMethod("PackAtlases", BindingFlags.NonPublic | BindingFlags.Static)
162- // .Invoke(null, new object[] { new[] { atlas }, EditorUserBuildSettings.activeBuildTarget });
163- //}
157+ /// <summary>
158+ /// 设置图集参数
159+ /// </summary>
160+ /// <param name="spriteAtlas"></param>
161+ private static void SetSpriteAtlas ( SpriteAtlas spriteAtlas )
162+ {
163+ _packingSettings . enableTightPacking = false ;
164+ _packingSettings . enableRotation = false ;
165+ _packingSettings . padding = 4 ;
166+ 167+ _androidSettings . name = "Android" ;
168+ _androidSettings . overridden = true ;
169+ // androidSettings.maxTextureSize = 2048;
170+ // _androidSettings.resizeAlgorithm = TextureResizeAlgorithm.Mitchell;
171+ _androidSettings . format = TextureImporterFormat . ETC2_RGBA8 ;
172+ 173+ _iOSSettings . name = "iOS" ;
174+ _iOSSettings . overridden = true ;
175+ //iOSSettings.maxTextureSize = 2048;
176+ // _iOSSettings.resizeAlgorithm = TextureResizeAlgorithm.Mitchell;
177+ _iOSSettings . format = TextureImporterFormat . PVRTC_RGBA4 ;
178+ 179+ spriteAtlas . SetIncludeInBuild ( false ) ;
180+ spriteAtlas . SetPackingSettings ( _packingSettings ) ;
181+ spriteAtlas . SetPlatformSettings ( _androidSettings ) ;
182+ spriteAtlas . SetPlatformSettings ( _iOSSettings ) ;
183+ }
164184
165185 }
166186}
0 commit comments