• 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
3
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
8

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Unity OnMouseDrag too fast for Update function 0 Answers

Change dynamicaly a PNG used in a motion 1 Answer

How can I achieve this particular 2D lighting effect? 2 Answers

How can I find the size (width, height) of a rotated Sprite? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges