1+ using UnityEngine ;
2+ using UnityEditor ;
3+ using System . Collections ;
4+ using UnityEditorInternal ;
5+ using System . Collections . Generic ;
6+ using System ;
7+ using System . Linq ;
8+ using System . IO ;
9+
10+
11+ // this version uses listeners to postprocess whenever sprites are loaded
12+ // - might be good for more established dev workflow
13+
14+
15+ public class SpriteAnimProcessor : AssetPostprocessor
16+ {
17+
18+
19+ // string monsterSpritesPath = "_Project/Sprites/monsters-400h";
20+
21+
22+
23+
24+ // // before texture is imported
25+ // private void OnPreprocessTexture()
26+ // {
27+
28+
29+ // //Debug.Log(assetPath.ToString());
30+
31+ // // MONSTER SPRITES
32+ // if (assetPath.Contains(monsterSpritesPath))
33+ // {
34+ // Debug.Log("OnPreprocessTexture overwriting defaults");
35+
36+ // // create TextureImporter
37+ // TextureImporter textureImporter = assetImporter as TextureImporter;
38+ // // set all to sprite, multiple (animations)
39+ // textureImporter.textureType = TextureImporterType.Sprite;
40+ // textureImporter.spriteImportMode = SpriteImportMode.Multiple;
41+ // // allow mipmaps (small versions on screen)
42+ // textureImporter.mipmapEnabled = false;
43+ // // how the Texture is filtered when it gets stretched by 3D transformations
44+ // textureImporter.filterMode = FilterMode.Point;
45+ // textureImporter.spritePivot = Vector2.down;
46+ // // do not compress
47+ // textureImporter.textureCompression = TextureImporterCompression.Uncompressed;
48+
49+
50+
51+ // var textureSettings = new TextureImporterSettings(); // need this stupid class because spriteExtrude and spriteMeshType aren't exposed on TextureImporter
52+ // textureImporter.ReadTextureSettings(textureSettings);
53+ // textureSettings.spriteMeshType = SpriteMeshType.Tight;
54+ // textureSettings.spriteExtrude = 0;
55+ // textureImporter.SetTextureSettings(textureSettings);
56+
57+
58+ // }
59+ // }
60+ // // after texture is imported
61+ // private void OnPostprocessTexture(Texture2D texture)
62+ // {
63+ // if (assetPath.ToLower().IndexOf(monsterSpritesPath) == -1)
64+ // return;
65+
66+
67+
68+
69+ // // # of animation frames
70+ // int spriteSize = 3;
71+ // int colCount = texture.width / spriteSize;
72+ // int rowCount = texture.height / spriteSize;
73+
74+
75+
76+
77+ // int minimumSpriteSize = 16;
78+ // int extrudeSize = 0;
79+ // Rect[] rects = InternalSpriteUtility.GenerateAutomaticSpriteRectangles(texture, minimumSpriteSize, extrudeSize);
80+
81+ // var rectsList = new List<Rect>(rects);
82+ // rectsList = SortRects(rectsList, texture.width);
83+
84+
85+ // string filenameNoExtension = Path.GetFileNameWithoutExtension(path);
86+ // var metas = new List<SpriteMetaData>();
87+ // int rectNum = 0;
88+
89+ // foreach (Rect rect in rectsList)
90+ // {
91+ // var meta = new SpriteMetaData();
92+ // meta.pivot = Vector2.down;
93+ // meta.alignment = (int)SpriteAlignment.BottomCenter;
94+ // meta.rect = rect;
95+ // meta.name = filenameNoExtension + "_" + rectNum++;
96+ // metas.Add(meta);
97+ // }
98+
99+ // importer.spritesheet = metas.ToArray();
100+
101+ // AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
102+
103+
104+
105+
106+
107+ // }
108+ //}
109+
110+ ////// after texture is imported
111+ ////public void OnPostprocessTexture(Texture2D texture)
112+ ////{
113+ //// if (assetPath.ToLower().IndexOf(monsterSpritesPath) == -1)
114+ //// return;
115+
116+
117+ //// // var A_Sprite = rects.OrderBy(r => r.width * r.height).First().center;
118+ //// // int colCount = rects.Where(r => r.Contains(new Vector2(r.center.x, A_Sprite.y))).Count();
119+ //// // int rowCount = rects.Where(r => r.Contains(new Vector2(A_Sprite.x, r.center.y))).Count();
120+ //// //Vector2Int spriteSize = new Vector2Int(texture.width / colCount, texture.height / rowCount);
121+
122+ //// //List<SpriteMetaData> metas = new List<SpriteMetaData>();
123+
124+ //// //for (int r = 0; r < rowCount; ++r)
125+ //// //{
126+ //// // for (int c = 0; c < colCount; ++c)
127+ //// // {
128+ //// // SpriteMetaData meta = new SpriteMetaData();
129+ //// // meta.rect = new Rect(c * spriteSize.x, r * spriteSize.y, spriteSize.x, spriteSize.y);
130+ //// // meta.name = string.Format("#{3} {0} ({1},{2})", Path.GetFileNameWithoutExtension(assetImporter.assetPath), c, r, r * colCount + c);
131+ //// // metas.Add(meta);
132+ //// // }
133+ //// //}
134+
135+ //// // TextureImporter textureImporter = (TextureImporter)assetImporter;
136+ //// // textureImporter.spritesheet = metas.ToArray();
137+ //// // AssetDatabase.Refresh();
138+
139+ ////}
140+
141+
142+ //static List<Rect> SortRects(List<Rect> rects, float textureWidth)
143+ //{
144+ // List<Rect> list = new List<Rect>();
145+ // while (rects.Count > 0)
146+ // {
147+ // Rect rect = rects[rects.Count - 1];
148+ // Rect sweepRect = new Rect(0f, rect.yMin, textureWidth, rect.height);
149+ // List<Rect> list2 = RectSweep(rects, sweepRect);
150+ // if (list2.Count <= 0)
151+ // {
152+ // list.AddRange(rects);
153+ // break;
154+ // }
155+ // list.AddRange(list2);
156+ // }
157+ // return list;
158+ //}
159+
160+ //static List<Rect> RectSweep(List<Rect> rects, Rect sweepRect)
161+ //{
162+ // List<Rect> result;
163+ // if (rects == null || rects.Count == 0)
164+ // {
165+ // result = new List<Rect>();
166+ // }
167+ // else
168+ // {
169+ // List<Rect> list = new List<Rect>();
170+ // foreach (Rect current in rects)
171+ // {
172+ // if (current.Overlaps(sweepRect))
173+ // {
174+ // list.Add(current);
175+ // }
176+ // }
177+ // foreach (Rect current2 in list)
178+ // {
179+ // rects.Remove(current2);
180+ // }
181+ // list.Sort((a, b) => a.x.CompareTo(b.x));
182+ // result = list;
183+ // }
184+ // return result;
185+ //}
186+
187+
188+
189+ }
0 commit comments