|
6 | 6 | using UnityEngine;
|
7 | 7 | using UnityEngine.Events;
|
8 | 8 |
|
9 |
| -public class AssetPacker : MonoBehaviour { |
| 9 | +namespace DaVikingCode.AssetPacker { |
10 | 10 |
|
11 |
| - public UnityEvent OnProcessCompleted; |
| 11 | + public class AssetPacker : MonoBehaviour { |
12 | 12 |
|
13 |
| - protected Dictionary<string, Sprite> mSprites = new Dictionary<string, Sprite>(); |
14 |
| - protected List<ItemToRaster> itemsToRaster = new List<ItemToRaster>(); |
| 13 | + public UnityEvent OnProcessCompleted; |
| 14 | + public float pixelsPerUnit = 100.0f; |
15 | 15 |
|
16 |
| - protected RectanglePacker mPacker; |
17 |
| - protected Texture2D mTexture; |
| 16 | + protected Dictionary<string, Sprite> mSprites = new Dictionary<string, Sprite>(); |
| 17 | + protected List<ItemToRaster> itemsToRaster = new List<ItemToRaster>(); |
18 | 18 |
|
19 |
| - protected bool allow4096Textures = false; |
| 19 | + protected bool allow4096Textures = false; |
20 | 20 |
|
21 |
| - public void AddItemToRaster(string file, string customID = null) { |
| 21 | + public void AddItemToRaster(string file, string customID = null) { |
22 | 22 |
|
23 |
| - itemsToRaster.Add(new ItemToRaster(file, customID != null ? customID : Path.GetFileNameWithoutExtension(file))); |
24 |
| - } |
| 23 | + itemsToRaster.Add(new ItemToRaster(file, customID != null ? customID : Path.GetFileNameWithoutExtension(file))); |
| 24 | + } |
25 | 25 |
|
26 |
| - public void AddItemsToRaster(string[] files) { |
| 26 | + public void AddItemsToRaster(string[] files) { |
27 | 27 |
|
28 |
| - foreach (string file in files) |
29 |
| - AddItemToRaster(file); |
30 |
| - } |
| 28 | + foreach (string file in files) |
| 29 | + AddItemToRaster(file); |
| 30 | + } |
31 | 31 |
|
32 |
| - public void Process(bool allow4096Textures = false) { |
| 32 | + public void Process(bool allow4096Textures = false) { |
33 | 33 |
|
34 |
| - this.allow4096Textures = allow4096Textures; |
| 34 | + this.allow4096Textures = allow4096Textures; |
35 | 35 |
|
36 |
| - StartCoroutine(process()); |
37 |
| - } |
| 36 | + StartCoroutine(process()); |
| 37 | + } |
38 | 38 |
|
39 |
| - protected IEnumerator process() { |
| 39 | + protected IEnumerator process() { |
40 | 40 |
|
41 |
| - List<Texture2D> textures = new List<Texture2D>(); |
42 |
| - List<string> images = new List<string>(); |
| 41 | + List<Texture2D> textures = new List<Texture2D>(); |
| 42 | + List<string> images = new List<string>(); |
43 | 43 |
|
44 |
| - foreach (ItemToRaster itemToRaster in itemsToRaster) { |
| 44 | + foreach (ItemToRaster itemToRaster in itemsToRaster) { |
45 | 45 |
|
46 |
| - WWW loader = new WWW("file:///" + itemToRaster.file); |
| 46 | + WWW loader = new WWW("file:///" + itemToRaster.file); |
47 | 47 |
|
48 |
| - yield return loader; |
| 48 | + yield return loader; |
49 | 49 |
|
50 |
| - textures.Add(loader.texture); |
51 |
| - images.Add(itemToRaster.id); |
52 |
| - } |
| 50 | + textures.Add(loader.texture); |
| 51 | + images.Add(itemToRaster.id); |
| 52 | + } |
53 | 53 |
|
54 |
| - List<Rect> mRectangles = new List<Rect>(); |
55 |
| - for (int i = 0; i < textures.Count; i++) |
56 |
| - mRectangles.Add(new Rect(0, 0, textures[i].width, textures[i].height)); |
| 54 | + List<Rect> mRectangles = new List<Rect>(); |
| 55 | + for (int i = 0; i < textures.Count; i++) |
| 56 | + mRectangles.Add(new Rect(0, 0, textures[i].width, textures[i].height)); |
57 | 57 |
|
58 |
| - const int padding = 1; |
| 58 | + const int padding = 1; |
59 | 59 |
|
60 |
| - int textureSize = allow4096Textures ? 4096 : 2048; |
| 60 | + int textureSize = allow4096Textures ? 4096 : 2048; |
61 | 61 |
|
62 |
| - int numSpriteSheet = 0; |
63 |
| - while (mRectangles.Count > 0) { |
| 62 | + int numSpriteSheet = 0; |
| 63 | + while (mRectangles.Count > 0) { |
64 | 64 |
|
65 |
| - mTexture = new Texture2D(textureSize, textureSize, TextureFormat.ARGB32, false); |
66 |
| - Color32[] mFillColor = mTexture.GetPixels32(); |
67 |
| - for (int i = 0; i < mFillColor.Length; ++i) |
68 |
| - mFillColor[i] = Color.clear; |
| 65 | + Texture2D mTexture = new Texture2D(textureSize, textureSize, TextureFormat.ARGB32, false); |
| 66 | + Color32[] mFillColor = mTexture.GetPixels32(); |
| 67 | + for (int i = 0; i < mFillColor.Length; ++i) |
| 68 | + mFillColor[i] = Color.clear; |
69 | 69 |
|
70 |
| - mPacker = new RectanglePacker(mTexture.width, mTexture.height, padding); |
| 70 | + RectanglePacker mPacker = new RectanglePacker(mTexture.width, mTexture.height, padding); |
71 | 71 |
|
72 |
| - for (int i = 0; i < mRectangles.Count; i++) |
73 |
| - mPacker.insertRectangle((int) mRectangles[i].width, (int) mRectangles[i].height, i); |
| 72 | + for (int i = 0; i < mRectangles.Count; i++) |
| 73 | + mPacker.insertRectangle((int) mRectangles[i].width, (int) mRectangles[i].height, i); |
74 | 74 |
|
75 |
| - mPacker.packRectangles(); |
| 75 | + mPacker.packRectangles(); |
76 | 76 |
|
77 |
| - if (mPacker.rectangleCount > 0) { |
| 77 | + if (mPacker.rectangleCount > 0) { |
78 | 78 |
|
79 |
| - mTexture.SetPixels32(mFillColor); |
80 |
| - IntegerRectangle rect = new IntegerRectangle(); |
81 |
| - List<TextureAsset> textureAssets = new List<TextureAsset>(); |
| 79 | + mTexture.SetPixels32(mFillColor); |
| 80 | + IntegerRectangle rect = new IntegerRectangle(); |
| 81 | + List<TextureAsset> textureAssets = new List<TextureAsset>(); |
82 | 82 |
|
83 |
| - List<Rect> garbageRect = new List<Rect>(); |
84 |
| - List<Texture2D> garabeTextures = new List<Texture2D>(); |
85 |
| - List<string> garbageImages = new List<string>(); |
| 83 | + List<Rect> garbageRect = new List<Rect>(); |
| 84 | + List<Texture2D> garabeTextures = new List<Texture2D>(); |
| 85 | + List<string> garbageImages = new List<string>(); |
86 | 86 |
|
87 |
| - for (int j = 0; j < mPacker.rectangleCount; j++) { |
| 87 | + for (int j = 0; j < mPacker.rectangleCount; j++) { |
88 | 88 |
|
89 |
| - rect = mPacker.getRectangle(j, rect); |
| 89 | + rect = mPacker.getRectangle(j, rect); |
90 | 90 |
|
91 |
| - int index = mPacker.getRectangleId(j); |
| 91 | + int index = mPacker.getRectangleId(j); |
92 | 92 |
|
93 |
| - mTexture.SetPixels32(rect.x, rect.y, rect.width, rect.height, textures[index].GetPixels32()); |
| 93 | + mTexture.SetPixels32(rect.x, rect.y, rect.width, rect.height, textures[index].GetPixels32()); |
94 | 94 |
|
95 |
| - TextureAsset texture = new TextureAsset(); |
96 |
| - texture.x = rect.x; |
97 |
| - texture.y = rect.y; |
98 |
| - texture.width = rect.width; |
99 |
| - texture.height = rect.height; |
100 |
| - texture.name = images[index]; |
| 95 | + TextureAsset texture = new TextureAsset(); |
| 96 | + texture.x = rect.x; |
| 97 | + texture.y = rect.y; |
| 98 | + texture.width = rect.width; |
| 99 | + texture.height = rect.height; |
| 100 | + texture.name = images[index]; |
101 | 101 |
|
102 |
| - textureAssets.Add(texture); |
| 102 | + textureAssets.Add(texture); |
103 | 103 |
|
104 |
| - garbageRect.Add(mRectangles[index]); |
105 |
| - garabeTextures.Add(textures[index]); |
106 |
| - garbageImages.Add(images[index]); |
107 |
| - } |
| 104 | + garbageRect.Add(mRectangles[index]); |
| 105 | + garabeTextures.Add(textures[index]); |
| 106 | + garbageImages.Add(images[index]); |
| 107 | + } |
| 108 | + |
| 109 | + foreach (Rect garbage in garbageRect) |
| 110 | + mRectangles.Remove(garbage); |
108 | 111 |
|
109 |
| - foreach (Rect garbage in garbageRect) |
110 |
| - mRectangles.Remove(garbage); |
| 112 | + foreach (Texture2D garbage in garabeTextures) |
| 113 | + textures.Remove(garbage); |
111 | 114 |
|
112 |
| - foreach (Texture2D garbage in garabeTextures) |
113 |
| - textures.Remove(garbage); |
| 115 | + foreach (string garbage in garbageImages) |
| 116 | + images.Remove(garbage); |
114 | 117 |
|
115 |
| - foreach (string garbage in garbageImages) |
116 |
| - images.Remove(garbage); |
| 118 | + mTexture.Apply(); |
117 | 119 |
|
118 |
| - mTexture.Apply(); |
| 120 | + Directory.CreateDirectory(Application.persistentDataPath + "/Test/"); |
119 | 121 |
|
120 |
| - Directory.CreateDirectory(Application.persistentDataPath + "/Test/"); |
| 122 | + File.WriteAllBytes(Application.persistentDataPath + "/Test/data" + numSpriteSheet + ".png", mTexture.EncodeToPNG()); |
| 123 | + File.WriteAllText(Application.persistentDataPath + "/Test/data" + numSpriteSheet + ".json", JsonUtility.ToJson(new TextureAssets(textureAssets.ToArray()))); |
| 124 | + ++numSpriteSheet; |
121 | 125 |
|
122 |
| - File.WriteAllBytes(Application.persistentDataPath + "/Test/data" + numSpriteSheet + ".png", mTexture.EncodeToPNG()); |
123 |
| - File.WriteAllText(Application.persistentDataPath + "/Test/data" + numSpriteSheet + ".json", JsonUtility.ToJson(new TextureAssets(textureAssets.ToArray()))); |
124 |
| - ++numSpriteSheet; |
| 126 | + /*WWW loaderTexture = new WWW("file:///" + Application.persistentDataPath + "/Test/data.png"); |
| 127 | + yield return loaderTexture; |
125 | 128 |
|
126 |
| - /*WWW loaderTexture = new WWW("file:///" + Application.persistentDataPath + "/Test/data.png"); |
127 |
| - yield return loaderTexture; |
| 129 | + WWW loaderJSON = new WWW("file:///" + Application.persistentDataPath + "/Test/data.json"); |
| 130 | + yield return loaderJSON; |
128 | 131 |
|
129 |
| - WWW loaderJSON = new WWW("file:///" + Application.persistentDataPath + "/Test/data.json"); |
130 |
| - yield return loaderJSON; |
| 132 | + TextureAssets textureAssets = JsonUtility.FromJson<TextureAssets>(loaderJSON.text);*/ |
131 | 133 |
|
132 |
| - TextureAssets textureAssets = JsonUtility.FromJson<TextureAssets>(loaderJSON.text);*/ |
| 134 | + foreach (TextureAsset textureAsset in textureAssets) |
| 135 | + mSprites.Add(textureAsset.name, Sprite.Create(mTexture, new Rect(textureAsset.x, textureAsset.y, textureAsset.width, textureAsset.height), Vector2.zero, pixelsPerUnit, 0, SpriteMeshType.FullRect)); |
| 136 | + } |
133 | 137 |
|
134 |
| - foreach (TextureAsset textureAsset in textureAssets) |
135 |
| - mSprites.Add(textureAsset.name, Sprite.Create(mTexture, new Rect(textureAsset.x, textureAsset.y, textureAsset.width, textureAsset.height), Vector2.zero, 100, 0, SpriteMeshType.FullRect)); |
136 | 138 | }
|
137 | 139 |
|
| 140 | + OnProcessCompleted.Invoke(); |
138 | 141 | }
|
139 | 142 |
|
140 |
| - OnProcessCompleted.Invoke(); |
141 |
| - } |
| 143 | + public Sprite GetSprite(string id) { |
142 | 144 |
|
143 |
| - public Sprite GetSprite(string id) { |
| 145 | + Sprite sprite = null; |
144 | 146 |
|
145 |
| - Sprite sprite = null; |
| 147 | + mSprites.TryGetValue (id, out sprite); |
146 | 148 |
|
147 |
| - mSprites.TryGetValue (id, out sprite); |
| 149 | + return sprite; |
| 150 | + } |
148 | 151 |
|
149 |
| - return sprite; |
150 |
| - } |
| 152 | + public Sprite[] GetSprites(string prefix) { |
151 | 153 |
|
152 |
| - public Sprite[] GetSprites(string prefix) { |
| 154 | + List<string> spriteNames = new List<string>(); |
| 155 | + foreach (var asset in mSprites) |
| 156 | + spriteNames.Add(asset.Key); |
153 | 157 |
|
154 |
| - List<string> spriteNames = new List<string>(); |
155 |
| - foreach (var asset in mSprites) |
156 |
| - spriteNames.Add(asset.Key); |
| 158 | + spriteNames.Sort(StringComparer.Ordinal); |
157 | 159 |
|
158 |
| - spriteNames.Sort(StringComparer.Ordinal); |
| 160 | + List<Sprite> sprites = new List<Sprite>(); |
| 161 | + Sprite sprite; |
| 162 | + for (int i = 0; i < spriteNames.Count; ++i) { |
159 | 163 |
|
160 |
| - List<Sprite> sprites = new List<Sprite>(); |
161 |
| - Sprite sprite; |
162 |
| - for (int i = 0; i < spriteNames.Count; ++i) { |
| 164 | + mSprites.TryGetValue(spriteNames[i], out sprite); |
163 | 165 |
|
164 |
| - mSprites.TryGetValue(spriteNames[i], out sprite); |
| 166 | + sprites.Add(sprite); |
| 167 | + } |
165 | 168 |
|
166 |
| - sprites.Add(sprite); |
| 169 | + return sprites.ToArray(); |
167 | 170 | }
|
168 |
| - |
169 |
| - return sprites.ToArray(); |
170 | 171 | }
|
171 | 172 | }
|
0 commit comments