Skip to content

Instantly share code, notes, and snippets.

@float3
Created March 14, 2023 16:51
Show Gist options
  • Save float3/6aa127acc8979308b36bcb6c4d3312f1 to your computer and use it in GitHub Desktop.
Save float3/6aa127acc8979308b36bcb6c4d3312f1 to your computer and use it in GitHub Desktop.
writing a compressed texture that's loaded in memory to disk
using System.IO;
using UnityEditor;
using UnityEngine;
public class test : EditorWindow
{
[MenuItem("CONTEXT/Material/Rip Texture")]
static void GetTexture(MenuCommand command)
{
Material m = command.context as Material;
Texture x = m.GetTexture("_TextureName");
Texture2D y = x as Texture2D;
Texture2D tex = new Texture2D(x.width, x.height, TextureFormat.ARGB32, false);
RenderTexture a = RenderTexture.GetTemporary(x.width, x.height, 0, RenderTextureFormat.ARGB32);
Graphics.Blit(y, a);
RenderTexture.active = a;
tex.ReadPixels(new Rect(0, 0, x.width, x.height), 0, 0);
byte[] bytes = tex.EncodeToPNG();
File.WriteAllBytes(Application.dataPath + "/tex.png", bytes);
RenderTexture.ReleaseTemporary(a);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment