您可以在 Unity 中导出切片精灵(PNG 图像)吗?
Can you export sliced sprites (PNG images) in Unity?
所以我有一个 Sprite 2d 和 UI 统一。 (PNG 图像) 包含许多小图像,如按钮。所以我将它们切片,然后我可以单独统一使用它们。但问题是我想导出我在切片时获得的每个 png 图像以供其他用途。那我可以这样做吗?并将它们作为单独的 png?
我想要的是这些图片:
将它们作为单独的 png 导出到我的(比方说)桌面。
谢谢!
使用 Photoshop,对其进行裁剪,将进行大量剪切和粘贴,另存为 PNG。
将其导入到文件夹内统一,我喜欢你的设计。也可以在 Youtube 上观看 Brackeys 教程,https://www.youtube.com/user/Brackeys,他将我从学生时代救了出来。
(编辑:我推荐 Photoshop,因为它是我唯一知道的。)
我也有这个需求,最后我用一个简单的编辑器脚本解决了它:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class ExportSubSprites : Editor {
[MenuItem("Assets/Export Sub-Sprites")]
public static void DoExportSubSprites() {
var folder = EditorUtility.OpenFolderPanel("Export subsprites into what folder?", "", "");
foreach (var obj in Selection.objects) {
var sprite = obj as Sprite;
if (sprite == null) continue;
var extracted = ExtractAndName(sprite);
SaveSubSprite(extracted, folder);
}
}
[MenuItem("Assets/Export Sub-Sprites", true)]
private static bool CanExportSubSprites()
{
return Selection.activeObject is Sprite;
}
// Since a sprite may exist anywhere on a tex2d, this will crop out the sprite's claimed region and return a new, cropped, tex2d.
private static Texture2D ExtractAndName(Sprite sprite) {
var output = new Texture2D((int)sprite.rect.width, (int)sprite.rect.height);
var r = sprite.textureRect;
var pixels = sprite.texture.GetPixels((int)r.x, (int)r.y, (int)r.width, (int)r.height);
output.SetPixels(pixels);
output.Apply();
output.name = sprite.texture.name + " " + sprite.name;
return output;
}
private static void SaveSubSprite(Texture2D tex, string saveToDirectory) {
if (!System.IO.Directory.Exists(saveToDirectory)) System.IO.Directory.CreateDirectory(saveToDirectory);
System.IO.File.WriteAllBytes(System.IO.Path.Combine(saveToDirectory, tex.name + ".png"), tex.EncodeToPNG());
}
}
首先,将其放入名为 EditorSubSprites.cs
的脚本文件中,并确保它位于 Editor 文件夹中。如果您没有编辑器文件夹,您只需在 /Assets/Editor/
处创建
要使用,请展开纹理资产的 sprite,然后 select 尽可能多地导出您想要导出的 sprite。右键单击并 select“导出子精灵”。
所以我有一个 Sprite 2d 和 UI 统一。 (PNG 图像) 包含许多小图像,如按钮。所以我将它们切片,然后我可以单独统一使用它们。但问题是我想导出我在切片时获得的每个 png 图像以供其他用途。那我可以这样做吗?并将它们作为单独的 png?
我想要的是这些图片:
将它们作为单独的 png 导出到我的(比方说)桌面。
谢谢!
使用 Photoshop,对其进行裁剪,将进行大量剪切和粘贴,另存为 PNG。 将其导入到文件夹内统一,我喜欢你的设计。也可以在 Youtube 上观看 Brackeys 教程,https://www.youtube.com/user/Brackeys,他将我从学生时代救了出来。
(编辑:我推荐 Photoshop,因为它是我唯一知道的。)
我也有这个需求,最后我用一个简单的编辑器脚本解决了它:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class ExportSubSprites : Editor {
[MenuItem("Assets/Export Sub-Sprites")]
public static void DoExportSubSprites() {
var folder = EditorUtility.OpenFolderPanel("Export subsprites into what folder?", "", "");
foreach (var obj in Selection.objects) {
var sprite = obj as Sprite;
if (sprite == null) continue;
var extracted = ExtractAndName(sprite);
SaveSubSprite(extracted, folder);
}
}
[MenuItem("Assets/Export Sub-Sprites", true)]
private static bool CanExportSubSprites()
{
return Selection.activeObject is Sprite;
}
// Since a sprite may exist anywhere on a tex2d, this will crop out the sprite's claimed region and return a new, cropped, tex2d.
private static Texture2D ExtractAndName(Sprite sprite) {
var output = new Texture2D((int)sprite.rect.width, (int)sprite.rect.height);
var r = sprite.textureRect;
var pixels = sprite.texture.GetPixels((int)r.x, (int)r.y, (int)r.width, (int)r.height);
output.SetPixels(pixels);
output.Apply();
output.name = sprite.texture.name + " " + sprite.name;
return output;
}
private static void SaveSubSprite(Texture2D tex, string saveToDirectory) {
if (!System.IO.Directory.Exists(saveToDirectory)) System.IO.Directory.CreateDirectory(saveToDirectory);
System.IO.File.WriteAllBytes(System.IO.Path.Combine(saveToDirectory, tex.name + ".png"), tex.EncodeToPNG());
}
}
首先,将其放入名为 EditorSubSprites.cs
的脚本文件中,并确保它位于 Editor 文件夹中。如果您没有编辑器文件夹,您只需在 /Assets/Editor/
要使用,请展开纹理资产的 sprite,然后 select 尽可能多地导出您想要导出的 sprite。右键单击并 select“导出子精灵”。