Skip to content

Instantly share code, notes, and snippets.

@zcyemi
Created May 17, 2018 10:48
Show Gist options
  • Save zcyemi/3be14bb7d417cd0a638cd6ee1329cfb0 to your computer and use it in GitHub Desktop.
Save zcyemi/3be14bb7d417cd0a638cd6ee1329cfb0 to your computer and use it in GitHub Desktop.
Script for exporting Unity sprite packer atlas texture.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
public class SpriteAtlasExporter : MonoBehaviour {
public Sprite sprite;
RenderTexture tex;
void Start () {
var stex = sprite.texture;
Debug.Log(stex.width + " " + stex.height);
tex = RenderTexture.GetTemporary(stex.width, stex.height, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Default);
tex.Create();
Graphics.Blit(stex, tex);
SaveToFile(tex, Application.dataPath + "/" + stex.name + ".png");
RenderTexture.ReleaseTemporary(tex);
}
public static void SaveToFile(RenderTexture renderTexture, string name)
{
RenderTexture currentActiveRT = RenderTexture.active;
RenderTexture.active = renderTexture;
Texture2D tex = new Texture2D(renderTexture.width, renderTexture.height);
tex.ReadPixels(new Rect(0, 0, tex.width, tex.height), 0, 0);
var bytes = tex.EncodeToPNG();
Debug.Log(name);
System.IO.File.WriteAllBytes(name, bytes);
UnityEngine.Object.Destroy(tex);
RenderTexture.active = currentActiveRT;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment