Guys as visible in the screenshot above ! I have two cams in my game the one is MainCamera which renders stuff to be shown on gamescene and another one is the second camera which I use to take some screenshots only(this cam is not to be show to the user).
Now to take screens from the second cam i have attached a Script to the second camera and when I hit ‘Space’ button the screenshot is captured from this cam successfully . But the issue is when I hit the space the camera switches from the MainCamera to this cam (which I dont want at all).
How do I prevent this camera switch ? since my only purpose of second cam is to take screenshot of that camera view in the background
Following is the script I am using to capture screenshot
using UnityEngine;
using System.Collections;
public class HiResScreenShot : MonoBehaviour {
public int resWidth = 2550;
public int resHeight = 3300;
private bool takeHiResShot = false;
public Camera cam;
void Start(){
resWidth = Screen.width;
resHeight = Screen.height;
//cam = (Camera)GameObject.FindGameObjectsWithTag("SecondCam");
}
public static string ScreenShotName(int width, int height) {
return string.Format("{0}/screenshots/screen_{1}x{2}_{3}.png",
Application.dataPath,
width, height,
System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"));
}
public void TakeHiResShot() {
takeHiResShot = true;
}
void LateUpdate() {
takeHiResShot |= Input.GetKeyDown(KeyCode.Space);
if (takeHiResShot) {
RenderTexture rt = new RenderTexture(resWidth, resHeight, 24);
cam.targetTexture = rt;
Texture2D screenShot = new Texture2D(resWidth, resHeight, TextureFormat.RGB24, false);
cam.Render();
RenderTexture.active = rt;
screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0);
cam.targetTexture = null;
RenderTexture.active = null; // JC: added to avoid errors
Destroy(rt);
byte[] bytes = screenShot.EncodeToPNG();
string filename = ScreenShotName(resWidth, resHeight);
System.IO.File.WriteAllBytes(Application.persistentDataPath + "/someshot.png", bytes);
Debug.Log(string.Format("Took screenshot to: {0}", filename));
takeHiResShot = false;
}
}
}