Skip to content

Instantly share code, notes, and snippets.

@the6th
Last active March 30, 2020 04:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save the6th/391bf5f329679cf5ff33be2d968065d0 to your computer and use it in GitHub Desktop.
Save the6th/391bf5f329679cf5ff33be2d968065d0 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
using System.IO;
public class SaveRenderTextureToJPG : MonoBehaviour
{
[SerializeField]
Renderer targetRenderer;
private void Start()
{
if (targetRenderer == null)
targetRenderer = GetComponent<MeshRenderer>();
}
WaitForEndOfFrame frameEnd = new WaitForEndOfFrame();
IEnumerator savePng()
{
yield return frameEnd;
var RenderTextureRef = targetRenderer.material.GetTexture("_MainTex") as Texture2D;
Debug.Log($"{RenderTextureRef.width} / {RenderTextureRef.height}");
byte[] bytes = RenderTextureRef.EncodeToJPG();
Object.Destroy(RenderTextureRef);
//Write to a file in the project folder
File.WriteAllBytes(Application.dataPath + "/../SavedScreen.jpg", bytes);
}
private void OnGUI()
{
if (GUI.Button(new Rect(10, 10, 200, 40), "SAVE"))
{
StartCoroutine("savePng");
}
}
}
using UnityEngine;
using System.Collections;
using System.IO;
public class SaveRenderTextureToPng : MonoBehaviour
{
[SerializeField]
Renderer targetRenderer;
private void Start()
{
if (targetRenderer == null)
targetRenderer = GetComponent<MeshRenderer>();
}
bool isCapture = false;
WaitForEndOfFrame frameEnd = new WaitForEndOfFrame();
IEnumerator savePng()
{
yield return frameEnd;
var RenderTextureRef = targetRenderer.material.GetTexture("_MainTex") as Texture2D;
Debug.Log($"{RenderTextureRef.width} / {RenderTextureRef.height}");
byte[] bytes = RenderTextureRef.EncodeToPNG();
Object.Destroy(RenderTextureRef);
//Write to a file in the project folder
File.WriteAllBytes(Application.dataPath + "/../SavedScreen.png", bytes);
}
private void OnGUI()
{
if (GUI.Button(new Rect(10, 10, 200, 40), "SAVE"))
{
StartCoroutine("savePng");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment