export sprite sheets

how can i export the sprite images that i have sliced from the sprite editor to use as independent images from my hard disk.

I know it’s been a while since this was asked, but I just had a need for this myself, so…

You can extract all the sprites in a sprite sheet using Unity itself. Here are the steps I used:

  1. make the imported sheet readable (settings → debug view → texture type: advanced)

  2. move it into your Resources (so you can load it via code)

  3. in script, load the sprite sheet:

     	Sprite[] sheet = Resources.LoadAll<Sprite> (sheetName);
    
  4. iterate over all the sprites, and extract each:

     	foreach (Sprite sprite in sheet) {
     		Texture2D tex = sprite.texture;
     		Rect r = sprite.textureRect;
     		Texture2D subtex = tex.CropTexture( (int)r.x, (int)r.y, (int)r.width, (int)r.height );
     		byte[] data = subtex.EncodeToPNG();
     		File.WriteAllBytes (Application.persistentDataPath + "/" + sprite.name + ".png", data);
     	}
    
  5. The extracted files will end up wherever the persistent path is - just debug it out so you know:

     	Debug.Log(Application.persistentDataPath);
    

The CropTexture method is an extension method to the Texture2D class. Create the file “Texture2DExtensions.cs”, and paste this in:

using UnityEngine;
using System.Collections;

static class Texture2DExtensions
{
	/**
	 * CropTexture
	 * 
	 * Returns a new texture, composed of the specified cropped region.
	 */
	public static Texture2D CropTexture(this Texture2D pSource, int left, int top, int width, int height)
	{
		if (left < 0) {
			width += left;
			left = 0;
		}
		if (top < 0) {
			height += top;
			top = 0;
		}
		if (left + width > pSource.width) {
			width = pSource.width - left;
		}
		if (top + height > pSource.height) {
			height = pSource.height - top;
		}

		if (width <= 0 || height <= 0) {
			return null;
		}

		Color[] aSourceColor = pSource.GetPixels(0);

		//*** Make New
		Texture2D oNewTex = new Texture2D(width, height, TextureFormat.RGBA32, false);
		
		//*** Make destination array
		int xLength = width * height;
		Color[] aColor = new Color[xLength];

		int i = 0;
		for (int y = 0; y < height; y++) {
			int sourceIndex = (y + top) * pSource.width + left;
			for ( int x = 0; x < width; x++ ) {
				aColor[ i++ ] = aSourceColor[ sourceIndex++ ];
			}
		}
		
		//*** Set Pixels
		oNewTex.SetPixels(aColor);
		oNewTex.Apply();
		
		//*** Return
		return oNewTex;
	}
}

Thank you so much!!! you’re a life saver.