Skip to content

Instantly share code, notes, and snippets.

@zxzsaga
Created January 17, 2019 07:52
Show Gist options
  • Select an option

  • Save zxzsaga/6f7e67984930c7008fb4ed82d55759c1 to your computer and use it in GitHub Desktop.

Select an option

Save zxzsaga/6f7e67984930c7008fb4ed82d55759c1 to your computer and use it in GitHub Desktop.
RenderTexture output PNG.
using System.IO;
using UnityEngine;
using UnityEditor;
public static class RenderTexture2PNGUtility {
[MenuItem("Assets/RenderTexture2PNG", true)]
private static bool RenderTexture2PNGValidation() {
return Selection.activeObject is RenderTexture;
}
/// <summary>
/// 将 RenderTexture 输出为 PNG
/// </summary>
[MenuItem("Assets/RenderTexture2PNG")]
public static void RenderTexture2PNG() {
RenderTexture rt = (RenderTexture)Selection.activeObject;
string pngOutPath = "Assets/out.png";
var tex = new Texture2D(rt.width, rt.height);
RenderTexture.active = rt;
tex.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0);
tex.Apply();
File.WriteAllBytes(pngOutPath, tex.EncodeToPNG());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment