Question by
thibetanus · Aug 17, 2018 at 04:15 AM ·
screenshotgameviewmaincamera
gameview capture in realtime
hello i have a question in unity I am using unity 5.6.3 linux on pc (ubuntu) I want to capture the screen in the current game view in real time and save it as a jpg file. Can I add a script to my main camera to make it possible? thank you
Comment
Answer by thibetanus · Aug 17, 2018 at 05:52 AM
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class capture : MonoBehaviour {
void Update(){
StartCoroutine("Rendering");
}
IEnumerator Rendering () {
int count = 0;
yield return new WaitForEndOfFrame();
byte[] imgBytes;
string path = @"/home/haeyun/Desktop/screen/test"+ count;
count = count + 1 ;
Texture2D texture = new Texture2D(Screen.width , Screen.height , TextureFormat.RGB24 , false);
texture.ReadPixels(new Rect(0,0,Screen.width, Screen.height), 0 ,0, false);
texture.Apply ();
imgBytes = texture.EncodeToJPG();
DestroyImmediate(texture);
System.IO.File.WriteAllBytes(path + ".jpg" , imgBytes);
Debug.Log( path = " has been saved");
yield return new WaitForSecondsRealtime (5);
}
}
I made my code this way roughly How do I capture and save game views in real time?
In this way, only pictures of the same name will continue to be created