I made this very simple script which should be attached to inactive camera Gameobject with renderTexure. If the camera Gameobject is active, this suppose to record only one frame to the renderTexure and then save to a path as a .png. After that the camera should be disabled.
public static string path;
void Update()
{
StartCoroutine(SSOT());
}
public static void SaveRTToFileToSharing()
{
RenderTexture rt = Resources.Load<RenderTexture>(@"Render Texure/ScreenShot") 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;
tex.Apply();
byte[] bytes;
bytes = tex.EncodeToPNG();
path = Application.persistentDataPath + "Test Shot.png";
System.IO.File.WriteAllBytes(path, bytes);
Destroy(tex);
}
IEnumerator SSOT()
{
yield return new WaitForEndOfFrame();
SaveRTToFileToSharing();
gameObject.SetActive(false);
}
The script works as I intended but i'm not sure if it really record only one frame or more. What would you change if you gonna change anything ?