Last active
March 30, 2020 04:09
-
-
Save the6th/391bf5f329679cf5ff33be2d968065d0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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