Skip to content

Commit 42a2cfb

Browse files
committedAug 15, 2019
finally got exporter working
1 parent 34b03f4 commit 42a2cfb

File tree

4 files changed

+73
-12
lines changed

4 files changed

+73
-12
lines changed
 

‎Assets/Scripts/editor/SpriteTextureSliceExporter.cs

+62-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.Collections.Generic;
1111
using System.IO;
1212
using System.Text;
13+
using System.Linq;
1314
using UnityEditor;
1415
using UnityEngine;
1516

@@ -175,7 +176,7 @@ public static void SpriteSheetExporter() {
175176
} // if (selectedTexture != null)
176177
} // foreach (Object activeObject in selectedTextures)
177178
}
178-
179+
179180
[MenuItem("SpriteTextureSliceExporter/ImageSliceExporter")]
180181
public static void ImageSliceExporter() {
181182
var selectedTextures = Selection.GetFiltered(typeof(Texture2D), SelectionMode.Assets);
@@ -278,4 +279,64 @@ internal struct SheetFrame
278279
public Vector2 pivot;
279280
public Rect rect;
280281
}
282+
283+
284+
[MenuItem("SpriteTextureSliceExporter/Export Slices")]
285+
public static void ExportSlices() {
286+
// var selectedTextures = Selection.GetFiltered(typeof(Texture2D), SelectionMode.Assets);
287+
// if (selectedTextures.Length == 0) {
288+
// EditorUtility.DisplayDialog("ImageSliceExporter", "Please select texture", "OK");
289+
// return;
290+
// }
291+
var outputDirectory = GetOutputDirectory();
292+
// var outputDirectory = Application.persistentDataPath;
293+
294+
var assetPaths = Selection.assetGUIDs
295+
.Select(AssetDatabase.GUIDToAssetPath)
296+
// .Select(path => Path.ChangeExtension(path, null))
297+
.Select(AssetDatabase.LoadAllAssetsAtPath)
298+
.ToArray();
299+
Debug.Log($"ExportSlicesContext, len={assetPaths.Length} {string.Join(System.Environment.NewLine, (object[])assetPaths)}");
300+
foreach (var subassets in assetPaths) {
301+
Debug.Log($"ExportSlicesContext, subasset len={subassets.Length} {string.Join(System.Environment.NewLine, (object[])subassets)}");
302+
var sprites = subassets
303+
.Where(x => x is Sprite)
304+
.Cast<Sprite>()
305+
.ToArray();
306+
foreach (var sprite in sprites) {
307+
Debug.Log($"ExportSlicesContext inner loop, {sprite.GetType()} = {sprite}");
308+
var tex = sprite.texture;
309+
var r = sprite.textureRect;
310+
var subtex = tex.CropTexture( (int)r.x, (int)r.y, (int)r.width, (int)r.height );
311+
var data = subtex.EncodeToPNG();
312+
var outPath = $"{outputDirectory}/{sprite.name}.png";
313+
File.WriteAllBytes(outPath, data);
314+
Debug.Log($"Wrote to '{outPath}'");
315+
}
316+
}
317+
// foreach (var texture in selectedTextures) {
318+
// var texture = Resources.Load<Texture2D>(assetPath);
319+
// Debug.Log($"ExportSlicesContext tex= {texture}");
320+
// var sprites = Resources.LoadAll<Sprite>(texture.name);
321+
// Debug.Log($"ExportSlicesContext sprites= {string.Join(System.Environment.NewLine, (object[])sprites)} length={sprites.Length}");
322+
// foreach (Sprite sprite in sprites) {
323+
// Debug.Log($"ExportSlicesContext inner loop, {sprite}");
324+
// var tex = sprite.texture;
325+
// var r = sprite.textureRect;
326+
// var subtex = tex.CropTexture( (int)r.x, (int)r.y, (int)r.width, (int)r.height );
327+
// var data = subtex.EncodeToPNG();
328+
// var outPath = $"{outputDirectory}/{sprite.name}.png";
329+
// File.WriteAllBytes(outPath, data);
330+
// Debug.Log($"Wrote to '{outPath}'");
331+
// }
332+
// }
333+
// var selectedTexture = Selection.activeObject as Texture2D;
334+
// selectedTexture.
335+
}
336+
337+
[MenuItem("SpriteTextureSliceExporter/Export Slices", true)]
338+
public static bool ExportSlicesValidation() {
339+
return Selection.activeObject as Texture2D != null;
340+
}
341+
281342
}
+11-11
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
%YAML 1.1
2-
%TAG !u! tag:unity3d.com,2011:
3-
--- !u!1045 &1
4-
EditorBuildSettings:
5-
m_ObjectHideFlags: 0
6-
serializedVersion: 2
7-
m_Scenes:
8-
- enabled: 1
9-
path: Assets/Scenes/SampleScene.unity
10-
guid: 2cda990e2423bbf4892e6590ba056729
11-
m_configObjects: {}
1+
%YAML 1.1
2+
%TAG !u! tag:unity3d.com,2011:
3+
--- !u!1045 &1
4+
EditorBuildSettings:
5+
m_ObjectHideFlags: 0
6+
serializedVersion: 2
7+
m_Scenes:
8+
- enabled: 0
9+
path:
10+
guid: 00000000000000000000000000000000
11+
m_configObjects: {}

0 commit comments

Comments
 (0)
Please sign in to comment.