Skip to content

Commit 1628493

Browse files
committed
created AssetManager class
1 parent 2dc03fd commit 1628493

File tree

6 files changed

+167
-165
lines changed

6 files changed

+167
-165
lines changed

Assets/SpriteSheetRuntimeGenerator/Demos/Scenes/LoadTextures.unity

Lines changed: 13 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ RectTransform:
206206
m_LocalScale: {x: 1, y: 1, z: 1}
207207
m_Children: []
208208
m_Father: {fileID: 1025881487}
209-
m_RootOrder: 1
209+
m_RootOrder: 0
210210
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
211211
m_AnchorMin: {x: 0.5, y: 0.5}
212212
m_AnchorMax: {x: 0.5, y: 0.5}
@@ -330,7 +330,6 @@ RectTransform:
330330
m_LocalPosition: {x: 0, y: 0, z: 0}
331331
m_LocalScale: {x: 0, y: 0, z: 0}
332332
m_Children:
333-
- {fileID: 1780215708}
334333
- {fileID: 506600172}
335334
m_Father: {fileID: 0}
336335
m_RootOrder: 2
@@ -340,74 +339,6 @@ RectTransform:
340339
m_AnchoredPosition: {x: 0, y: 0}
341340
m_SizeDelta: {x: 0, y: 0}
342341
m_Pivot: {x: 0, y: 0}
343-
--- !u!1 &1780215707
344-
GameObject:
345-
m_ObjectHideFlags: 0
346-
m_PrefabParentObject: {fileID: 0}
347-
m_PrefabInternal: {fileID: 0}
348-
serializedVersion: 5
349-
m_Component:
350-
- component: {fileID: 1780215708}
351-
- component: {fileID: 1780215710}
352-
- component: {fileID: 1780215709}
353-
m_Layer: 5
354-
m_Name: Image
355-
m_TagString: Untagged
356-
m_Icon: {fileID: 0}
357-
m_NavMeshLayer: 0
358-
m_StaticEditorFlags: 0
359-
m_IsActive: 1
360-
--- !u!224 &1780215708
361-
RectTransform:
362-
m_ObjectHideFlags: 0
363-
m_PrefabParentObject: {fileID: 0}
364-
m_PrefabInternal: {fileID: 0}
365-
m_GameObject: {fileID: 1780215707}
366-
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
367-
m_LocalPosition: {x: 0, y: 0, z: 0}
368-
m_LocalScale: {x: 1, y: 1, z: 1}
369-
m_Children: []
370-
m_Father: {fileID: 1025881487}
371-
m_RootOrder: 0
372-
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
373-
m_AnchorMin: {x: 0.5, y: 0.5}
374-
m_AnchorMax: {x: 0.5, y: 0.5}
375-
m_AnchoredPosition: {x: 177.66663, y: 0}
376-
m_SizeDelta: {x: 2048, y: 2048}
377-
m_Pivot: {x: 0.5, y: 0.5}
378-
--- !u!114 &1780215709
379-
MonoBehaviour:
380-
m_ObjectHideFlags: 0
381-
m_PrefabParentObject: {fileID: 0}
382-
m_PrefabInternal: {fileID: 0}
383-
m_GameObject: {fileID: 1780215707}
384-
m_Enabled: 1
385-
m_EditorHideFlags: 0
386-
m_Script: {fileID: -765806418, guid: f70555f144d8491a825f0804e09c671c, type: 3}
387-
m_Name:
388-
m_EditorClassIdentifier:
389-
m_Material: {fileID: 0}
390-
m_Color: {r: 1, g: 1, b: 1, a: 1}
391-
m_RaycastTarget: 1
392-
m_OnCullStateChanged:
393-
m_PersistentCalls:
394-
m_Calls: []
395-
m_TypeName: UnityEngine.UI.MaskableGraphic+CullStateChangedEvent, UnityEngine.UI,
396-
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
397-
m_Sprite: {fileID: 0}
398-
m_Type: 0
399-
m_PreserveAspect: 0
400-
m_FillCenter: 1
401-
m_FillMethod: 4
402-
m_FillAmount: 1
403-
m_FillClockwise: 1
404-
m_FillOrigin: 0
405-
--- !u!222 &1780215710
406-
CanvasRenderer:
407-
m_ObjectHideFlags: 0
408-
m_PrefabParentObject: {fileID: 0}
409-
m_PrefabInternal: {fileID: 0}
410-
m_GameObject: {fileID: 1780215707}
411342
--- !u!1 &2034321410
412343
GameObject:
413344
m_ObjectHideFlags: 0
@@ -479,6 +410,7 @@ GameObject:
479410
m_Component:
480411
- component: {fileID: 2090206894}
481412
- component: {fileID: 2090206893}
413+
- component: {fileID: 2090206895}
482414
m_Layer: 0
483415
m_Name: Demo
484416
m_TagString: Untagged
@@ -497,7 +429,6 @@ MonoBehaviour:
497429
m_Script: {fileID: 11500000, guid: 6aa16add9ffd040e6a87e6c59e6fc1da, type: 3}
498430
m_Name:
499431
m_EditorClassIdentifier:
500-
img: {fileID: 1780215709}
501432
anim: {fileID: 506600173}
502433
--- !u!4 &2090206894
503434
Transform:
@@ -512,3 +443,14 @@ Transform:
512443
m_Father: {fileID: 0}
513444
m_RootOrder: 1
514445
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
446+
--- !u!114 &2090206895
447+
MonoBehaviour:
448+
m_ObjectHideFlags: 0
449+
m_PrefabParentObject: {fileID: 0}
450+
m_PrefabInternal: {fileID: 0}
451+
m_GameObject: {fileID: 2090206892}
452+
m_Enabled: 1
453+
m_EditorHideFlags: 0
454+
m_Script: {fileID: 11500000, guid: 74f2e23464b984a9eaaed29b14be63f5, type: 3}
455+
m_Name:
456+
m_EditorClassIdentifier:

Assets/SpriteSheetRuntimeGenerator/Demos/Scripts/LoadTextures.cs

Lines changed: 8 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,116 +1,30 @@
1-
using DaVikingCode.RectanglePacking;
2-
using System.Collections;
1+
using System.Collections;
32
using System.Collections.Generic;
43
using System.IO;
54
using UnityEngine;
65
using UnityEngine.UI;
76

87
public class LoadTextures : MonoBehaviour {
9-
10-
public Image img;
8+
119
public Image anim;
12-
13-
Texture2D mTexture;
14-
Color32[] mFillColor;
15-
16-
private RectanglePacker mPacker;
17-
18-
List<Texture2D> textures = new List<Texture2D>();
19-
List<string> images = new List<string>();
20-
21-
List<Rect> mRectangles = new List<Rect>();
2210

2311
void Start () {
2412

25-
mTexture = new Texture2D(2048, 2048, TextureFormat.ARGB32, false);
26-
27-
mFillColor = mTexture.GetPixels32();
28-
29-
for (int i = 0; i < mFillColor.Length; ++i)
30-
mFillColor[i] = Color.clear;
31-
32-
img.sprite = Sprite.Create(mTexture, new Rect(0, 0, mTexture.width, mTexture.height), Vector2.zero);
33-
3413
CopyPasteFoldersAndPNG(Application.dataPath + "/SpriteSheetRuntimeGenerator/Demos/StreamingAssets", Application.persistentDataPath);
35-
StartCoroutine(LoadAllTextures());
36-
}
37-
38-
IEnumerator LoadAllTextures() {
3914

4015
string[] files = Directory.GetFiles(Application.persistentDataPath + "/Textures", "*.png");
4116

42-
foreach (string file in files) {
43-
44-
WWW loader = new WWW("file:///" + file);
45-
46-
yield return loader;
47-
48-
textures.Add(loader.texture);
49-
images.Add(Path.GetFileNameWithoutExtension(file));
50-
}
51-
52-
createRectangles();
53-
54-
updateRectangles();
55-
}
17+
AssetManager assetManager = GetComponent<AssetManager>();
5618

57-
private void createRectangles() {
19+
assetManager.OnProcessCompleted.AddListener(LaunchAnimations);
5820

59-
for (int i = 0; i < textures.Count; i++)
60-
mRectangles.Add(new Rect(0, 0, textures[i].width, textures[i].height));
21+
assetManager.AddItemsToRaster(files);
22+
assetManager.Process();
6123
}
6224

63-
private void updateRectangles() {
64-
65-
const int padding = 1;
66-
67-
if (mPacker == null)
68-
mPacker = new RectanglePacker(mTexture.width, mTexture.height, padding);
69-
else
70-
mPacker.reset(mTexture.width, mTexture.height, padding);
71-
72-
for (int i = 0; i < textures.Count; i++)
73-
mPacker.insertRectangle((int) mRectangles[i].width, (int) mRectangles[i].height, i);
74-
75-
mPacker.packRectangles();
76-
77-
if (mPacker.rectangleCount > 0) {
78-
79-
mTexture.SetPixels32(mFillColor);
80-
IntegerRectangle rect = new IntegerRectangle();
81-
82-
List<TextureAsset> textureAssets = new List<TextureAsset>();
83-
84-
for (int j = 0; j < mPacker.rectangleCount; j++) {
85-
86-
rect = mPacker.getRectangle(j, rect);
87-
88-
int index = mPacker.getRectangleId(j);
89-
90-
mTexture.SetPixels32(rect.x, rect.y, rect.width, rect.height, textures[index].GetPixels32());
91-
92-
TextureAsset texture = new TextureAsset ();
93-
texture.x = rect.x;
94-
texture.y = rect.y;
95-
texture.width = rect.width;
96-
texture.height = rect.height;
97-
texture.name = images[index];
98-
99-
textureAssets.Add(texture);
100-
}
101-
102-
mTexture.Apply();
103-
104-
Directory.CreateDirectory(Application.persistentDataPath + "/Test/");
105-
106-
byte[] bytes = mTexture.EncodeToPNG();
107-
File.WriteAllBytes(Application.persistentDataPath + "/Test/data.png", bytes);
108-
109-
TextureAssets assets = new TextureAssets(textureAssets.ToArray());
110-
File.WriteAllText(Application.persistentDataPath + "/Test/data.json", JsonUtility.ToJson(assets));
25+
void LaunchAnimations() {
11126

112-
StartCoroutine(LoadAnimation());
113-
}
27+
StartCoroutine(LoadAnimation ());
11428
}
11529

11630
IEnumerator LoadAnimation() {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
public class ItemToRaster {
2+
3+
public string file;
4+
public string id;
5+
6+
public ItemToRaster(string file, string id) {
7+
8+
this.file = file;
9+
this.id = id;
10+
}
11+
}

Assets/SpriteSheetRuntimeGenerator/Scripts/AssetManager/Asset/ItemToRaster.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
using DaVikingCode.RectanglePacking;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.IO;
5+
using UnityEngine;
6+
using UnityEngine.Events;
7+
8+
public class AssetManager : MonoBehaviour {
9+
10+
public UnityEvent OnProcessCompleted;
11+
12+
protected List<ItemToRaster> itemsToRaster = new List<ItemToRaster>();
13+
14+
protected List<Texture2D> textures = new List<Texture2D>();
15+
protected List<string> images = new List<string>();
16+
17+
protected RectanglePacker mPacker;
18+
protected Texture2D mTexture;
19+
20+
protected bool allow4096Textures = false;
21+
22+
public void AddItemToRaster(string file, string customID = null) {
23+
24+
itemsToRaster.Add(new ItemToRaster(file, customID != null ? customID : Path.GetFileNameWithoutExtension(file)));
25+
}
26+
27+
public void AddItemsToRaster(string[] files) {
28+
29+
foreach (string file in files)
30+
AddItemToRaster(file);
31+
}
32+
33+
public void Process(bool allow4096Textures = false) {
34+
35+
this.allow4096Textures = allow4096Textures;
36+
37+
StartCoroutine(process());
38+
}
39+
40+
protected IEnumerator process() {
41+
42+
foreach (ItemToRaster itemToRaster in itemsToRaster) {
43+
44+
WWW loader = new WWW("file:///" + itemToRaster.file);
45+
46+
yield return loader;
47+
48+
textures.Add(loader.texture);
49+
images.Add(itemToRaster.id);
50+
}
51+
52+
List<Rect> mRectangles = new List<Rect>();
53+
for (int i = 0; i < textures.Count; i++)
54+
mRectangles.Add(new Rect(0, 0, textures[i].width, textures[i].height));
55+
56+
const int padding = 1;
57+
58+
int textureSize = allow4096Textures ? 4096 : 2048;
59+
60+
mTexture = new Texture2D(textureSize, textureSize, TextureFormat.ARGB32, false);
61+
62+
Color32[] mFillColor = mTexture.GetPixels32();
63+
for (int i = 0; i < mFillColor.Length; ++i)
64+
mFillColor[i] = Color.clear;
65+
66+
mPacker = new RectanglePacker(mTexture.width, mTexture.height, padding);
67+
for (int i = 0; i < textures.Count; i++)
68+
mPacker.insertRectangle((int) mRectangles[i].width, (int) mRectangles[i].height, i);
69+
70+
mPacker.packRectangles();
71+
72+
if (mPacker.rectangleCount > 0) {
73+
74+
mTexture.SetPixels32(mFillColor);
75+
IntegerRectangle rect = new IntegerRectangle();
76+
77+
List<TextureAsset> textureAssets = new List<TextureAsset>();
78+
79+
for (int j = 0; j < mPacker.rectangleCount; j++) {
80+
81+
rect = mPacker.getRectangle(j, rect);
82+
83+
int index = mPacker.getRectangleId(j);
84+
85+
mTexture.SetPixels32(rect.x, rect.y, rect.width, rect.height, textures[index].GetPixels32());
86+
87+
TextureAsset texture = new TextureAsset();
88+
texture.x = rect.x;
89+
texture.y = rect.y;
90+
texture.width = rect.width;
91+
texture.height = rect.height;
92+
texture.name = images[index];
93+
94+
textureAssets.Add(texture);
95+
}
96+
97+
mTexture.Apply();
98+
99+
Directory.CreateDirectory(Application.persistentDataPath + "/Test/");
100+
101+
byte[] bytes = mTexture.EncodeToPNG();
102+
File.WriteAllBytes(Application.persistentDataPath + "/Test/data.png", bytes);
103+
104+
TextureAssets assets = new TextureAssets(textureAssets.ToArray());
105+
File.WriteAllText(Application.persistentDataPath + "/Test/data.json", JsonUtility.ToJson(assets));
106+
107+
OnProcessCompleted.Invoke();
108+
}
109+
110+
}
111+
}

Assets/SpriteSheetRuntimeGenerator/Scripts/AssetManager/AssetManager.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)