Skip to content

Commit 2dc03fd

Browse files
committed
Can load correct texture from sprite!
1 parent 2b5dd10 commit 2dc03fd

File tree

3 files changed

+104
-2
lines changed

3 files changed

+104
-2
lines changed

Assets/SpriteSheetRuntimeGenerator/Demos/Scenes/LoadTextures.unity

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,74 @@ Transform:
178178
m_Father: {fileID: 0}
179179
m_RootOrder: 0
180180
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
181+
--- !u!1 &506600171
182+
GameObject:
183+
m_ObjectHideFlags: 0
184+
m_PrefabParentObject: {fileID: 0}
185+
m_PrefabInternal: {fileID: 0}
186+
serializedVersion: 5
187+
m_Component:
188+
- component: {fileID: 506600172}
189+
- component: {fileID: 506600174}
190+
- component: {fileID: 506600173}
191+
m_Layer: 5
192+
m_Name: Animation
193+
m_TagString: Untagged
194+
m_Icon: {fileID: 0}
195+
m_NavMeshLayer: 0
196+
m_StaticEditorFlags: 0
197+
m_IsActive: 1
198+
--- !u!224 &506600172
199+
RectTransform:
200+
m_ObjectHideFlags: 0
201+
m_PrefabParentObject: {fileID: 0}
202+
m_PrefabInternal: {fileID: 0}
203+
m_GameObject: {fileID: 506600171}
204+
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
205+
m_LocalPosition: {x: 0, y: 0, z: 0}
206+
m_LocalScale: {x: 1, y: 1, z: 1}
207+
m_Children: []
208+
m_Father: {fileID: 1025881487}
209+
m_RootOrder: 1
210+
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
211+
m_AnchorMin: {x: 0.5, y: 0.5}
212+
m_AnchorMax: {x: 0.5, y: 0.5}
213+
m_AnchoredPosition: {x: 0, y: 0}
214+
m_SizeDelta: {x: 286, y: 387}
215+
m_Pivot: {x: 0.5, y: 0.5}
216+
--- !u!114 &506600173
217+
MonoBehaviour:
218+
m_ObjectHideFlags: 0
219+
m_PrefabParentObject: {fileID: 0}
220+
m_PrefabInternal: {fileID: 0}
221+
m_GameObject: {fileID: 506600171}
222+
m_Enabled: 1
223+
m_EditorHideFlags: 0
224+
m_Script: {fileID: -765806418, guid: f70555f144d8491a825f0804e09c671c, type: 3}
225+
m_Name:
226+
m_EditorClassIdentifier:
227+
m_Material: {fileID: 0}
228+
m_Color: {r: 1, g: 1, b: 1, a: 1}
229+
m_RaycastTarget: 1
230+
m_OnCullStateChanged:
231+
m_PersistentCalls:
232+
m_Calls: []
233+
m_TypeName: UnityEngine.UI.MaskableGraphic+CullStateChangedEvent, UnityEngine.UI,
234+
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
235+
m_Sprite: {fileID: 0}
236+
m_Type: 0
237+
m_PreserveAspect: 1
238+
m_FillCenter: 1
239+
m_FillMethod: 4
240+
m_FillAmount: 1
241+
m_FillClockwise: 1
242+
m_FillOrigin: 0
243+
--- !u!222 &506600174
244+
CanvasRenderer:
245+
m_ObjectHideFlags: 0
246+
m_PrefabParentObject: {fileID: 0}
247+
m_PrefabInternal: {fileID: 0}
248+
m_GameObject: {fileID: 506600171}
181249
--- !u!1 &1025881483
182250
GameObject:
183251
m_ObjectHideFlags: 0
@@ -263,6 +331,7 @@ RectTransform:
263331
m_LocalScale: {x: 0, y: 0, z: 0}
264332
m_Children:
265333
- {fileID: 1780215708}
334+
- {fileID: 506600172}
266335
m_Father: {fileID: 0}
267336
m_RootOrder: 2
268337
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
@@ -429,6 +498,7 @@ MonoBehaviour:
429498
m_Name:
430499
m_EditorClassIdentifier:
431500
img: {fileID: 1780215709}
501+
anim: {fileID: 506600173}
432502
--- !u!4 &2090206894
433503
Transform:
434504
m_ObjectHideFlags: 0

Assets/SpriteSheetRuntimeGenerator/Demos/Scripts/LoadTextures.cs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88
public class LoadTextures : MonoBehaviour {
99

1010
public Image img;
11+
public Image anim;
1112

1213
Texture2D mTexture;
1314
Color32[] mFillColor;
1415

1516
private RectanglePacker mPacker;
1617

1718
List<Texture2D> textures = new List<Texture2D>();
19+
List<string> images = new List<string>();
1820

1921
List<Rect> mRectangles = new List<Rect>();
2022

@@ -44,6 +46,7 @@ IEnumerator LoadAllTextures() {
4446
yield return loader;
4547

4648
textures.Add(loader.texture);
49+
images.Add(Path.GetFileNameWithoutExtension(file));
4750
}
4851

4952
createRectangles();
@@ -91,7 +94,7 @@ private void updateRectangles() {
9194
texture.y = rect.y;
9295
texture.width = rect.width;
9396
texture.height = rect.height;
94-
texture.id = index;
97+
texture.name = images[index];
9598

9699
textureAssets.Add(texture);
97100
}
@@ -105,9 +108,38 @@ private void updateRectangles() {
105108

106109
TextureAssets assets = new TextureAssets(textureAssets.ToArray());
107110
File.WriteAllText(Application.persistentDataPath + "/Test/data.json", JsonUtility.ToJson(assets));
111+
112+
StartCoroutine(LoadAnimation());
108113
}
109114
}
110115

116+
IEnumerator LoadAnimation() {
117+
118+
WWW loaderTexture = new WWW("file:///" + Application.persistentDataPath + "/Test/data.png");
119+
yield return loaderTexture;
120+
121+
WWW loaderJSON = new WWW("file:///" + Application.persistentDataPath + "/Test/data.json");
122+
yield return loaderJSON;
123+
124+
TextureAssets textureAssets = JsonUtility.FromJson<TextureAssets>(loaderJSON.text);
125+
126+
Dictionary<string, TextureAsset> assets = new Dictionary<string, TextureAsset>();
127+
foreach (TextureAsset textureAsset in textureAssets.assets)
128+
assets.Add(textureAsset.name, textureAsset);
129+
130+
TextureAsset asset = null;
131+
assets.TryGetValue("walking0004", out asset);
132+
133+
Color[] colors = loaderTexture.texture.GetPixels(asset.x, asset.y, asset.width, asset.height);
134+
135+
Texture2D texture = new Texture2D(asset.width, asset.height, TextureFormat.ARGB32, false);
136+
137+
anim.sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
138+
139+
anim.sprite.texture.SetPixels(colors);
140+
141+
}
142+
111143
void CopyPasteFoldersAndPNG(string SourcePath, string DestinationPath) {
112144

113145
foreach (string dirPath in Directory.GetDirectories(SourcePath, "*", SearchOption.AllDirectories))

Assets/SpriteSheetRuntimeGenerator/Scripts/AssetManager/Asset/TextureAsset.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ public class TextureAsset {
1818
public int y;
1919
public int width;
2020
public int height;
21-
public int id;
21+
public string name;
2222
}

0 commit comments

Comments
 (0)