@@ -40,7 +40,6 @@ protected IEnumerator process() {
40
40
41
41
List < Texture2D > textures = new List < Texture2D > ( ) ;
42
42
List < string > images = new List < string > ( ) ;
43
- List < TextureAsset > textureAssets = new List < TextureAsset > ( ) ;
44
43
45
44
foreach ( ItemToRaster itemToRaster in itemsToRaster ) {
46
45
@@ -66,56 +65,76 @@ protected IEnumerator process() {
66
65
for ( int i = 0 ; i < mFillColor . Length ; ++ i )
67
66
mFillColor [ i ] = Color . clear ;
68
67
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 ) {
72
70
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 ) ;
74
74
75
- if ( mPacker . rectangleCount > 0 ) {
75
+ mPacker . packRectangles ( ) ;
76
76
77
- mTexture . SetPixels32 ( mFillColor ) ;
78
- IntegerRectangle rect = new IntegerRectangle ( ) ;
79
- textureAssets = new List < TextureAsset > ( ) ;
77
+ if ( mPacker . rectangleCount > 0 ) {
80
78
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 > ( ) ;
82
82
83
- rect = mPacker . getRectangle ( j , rect ) ;
83
+ List < Rect > rectGarbages = new List < Rect > ( ) ;
84
84
85
- int index = mPacker . getRectangleId ( j ) ;
85
+ for ( int j = 0 ; j < mPacker . rectangleCount ; j ++ ) {
86
86
87
- mTexture . SetPixels32 ( rect . x , rect . y , rect . width , rect . height , textures [ index ] . GetPixels32 ( ) ) ;
87
+ rect = mPacker . getRectangle ( j , rect ) ;
88
88
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 ) ;
95
90
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 ) {
98
106
99
- mTexture . Apply ( ) ;
107
+ int indexToDestroy = mRectangles . IndexOf ( rectGarbage ) ;
100
108
101
- Directory . CreateDirectory ( Application . persistentDataPath + "/Test/" ) ;
109
+ mRectangles . RemoveAt ( indexToDestroy ) ;
110
+ textures . RemoveAt ( indexToDestroy ) ;
111
+ images . RemoveAt ( indexToDestroy ) ;
112
+ }
102
113
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 ( ) ;
105
115
106
- /*WWW loaderTexture = new WWW("file:///" + Application.persistentDataPath + "/Test/data.png");
107
- yield return loaderTexture;
116
+ Directory . CreateDirectory ( Application . persistentDataPath + "/Test/" ) ;
108
117
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 ;
111
121
112
- TextureAssets textureAssets = JsonUtility.FromJson<TextureAssets>(loaderJSON.text);*/
122
+ /*WWW loaderTexture = new WWW("file:///" + Application.persistentDataPath + "/Test/data.png");
123
+ yield return loaderTexture;
113
124
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
+ }
116
134
117
- OnProcessCompleted . Invoke ( ) ;
118
135
}
136
+
137
+ OnProcessCompleted . Invoke ( ) ;
119
138
}
120
139
121
140
public Sprite GetSprite ( string id ) {
0 commit comments