• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by outsorts · Apr 09, 2014 at 08:23 PM · spriteexport

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
7

Answer by Artgig · Jul 16, 2015 at 06:42 PM

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



Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sehathesam · Jun 27, 2019 at 01:53 PM 0
Share

very useful

avatar image StartStart · Aug 26, 2020 at 04:09 AM 0
Share

Worked for me! Thank you!

avatar image leehoanminh · Dec 23, 2020 at 09:53 PM 0
Share

This information is useful for me. Thank you for uploading this. I just wonder if there is any other tool to do this job, so I can use for non-unity project. It is very weird to use unity just to split image =)).

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

25 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Save a map as a PNG file. 3 Answers

Multiple lights disable additive intensity for Sprites-Diffuse? 0 Answers

Lifebars not appearing above enemies' heads 3 Answers

How to swap a spriteRenderers source sprite at runtime when used by animator 0 Answers

How can I add a border to my sprites? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges