1
1
using DaVikingCode . RectanglePacking ;
2
+ using System . Collections ;
2
3
using System . Collections . Generic ;
3
4
using System . IO ;
4
5
using UnityEngine ;
@@ -13,7 +14,7 @@ public class LoadTextures : MonoBehaviour {
13
14
14
15
private RectanglePacker mPacker ;
15
16
16
- Texture2D [ ] textures ;
17
+ List < Texture2D > textures = new List < Texture2D > ( ) ;
17
18
18
19
List < Rect > mRectangles = new List < Rect > ( ) ;
19
20
@@ -27,10 +28,23 @@ void Start () {
27
28
mFillColor [ i ] = Color . clear ;
28
29
29
30
img . sprite = Sprite . Create ( mTexture , new Rect ( 0 , 0 , mTexture . width , mTexture . height ) , Vector2 . zero ) ;
30
-
31
- //textures = Resources.LoadAll<Texture2D> ("Textures/");
32
31
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
+ }
34
48
35
49
createRectangles ( ) ;
36
50
@@ -39,15 +53,8 @@ void Start () {
39
53
40
54
private void createRectangles ( ) {
41
55
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 ) ) ;
51
58
}
52
59
53
60
private void updateRectangles ( ) {
@@ -59,7 +66,7 @@ private void updateRectangles() {
59
66
else
60
67
mPacker . reset ( mTexture . width , mTexture . height , padding ) ;
61
68
62
- for ( int i = 0 ; i < textures . Length ; i ++ )
69
+ for ( int i = 0 ; i < textures . Count ; i ++ )
63
70
mPacker . insertRectangle ( ( int ) mRectangles [ i ] . width , ( int ) mRectangles [ i ] . height , i ) ;
64
71
65
72
mPacker . packRectangles ( ) ;
@@ -75,10 +82,10 @@ private void updateRectangles() {
75
82
rect = mPacker . getRectangle ( j , rect ) ;
76
83
77
84
int size = rect . width * rect . height ;
85
+
78
86
size -= 4 ;
79
87
80
88
int index = mPacker . getRectangleId ( j ) ;
81
- Debug . Log ( index ) ;
82
89
83
90
mTexture . SetPixels32 ( rect . x + 1 , rect . y + 1 , rect . width - 2 , rect . height - 2 , textures [ index ] . GetPixels32 ( ) ) ;
84
91
@@ -88,7 +95,7 @@ private void updateRectangles() {
88
95
}
89
96
}
90
97
91
- void CopyPasteFromStreamingAssetsToPersistentDataPath ( string SourcePath , string DestinationPath ) {
98
+ void CopyPasteFoldersAndPNG ( string SourcePath , string DestinationPath ) {
92
99
93
100
foreach ( string dirPath in Directory . GetDirectories ( SourcePath , "*" , SearchOption . AllDirectories ) )
94
101
Directory . CreateDirectory ( dirPath . Replace ( SourcePath , DestinationPath ) ) ;
0 commit comments