Created
March 23, 2019 14:19
-
-
Save huihut/ccda652deaaf8ed9bfc4e401cac010de to your computer and use it in GitHub Desktop.
Unity TakeScreenshot
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
// <http://wiki.unity3d.com/index.php?title=TakeScreenshot> | |
// ****** Notice : It doesn't works in Wep Player environment. ****** | |
// ****** It works in PC environment. ****** | |
// Default method have some problem, when you take a Screen shot for your game. | |
// So add this script. | |
// CF Page : http://technology.blurst.com/unity-jpg-encoding-javascript/ | |
// made by Jerry ( sdragoon@nate.com ) | |
using UnityEngine; | |
using System.Collections; | |
using System.IO; | |
public class ScreenShot : MonoBehaviour | |
{ | |
private int count = 0; | |
void Update() | |
{ | |
if (Input.GetKeyDown("k")) | |
StartCoroutine(ScreenshotEncode()); | |
} | |
IEnumerator ScreenshotEncode() | |
{ | |
// wait for graphics to render | |
yield return new WaitForEndOfFrame(); | |
// create a texture to pass to encoding | |
Texture2D texture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false); | |
// put buffer into texture | |
texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0); | |
texture.Apply(); | |
// split the process up--ReadPixels() and the GetPixels() call inside of the encoder are both pretty heavy | |
yield return 0; | |
byte[] bytes = texture.EncodeToPNG(); | |
// save our test image (could also upload to WWW) | |
File.WriteAllBytes(Application.dataPath + "/../testscreen-" + count + ".png", bytes); | |
count++; | |
// Added by Karl. - Tell unity to delete the texture, by default it seems to keep hold of it and memory crashes will occur after too many screenshots. | |
DestroyObject( texture ); | |
//Debug.Log( Application.dataPath + "/../testscreen-" + count + ".png" ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment