Skip to content

Commit d41bf9d

Browse files
committed
Refactored Sprite Extraction code to include multiple sources
Fixed a bug with extracting individual sprites from multiple sources
1 parent 76cd722 commit d41bf9d

File tree

9 files changed

+341
-206
lines changed

9 files changed

+341
-206
lines changed

Assets/Tools/Artwork/Spritesheet/4096-spritesheet.png.meta

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

Assets/Tools/SpriteExtractor/Editor/SpriteExtractor.cs

Lines changed: 60 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#if UNITY_EDITOR
2-
using System.Collections.Generic;
32
using System.IO;
4-
using System.Linq;
53
using UnityEditor;
64
using UnityEngine;
75
using UnityEditor.U2D.Sprites;
@@ -12,62 +10,88 @@ public static class SpriteExtractor
1210
{
1311
#region METHODS
1412

15-
[MenuItem("Assets/Extract/Sprites/Extract All Sprites", priority = 10)]
16-
public static void ExtractAllSprites()
13+
14+
[MenuItem("Assets/Extract/Sprites/Extract Selected Sprites", priority = 10)]
15+
public static void ExtractSelectedSprites()
1716
{
18-
Object selectedObject = Selection.activeObject;
19-
if (selectedObject is not Texture2D texture2D)
17+
Object[] selectedObjects = Selection.objects;
18+
if (selectedObjects.Length == 0)
2019
{
2120
return;
2221
}
2322

24-
string assetPath = AssetDatabase.GetAssetPath(selectedObject);
25-
string path = Path.GetDirectoryName(assetPath);
26-
string format = Path.GetExtension(assetPath);
27-
28-
ISpriteEditorDataProvider spriteDataProvider = GetSpriteDateProvider(selectedObject);
29-
SpriteRect[] spriteRects = spriteDataProvider.GetSpriteRects();
30-
TextureImporter textureImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter;
23+
TextureImporter textureImporter = null;
3124

32-
if (!textureImporter!.isReadable)
25+
foreach (Object selectedObject in selectedObjects)
3326
{
34-
textureImporter.isReadable = true;
35-
textureImporter.SaveAndReimport();
36-
}
27+
string assetPath = AssetDatabase.GetAssetPath(selectedObjects[0]);
28+
string path = Path.GetDirectoryName(assetPath);
29+
string format = Path.GetExtension(assetPath);
30+
textureImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter;
3731

38-
foreach (SpriteRect spriteRect in spriteRects)
39-
{
40-
Texture2D newTexture = texture2D.GetTextureFromBounds(
41-
(int)spriteRect.rect.x,
42-
(int)spriteRect.rect.y,
43-
(int)spriteRect.rect.width,
44-
(int)spriteRect.rect.height);
32+
if (!textureImporter!.isReadable)
33+
{
34+
textureImporter.isReadable = true;
35+
textureImporter.SaveAndReimport();
36+
}
37+
38+
if (selectedObject is not Sprite sprite)
39+
{
40+
continue;
41+
}
42+
43+
Texture2D newTexture = sprite.texture.GetTextureFromBounds(
44+
(int)sprite.rect.x,
45+
(int)sprite.rect.y,
46+
(int)sprite.rect.width,
47+
(int)sprite.rect.height);
4548
newTexture.filterMode = textureImporter.filterMode;
4649
newTexture.Apply();
4750
byte[] pngTexture = newTexture.EncodeTexture2D(format);
48-
File.WriteAllBytes($"{Path.Combine(path!, spriteRect.name)}{format}", pngTexture);
51+
File.WriteAllBytes($"{Path.Combine(path!, sprite.name)}{format}", pngTexture);
52+
}
53+
54+
if (textureImporter)
55+
{
56+
textureImporter.isReadable = false;
57+
textureImporter.SaveAndReimport();
4958
}
5059

51-
textureImporter.isReadable = false;
52-
textureImporter.SaveAndReimport();
5360
AssetDatabase.Refresh();
5461
}
5562

56-
[MenuItem("Assets/Extract/Sprites/Extract Selected Sprites", priority = 10)]
57-
public static void ExtractSprites()
63+
[MenuItem("Assets/Extract/Sprites/Extract All", priority = 10)]
64+
public static void ExtractAllFromSelectedSpritesheets()
5865
{
5966
Object[] selectedObjects = Selection.objects;
6067
if (selectedObjects.Length == 0)
6168
{
6269
return;
6370
}
6471

65-
string assetPath = AssetDatabase.GetAssetPath(selectedObjects[0]);
72+
foreach (Object selectedObject in selectedObjects)
73+
{
74+
ExtractAllSpritesHelper(selectedObject);
75+
}
76+
}
77+
78+
#endregion
79+
80+
#region HELPER METHODS
81+
82+
private static void ExtractAllSpritesHelper(Object sourceObject)
83+
{
84+
if (sourceObject is not Texture2D texture2D)
85+
{
86+
return;
87+
}
88+
89+
string assetPath = AssetDatabase.GetAssetPath(sourceObject);
6690
string path = Path.GetDirectoryName(assetPath);
6791
string format = Path.GetExtension(assetPath);
6892

69-
ISpriteEditorDataProvider spriteDataProvider = GetSpriteDateProvider(selectedObjects[0]);
70-
List<SpriteRect> spriteRects = spriteDataProvider.GetSpriteRects().ToList();
93+
ISpriteEditorDataProvider spriteDataProvider = GetSpriteDateProvider(sourceObject);
94+
SpriteRect[] spriteRects = spriteDataProvider.GetSpriteRects();
7195
TextureImporter textureImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter;
7296

7397
if (!textureImporter!.isReadable)
@@ -76,15 +100,9 @@ public static void ExtractSprites()
76100
textureImporter.SaveAndReimport();
77101
}
78102

79-
foreach (Object selectedObject in selectedObjects)
103+
foreach (SpriteRect spriteRect in spriteRects)
80104
{
81-
if (selectedObject is not Sprite sprite)
82-
{
83-
continue;
84-
}
85-
86-
SpriteRect spriteRect = spriteRects.Find(spriteRect => string.CompareOrdinal(spriteRect.name, sprite.name) == 0);
87-
Texture2D newTexture = sprite.texture.GetTextureFromBounds(
105+
Texture2D newTexture = texture2D.GetTextureFromBounds(
88106
(int)spriteRect.rect.x,
89107
(int)spriteRect.rect.y,
90108
(int)spriteRect.rect.width,
@@ -100,15 +118,12 @@ public static void ExtractSprites()
100118
AssetDatabase.Refresh();
101119
}
102120

103-
#endregion
104-
105-
#region HELPER METHODS
106-
107121
private static ISpriteEditorDataProvider GetSpriteDateProvider(Object selectedObject)
108122
{
109123
SpriteDataProviderFactories factory = new SpriteDataProviderFactories();
110124
factory.Init();
111-
ISpriteEditorDataProvider spriteDataProvider = factory.GetSpriteEditorDataProviderFromObject(selectedObject);
125+
ISpriteEditorDataProvider spriteDataProvider =
126+
factory.GetSpriteEditorDataProviderFromObject(selectedObject);
112127
spriteDataProvider.InitSpriteEditorDataProvider();
113128
return spriteDataProvider;
114129
}

Packages/manifest.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
22
"dependencies": {
33
"com.unity.2d.sprite": "1.0.0",
4-
"com.unity.ai.navigation": "1.1.4",
5-
"com.unity.ide.rider": "3.0.25",
6-
"com.unity.ide.visualstudio": "2.0.21",
4+
"com.unity.ai.navigation": "2.0.0",
5+
"com.unity.ide.rider": "3.0.28",
6+
"com.unity.ide.visualstudio": "2.0.22",
77
"com.unity.test-framework": "1.3.9",
8-
"com.unity.textmeshpro": "3.0.6",
9-
"com.unity.timeline": "1.8.2",
10-
"com.unity.ugui": "1.0.0",
8+
"com.unity.timeline": "1.8.6",
9+
"com.unity.ugui": "2.0.0",
10+
"com.unity.modules.accessibility": "1.0.0",
1111
"com.unity.modules.ai": "1.0.0",
1212
"com.unity.modules.androidjni": "1.0.0",
1313
"com.unity.modules.animation": "1.0.0",

Packages/packages-lock.json

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"dependencies": {}
88
},
99
"com.unity.ai.navigation": {
10-
"version": "1.1.4",
10+
"version": "2.0.0",
1111
"depth": 0,
1212
"source": "registry",
1313
"dependencies": {
@@ -16,14 +16,14 @@
1616
"url": "https://packages.unity.com"
1717
},
1818
"com.unity.ext.nunit": {
19-
"version": "2.0.3",
19+
"version": "2.0.5",
2020
"depth": 1,
2121
"source": "registry",
2222
"dependencies": {},
2323
"url": "https://packages.unity.com"
2424
},
2525
"com.unity.ide.rider": {
26-
"version": "3.0.25",
26+
"version": "3.0.28",
2727
"depth": 0,
2828
"source": "registry",
2929
"dependencies": {
@@ -32,7 +32,7 @@
3232
"url": "https://packages.unity.com"
3333
},
3434
"com.unity.ide.visualstudio": {
35-
"version": "2.0.21",
35+
"version": "2.0.22",
3636
"depth": 0,
3737
"source": "registry",
3838
"dependencies": {
@@ -51,17 +51,8 @@
5151
},
5252
"url": "https://packages.unity.com"
5353
},
54-
"com.unity.textmeshpro": {
55-
"version": "3.0.6",
56-
"depth": 0,
57-
"source": "registry",
58-
"dependencies": {
59-
"com.unity.ugui": "1.0.0"
60-
},
61-
"url": "https://packages.unity.com"
62-
},
6354
"com.unity.timeline": {
64-
"version": "1.8.2",
55+
"version": "1.8.6",
6556
"depth": 0,
6657
"source": "registry",
6758
"dependencies": {
@@ -73,14 +64,20 @@
7364
"url": "https://packages.unity.com"
7465
},
7566
"com.unity.ugui": {
76-
"version": "1.0.0",
67+
"version": "2.0.0",
7768
"depth": 0,
7869
"source": "builtin",
7970
"dependencies": {
8071
"com.unity.modules.ui": "1.0.0",
8172
"com.unity.modules.imgui": "1.0.0"
8273
}
8374
},
75+
"com.unity.modules.accessibility": {
76+
"version": "1.0.0",
77+
"depth": 0,
78+
"source": "builtin",
79+
"dependencies": {}
80+
},
8481
"com.unity.modules.ai": {
8582
"version": "1.0.0",
8683
"depth": 0,
@@ -128,6 +125,12 @@
128125
"com.unity.modules.animation": "1.0.0"
129126
}
130127
},
128+
"com.unity.modules.hierarchycore": {
129+
"version": "1.0.0",
130+
"depth": 1,
131+
"source": "builtin",
132+
"dependencies": {}
133+
},
131134
"com.unity.modules.imageconversion": {
132135
"version": "1.0.0",
133136
"depth": 0,
@@ -216,7 +219,8 @@
216219
"dependencies": {
217220
"com.unity.modules.ui": "1.0.0",
218221
"com.unity.modules.imgui": "1.0.0",
219-
"com.unity.modules.jsonserialize": "1.0.0"
222+
"com.unity.modules.jsonserialize": "1.0.0",
223+
"com.unity.modules.hierarchycore": "1.0.0"
220224
}
221225
},
222226
"com.unity.modules.umbra": {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
%YAML 1.1
2+
%TAG !u! tag:unity3d.com,2011:
3+
--- !u!655991488 &1
4+
MultiplayerManager:
5+
m_ObjectHideFlags: 0
6+
m_EnableMultiplayerRoles: 0
7+
m_ActiveMultiplayerRole: 0

ProjectSettings/ProjectVersion.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
m_EditorVersion: 2023.1.16f1
2-
m_EditorVersionWithRevision: 2023.1.16f1 (e5ad54273a6f)
1+
m_EditorVersion: 2023.2.20f1
2+
m_EditorVersionWithRevision: 2023.2.20f1 (0e25a174756c)

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Lazy Sprite Extractor
22

3-
The Sprite Extractor Tool allows you to extract individual sprites from a larger sprite sheet.
3+
The Sprite Extractor Tool allows you to extract individual or all the sprites from one or more larger spritesheets.
44

55
This version of the tool has been updated to use ISpriteEditorDataProvider to extract sprites from a spritesheet.
66
I have included the old version of the tool in the repository, but it is no longer supported in newer versions of Unity. <br>

0 commit comments

Comments
 (0)