Skip to content

Instantly share code, notes, and snippets.

@ktpttd
Forked from krzys-h/SaveRenderTextureToFile.cs
Created April 11, 2023 06:57
Show Gist options
  • Save ktpttd/df5be103b99aad5d98df19bc0b9f2c5e to your computer and use it in GitHub Desktop.
Save ktpttd/df5be103b99aad5d98df19bc0b9f2c5e to your computer and use it in GitHub Desktop.

Revisions

  1. @krzys-h krzys-h renamed this gist Jun 17, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion RenderCameraToFile.cs → SaveScreenshotToFile.cs
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    using UnityEngine;
    using UnityEditor;

    public class RenderCameraToFile {
    public class SaveScreenshotToFile {
    [MenuItem("Custom/Render Camera to file")]
    public static void RenderCameraToFile()
    {
  2. @krzys-h krzys-h revised this gist Jun 17, 2022. 1 changed file with 33 additions and 0 deletions.
    33 changes: 33 additions & 0 deletions RenderCameraToFile.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    using UnityEngine;
    using UnityEditor;

    public class RenderCameraToFile {
    [MenuItem("Custom/Render Camera to file")]
    public static void RenderCameraToFile()
    {
    Camera camera = Selection.activeGameObject.GetComponent<Camera>();

    RenderTexture rt = new RenderTexture(2560, 1440, 24, RenderTextureFormat.ARGB32, RenderTextureReadWrite.sRGB);
    RenderTexture oldRT = camera.targetTexture;
    camera.targetTexture = rt;
    camera.Render();
    camera.targetTexture = oldRT;

    RenderTexture.active = rt;
    Texture2D tex = new Texture2D(rt.width, rt.height, TextureFormat.RGB24, false);
    tex.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0);
    RenderTexture.active = null;

    byte[] bytes = tex.EncodeToPNG();
    string path = "Assets/screenshot.png";
    System.IO.File.WriteAllBytes(path, bytes);
    AssetDatabase.ImportAsset(path);
    Debug.Log("Saved to " + path);
    }

    [MenuItem("Custom/Render Camera to file", true)]
    public static bool RenderCameraToFileValidation()
    {
    return Selection.activeGameObject != null && Selection.activeGameObject.GetComponent<Camera>() != null;
    }
    }
  3. @krzys-h krzys-h created this gist Jan 20, 2018.
    29 changes: 29 additions & 0 deletions SaveRenderTextureToFile.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    using UnityEngine;
    using UnityEditor;

    public class SaveRenderTextureToFile {
    [MenuItem("Assets/Save RenderTexture to file")]
    public static void SaveRTToFile()
    {
    RenderTexture rt = Selection.activeObject as RenderTexture;

    RenderTexture.active = rt;
    Texture2D tex = new Texture2D(rt.width, rt.height, TextureFormat.RGB24, false);
    tex.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0);
    RenderTexture.active = null;

    byte[] bytes;
    bytes = tex.EncodeToPNG();

    string path = AssetDatabase.GetAssetPath(rt) + ".png";
    System.IO.File.WriteAllBytes(path, bytes);
    AssetDatabase.ImportAsset(path);
    Debug.Log("Saved to " + path);
    }

    [MenuItem("Assets/Save RenderTexture to file", true)]
    public static bool SaveRTToFileValidation()
    {
    return Selection.activeObject is RenderTexture;
    }
    }