Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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
1
Question by turbolek · Jan 30, 2019 at 12:10 PM · atlaspngtexture atlas

Saving atlas texture generated by Sprite Packer to a PNG file

When I go to: Window > 2D > Sprite Packer I can preview atlas textures generated automatically by Sprite Packer. What I would like to do is to save all of them into PNG files so I can look them up outside Unity editor.

I wrote following script that was supposed to do so:

 using UnityEngine;
 using UnityEditor;
 using UnityEditor.Sprites;
 using System.Collections;
 using System.IO;
 
 public class AtlasTextureExporter : MonoBehaviour
 {
 
     [MenuItem("Tools/Atlas Exporter/Export atlases as PNG")]
     static void ExportAtlases()
     {
         string[] atlasNames = Packer.atlasNames;
 
         for (int i = 0; i < atlasNames.Length; i++)
         {
             string atlasName = atlasNames[i];
             Texture2D[] textures = Packer.GetTexturesForAtlas(atlasName);
 
             for (int j = 0; j < textures.Length; j++)
             {
                 Texture2D texture = textures[j];
                 if (!Directory.Exists(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop) + "/Atlases"))
                     Directory.CreateDirectory(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop) + "/Atlases");
                 FileStream fs = new FileStream(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop) + "/Atlases/" + texture.name + ".png", FileMode.Create);
                 BinaryWriter bw = new BinaryWriter(fs);
                 bw.Write(texture.EncodeToPNG());
                 bw.Close();
                 fs.Close();
             }
         }
     }
 }

But I get following error:

ArgumentException: Texture 'SpriteAtlasTexture-myScene-2048x2048-fmt12' is not readable, the texture memory can not be accessed from scripts. You can make the texture readable in the Texture Import Settings. AtlasTextureExporter.ExportAtlases () (at Assets/Root/Editor/AtlasTextureExporter.cs:27)

How can I make an atlas texture readable? If I cannot, how can I save an atlas texture into a PNG file?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by sarahnorthway · Feb 07, 2020 at 12:43 AM

Same issue here with the new SpriteAtlas system. I had to copy the texture to a new RenderTexture first then save the copy, solution from here.

I couldn't find a direct way to access the SpriteAtlas texture so I went through the internal method Unity uses to show it in the inspector panel. Obviously not using this for production, but here is how I save a SpriteAtlas to a PNG:

     [MenuItem("Tools/Export atlases as PNG")]
     static void ExportAtlases() {
         string exportPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "/Atlases";
         foreach (UnityEngine.Object obj in Selection.objects) {
             SpriteAtlas atlas = (SpriteAtlas) obj;
             if (atlas == null) continue;
             Debug.Log("Exporting selected atlas: " + atlas);
             
             // use reflection to run this internal editor method
             // UnityEditor.U2D.SpriteAtlasExtensions.GetPreviewTextures
             // internal static extern Texture2D[] GetPreviewTextures(this SpriteAtlas spriteAtlas);
             Type type = typeof(UnityEditor.U2D.SpriteAtlasExtensions);
             MethodInfo methodInfo = type.GetMethod("GetPreviewTextures", BindingFlags.Static | BindingFlags.NonPublic);
             if (methodInfo == null) {
                 Debug.LogWarning("Failed to get UnityEditor.U2D.SpriteAtlasExtensions");
                 return;
             }
             Texture2D[] textures = (Texture2D[])methodInfo.Invoke(null, new object[] {atlas} );
             if (textures == null) {
                 Debug.LogWarning("Failed to get texture results");
                 continue;
             }
 
             foreach (Texture2D texture in textures) {
                 // these textures in memory are not saveable so copy them to a RenderTexture first
                 Texture2D textureCopy = DuplicateTexture(texture);
                 if (!Directory.Exists(exportPath)) Directory.CreateDirectory(exportPath);
                 string filename = exportPath + "/" + texture.name + ".png";
                 FileStream fs = new FileStream(filename, FileMode.Create);
                 BinaryWriter bw = new BinaryWriter(fs);
                 bw.Write(textureCopy.EncodeToPNG());
                 bw.Close();
                 fs.Close();
                 Debug.Log("Saved texture to " + filename);
             }
         }
     }
     
     private static Texture2D DuplicateTexture(Texture2D source) {
         RenderTexture renderTex = RenderTexture.GetTemporary(
             source.width,
             source.height,
             0,
             RenderTextureFormat.Default,
             RenderTextureReadWrite.Linear);
 
         Graphics.Blit(source, renderTex);
         RenderTexture previous = RenderTexture.active;
         RenderTexture.active = renderTex;
         Texture2D readableText = new Texture2D(source.width, source.height);
         readableText.ReadPixels(new Rect(0, 0, renderTex.width, renderTex.height), 0, 0);
         readableText.Apply();
         RenderTexture.active = previous;
         RenderTexture.ReleaseTemporary(renderTex);
         return readableText;
     }


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 Knugke · Aug 01, 2021 at 08:19 PM 0
Share

Thanks! Helped me out :)

avatar image iamarugin · Feb 08 at 09:32 AM 0
Share

File.WriteAllBytes(filename, textureCopy.EncodeToPNG());

avatar image iamarugin · Feb 08 at 09:33 AM 0
Share

Also
var filename = Path.Combine(exportPath, $"{texture.name}.png");

avatar image
0

Answer by idbrii · Jan 08, 2021 at 09:51 PM

You should be able to make it readable by enabling "Read/Write Enabled" in the atlas's Import Settings inspector.

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

102 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 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 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 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 avatar image avatar image

Related Questions

NGUI black border on png and targa 2 Answers

Texture Atlasing Performance 3 Answers

Unity 2018.2.[1,2]f1 - Introduced artifacts to UI sprites at runtime 2 Answers

Texture atlas - little glow of nearby textures seen from distance 1 Answer

Best practice? 200PNG vs 6kx6k Atlas 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