1

So I have a Sprite 2d and UI in unity. (Png image) That contains many small images like buttons. So I slice them and I can use them individualy in unity. But the thing is that I want to export each of the png Images that I got while slicing them for another use. So can I do that? And have them as separate pngs?

This is the image which contains the multiple images

And what I want is these Images:

After I sliced them

To export them in my (lets say) desktop as individual pngs.

Thank you!

Alex Rika
  • 141
  • 2
  • 14

2 Answers2

7

I had a need for this too, I ended up solving it with a simple editor script:

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());
    }
}

First, drop this into a script file named EditorSubSprites.cs and make sure that it resides in an Editor folder. If you have no editor folder you can simply create at /Assets/Editor/

To use, expand a texture asset's sprites, and select as many of the sprites as you wish to export. Right-click and select "Export SubSprites".

Samuel-IH
  • 703
  • 4
  • 7
  • Sprite also need to be configured in specific way for this to work, so check your main spritesheet that: "Mesh Type = Full Rect" and "Advanced>Read/Write Enabled = true". – Wappenull May 04 '22 at 20:11
2

Use Photoshop, Crop it, a Lot of Cut and Paste is going to happen save as PNG. Import it to unity inside the folder, i like your designs. Also Watch Brackeys Tutorial on Youtube, https://www.youtube.com/user/Brackeys , he save me from my school years.

(edit : i recommend Photoshop because its the only thing i know.)

Æthenwulf
  • 213
  • 2
  • 19