- Home /
Can you change ScreenShotCapture Camera?
Hey guys!
I'm making this application where you have a character that will have multiple poses and saved as jpg. For now everything works great!
The thing is, I make multiple screenshots very fast, making the different images fly on the screen while the user waits for the result, which is just ugly.
I would want to add a 2nd camera with a loading screen while the screenshots are taken behind this screen. So, 1st camera would take the screenshot while the 2nd would display a loading scren to the user.
Is that possible? Here what I have right now :
// Check Date to Scren Capture
string date = GetDate();
int sceneCount = 0;
int pageCount = 1;
GameObject[] activeScenes = new GameObject[2];
sceneSelectionElementsParent.SetActive(false);
foreach (int sceneId in scenesToLoad)
{
GameObject scene = availableScenesToLoad[sceneId];
activeScenes[sceneCount] = scene;
scene.transform.position = sceneLocation[sceneCount].position;
scene.SetActive(true);
Debug.Log("Scene " + sceneId + " has been loaded!");
sceneCount++;
if((sceneCount) == 2)
{
ScreenCapture.CaptureScreenshot("MyCharacter_0" + pageCount + "_" + date + ".jpeg");
yield return new WaitForSeconds(0.1f);
sceneCount = 0;
pageCount++;
activeScenes[0].SetActive(false);
activeScenes[1].SetActive(false);
}
}
Answer by BastianUrbach · Sep 09, 2019 at 09:09 AM
I believe CaptureScreenshot can only capture what's on the screen but you can render a camera into a RenderTexture and convert the result into a JPG:
static void CaptureCamera(Camera camera, string path, int width, int height) {
var renderTexture = new RenderTexture(width, height, 16);
var texture2D = new Texture2D(width, height);
var target = camera.targetTexture;
camera.targetTexture = renderTexture;
camera.Render();
camera.targetTexture = target;
var active = RenderTexture.active;
RenderTexture.active = renderTexture;
texture2D.ReadPixels(new Rect(0, 0, width, height), 0, 0);
RenderTexture.active = active;
texture2D.Apply();
System.IO.File.WriteAllBytes(path, texture2D.EncodeToJPG());
}
Very interesting! I'll be checking this out tonight when I come back from work, but it seems like a great idea!
Thanks a lot!
Changed a bit of things here and there, but the general idea behind it is exactly what I needed.
Thanks a lot man :D