Skip to content

Commit 0111c74

Browse files
committed
Working on multiple sprite sheet generation
1 parent e5079f8 commit 0111c74

File tree

1 file changed

+52
-33
lines changed

1 file changed

+52
-33
lines changed

Assets/SpriteSheetRuntimeGenerator/Scripts/AssetManager/AssetManager.cs

Lines changed: 52 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ protected IEnumerator process() {
4040

4141
List<Texture2D> textures = new List<Texture2D>();
4242
List<string> images = new List<string>();
43-
List<TextureAsset> textureAssets = new List<TextureAsset>();
4443

4544
foreach (ItemToRaster itemToRaster in itemsToRaster) {
4645

@@ -66,56 +65,76 @@ protected IEnumerator process() {
6665
for (int i = 0; i < mFillColor.Length; ++i)
6766
mFillColor[i] = Color.clear;
6867

69-
mPacker = new RectanglePacker(mTexture.width, mTexture.height, padding);
70-
for (int i = 0; i < textures.Count; i++)
71-
mPacker.insertRectangle((int) mRectangles[i].width, (int) mRectangles[i].height, i);
68+
int numSpriteSheet = 0;
69+
while (mRectangles.Count > 0) {
7270

73-
mPacker.packRectangles();
71+
mPacker = new RectanglePacker(mTexture.width, mTexture.height, padding);
72+
for (int i = 0; i < mRectangles.Count; i++)
73+
mPacker.insertRectangle((int) mRectangles[i].width, (int) mRectangles[i].height, i);
7474

75-
if (mPacker.rectangleCount > 0) {
75+
mPacker.packRectangles();
7676

77-
mTexture.SetPixels32(mFillColor);
78-
IntegerRectangle rect = new IntegerRectangle();
79-
textureAssets = new List<TextureAsset>();
77+
if (mPacker.rectangleCount > 0) {
8078

81-
for (int j = 0; j < mPacker.rectangleCount; j++) {
79+
mTexture.SetPixels32(mFillColor);
80+
IntegerRectangle rect = new IntegerRectangle();
81+
List<TextureAsset> textureAssets = new List<TextureAsset>();
8282

83-
rect = mPacker.getRectangle(j, rect);
83+
List<Rect> rectGarbages = new List<Rect>();
8484

85-
int index = mPacker.getRectangleId(j);
85+
for (int j = 0; j < mPacker.rectangleCount; j++) {
8686

87-
mTexture.SetPixels32(rect.x, rect.y, rect.width, rect.height, textures[index].GetPixels32());
87+
rect = mPacker.getRectangle(j, rect);
8888

89-
TextureAsset texture = new TextureAsset();
90-
texture.x = rect.x;
91-
texture.y = rect.y;
92-
texture.width = rect.width;
93-
texture.height = rect.height;
94-
texture.name = images[index];
89+
int index = mPacker.getRectangleId(j);
9590

96-
textureAssets.Add(texture);
97-
}
91+
mTexture.SetPixels32(rect.x, rect.y, rect.width, rect.height, textures[index].GetPixels32());
92+
93+
TextureAsset texture = new TextureAsset();
94+
texture.x = rect.x;
95+
texture.y = rect.y;
96+
texture.width = rect.width;
97+
texture.height = rect.height;
98+
texture.name = images[index];
99+
100+
textureAssets.Add(texture);
101+
102+
rectGarbages.Add(mRectangles[index]);
103+
}
104+
105+
foreach (Rect rectGarbage in rectGarbages) {
98106

99-
mTexture.Apply();
107+
int indexToDestroy = mRectangles.IndexOf(rectGarbage);
100108

101-
Directory.CreateDirectory(Application.persistentDataPath + "/Test/");
109+
mRectangles.RemoveAt(indexToDestroy);
110+
textures.RemoveAt(indexToDestroy);
111+
images.RemoveAt(indexToDestroy);
112+
}
102113

103-
File.WriteAllBytes(Application.persistentDataPath + "/Test/data.png", mTexture.EncodeToPNG());
104-
File.WriteAllText(Application.persistentDataPath + "/Test/data.json", JsonUtility.ToJson(new TextureAssets(textureAssets.ToArray())));
114+
mTexture.Apply();
105115

106-
/*WWW loaderTexture = new WWW("file:///" + Application.persistentDataPath + "/Test/data.png");
107-
yield return loaderTexture;
116+
Directory.CreateDirectory(Application.persistentDataPath + "/Test/");
108117

109-
WWW loaderJSON = new WWW("file:///" + Application.persistentDataPath + "/Test/data.json");
110-
yield return loaderJSON;
118+
File.WriteAllBytes(Application.persistentDataPath + "/Test/data" + numSpriteSheet + ".png", mTexture.EncodeToPNG());
119+
File.WriteAllText(Application.persistentDataPath + "/Test/data" + numSpriteSheet + ".json", JsonUtility.ToJson(new TextureAssets(textureAssets.ToArray())));
120+
++numSpriteSheet;
111121

112-
TextureAssets textureAssets = JsonUtility.FromJson<TextureAssets>(loaderJSON.text);*/
122+
/*WWW loaderTexture = new WWW("file:///" + Application.persistentDataPath + "/Test/data.png");
123+
yield return loaderTexture;
113124
114-
foreach (TextureAsset textureAsset in textureAssets)
115-
mSprites.Add(textureAsset.name, Sprite.Create(mTexture, new Rect(textureAsset.x, textureAsset.y, textureAsset.width, textureAsset.height), Vector2.zero));
125+
WWW loaderJSON = new WWW("file:///" + Application.persistentDataPath + "/Test/data.json");
126+
yield return loaderJSON;
127+
128+
TextureAssets textureAssets = JsonUtility.FromJson<TextureAssets>(loaderJSON.text);*/
129+
130+
foreach (TextureAsset textureAsset in textureAssets)
131+
mSprites.Add(textureAsset.name, Sprite.Create(mTexture, new Rect(textureAsset.x, textureAsset.y, textureAsset.width, textureAsset.height), Vector2.zero));
132+
133+
}
116134

117-
OnProcessCompleted.Invoke();
118135
}
136+
137+
OnProcessCompleted.Invoke();
119138
}
120139

121140
public Sprite GetSprite(string id) {

0 commit comments

Comments
 (0)