Skip to content

Commit cd451c6

Browse files
committed
Loading via WWW
1 parent adfcd38 commit cd451c6

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

Assets/SpriteSheetRuntimeGenerator/Demos/Scripts/LoadTextures.cs

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using DaVikingCode.RectanglePacking;
2+
using System.Collections;
23
using System.Collections.Generic;
34
using System.IO;
45
using UnityEngine;
@@ -13,7 +14,7 @@ public class LoadTextures : MonoBehaviour {
1314

1415
private RectanglePacker mPacker;
1516

16-
Texture2D[] textures;
17+
List<Texture2D> textures = new List<Texture2D>();
1718

1819
List<Rect> mRectangles = new List<Rect>();
1920

@@ -27,10 +28,23 @@ void Start () {
2728
mFillColor[i] = Color.clear;
2829

2930
img.sprite = Sprite.Create(mTexture, new Rect(0, 0, mTexture.width, mTexture.height), Vector2.zero);
30-
31-
//textures = Resources.LoadAll<Texture2D> ("Textures/");
3231

33-
CopyPasteFromStreamingAssetsToPersistentDataPath(Application.dataPath + "/SpriteSheetRuntimeGenerator/Demos/StreamingAssets", Application.persistentDataPath);
32+
CopyPasteFoldersAndPNG(Application.dataPath + "/SpriteSheetRuntimeGenerator/Demos/StreamingAssets", Application.persistentDataPath);
33+
StartCoroutine(LoadAllTextures());
34+
}
35+
36+
IEnumerator LoadAllTextures() {
37+
38+
string[] files = Directory.GetFiles(Application.persistentDataPath + "/Textures", "*.png");
39+
40+
foreach (string file in files) {
41+
42+
WWW loader = new WWW("file:///" + file);
43+
44+
yield return loader;
45+
46+
textures.Add(loader.texture);
47+
}
3448

3549
createRectangles();
3650

@@ -39,15 +53,8 @@ void Start () {
3953

4054
private void createRectangles() {
4155

42-
int width;
43-
int height;
44-
45-
for (int i = 0; i < textures.Length; i++) {
46-
47-
width = textures[i].width;
48-
height = textures[i].height;
49-
mRectangles.Add(new Rect(0, 0, width, height));
50-
}
56+
for (int i = 0; i < textures.Count; i++)
57+
mRectangles.Add(new Rect(0, 0, textures[i].width, textures[i].height));
5158
}
5259

5360
private void updateRectangles() {
@@ -59,7 +66,7 @@ private void updateRectangles() {
5966
else
6067
mPacker.reset(mTexture.width, mTexture.height, padding);
6168

62-
for (int i = 0; i < textures.Length; i++)
69+
for (int i = 0; i < textures.Count; i++)
6370
mPacker.insertRectangle((int) mRectangles[i].width, (int) mRectangles[i].height, i);
6471

6572
mPacker.packRectangles();
@@ -75,10 +82,10 @@ private void updateRectangles() {
7582
rect = mPacker.getRectangle(j, rect);
7683

7784
int size = rect.width * rect.height;
85+
7886
size -= 4;
7987

8088
int index = mPacker.getRectangleId(j);
81-
Debug.Log (index);
8289

8390
mTexture.SetPixels32(rect.x + 1, rect.y + 1, rect.width - 2, rect.height - 2, textures[index].GetPixels32());
8491

@@ -88,7 +95,7 @@ private void updateRectangles() {
8895
}
8996
}
9097

91-
void CopyPasteFromStreamingAssetsToPersistentDataPath(string SourcePath, string DestinationPath) {
98+
void CopyPasteFoldersAndPNG(string SourcePath, string DestinationPath) {
9299

93100
foreach (string dirPath in Directory.GetDirectories(SourcePath, "*", SearchOption.AllDirectories))
94101
Directory.CreateDirectory(dirPath.Replace(SourcePath, DestinationPath));

ProjectSettings/EditorBuildSettings.asset

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ EditorBuildSettings:
66
serializedVersion: 2
77
m_Scenes:
88
- enabled: 1
9-
path: Assets/Main.unity
9+
path: Assets/SpriteSheetRuntimeGenerator/Demos/Scenes/RectanglePacking.unity

0 commit comments

Comments
 (0)