Issues when taking screenshot from multiple cameras.
I am trying to take screenshots from multiple cameras(2 for now), but it isn't working. The screenshot for the 2nd camera is the same as the screenshot for the first one i.e. the screenshot for the 2nd camera is the view of the first one. Following is my script. The behaviour when using Application.CaptureScreenshot is the same.
public class ScrnshotTrigger : MonoBehaviour {
// Use this for initialization
Camera[] AllCams;
Camera[] initActiveCams;
string[] cameraNames = { "MainCamera", "MainCamera2" };
void Start () {
AllCams = new Camera[cameraNames.Length];
string s = "";
for (int i1 = 0; i1 < cameraNames.Length; i1++) {
AllCams[i1] = GameObject.Find(cameraNames[i1]).GetComponent<Camera>();
s += AllCams[i1].name + ",";
}
Debug.Log("Cameras in allCams: " + s);
s = "";
foreach (var cam in Camera.allCameras)
s += cam.name + ", ";
Debug.Log("Cameras in active Cameras: " + s);
initActiveCams = new Camera[Camera.allCameras.Length];
Camera.allCameras.CopyTo(initActiveCams, 0);
//InvokeRepeating("SetCamera", 0f, 1.0f);
AllCams[1].enabled = false;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.E))
{
SetCamera();
}
}
void SetCamera() {
Debug.Log("setCamera called");
// first disable all cameras
foreach (var cam1 in initActiveCams)
cam1.enabled = false;
float startTime = Time.time;
foreach (var cam in AllCams) {
Debug.Log("Camera1: " + AllCams[0].enabled + ":::: Camera2:" + AllCams[1].enabled + "::::: Main Camera:" + ((Camera.main == null) ? "null" : Camera.main.name));
Debug.Log("Processing for: " + cam.name);
cam.enabled = true;
StartCoroutine(TakeTemporaryTexScreenshot(cam, startTime));
cam.enabled = false;
Debug.Log("Processing completed for: " + cam.name);
}
foreach (var cam2 in initActiveCams)
cam2.enabled = true;
}
IEnumerator TakeTemporaryTexScreenshot(Camera cam, float startTime_init) {
Debug.Log("Camera1: " + AllCams[0].enabled + ":::: Camera2:" + AllCams[1].enabled + "::::: Main Camera:" + ((Camera.main == null) ? "null" : Camera.main.name));
string name = cam.name;
Debug.Log("Taking screenshot for :" + name);
// wait for graphics to render
yield return new WaitForEndOfFrame();
string pathToImage = Application.persistentDataPath + "/" + name + "/" + startTime_init.ToString() + ".png";
//Application.CaptureScreenshot(pathToImage);
int width = Screen.width;
int height = Screen.height;
Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);
// Read screen contents into the texture
tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
tex.Apply();
// Encode texture into PNG
byte[] bytes = tex.EncodeToPNG();
Object.Destroy(tex);
File.WriteAllBytes(pathToImage, bytes);
Debug.Log("File name :" + pathToImage);
Debug.Log(pathToImage +" ::" +System.IO.File.Exists(pathToImage));
}
}
I am unable to understand the reason. I thought it was because cam.enabled is immediately being set to false after the coroutine is started, so I tried blocking the script after calling the coroutine using
while(false == System.IO.File.Exists(path)) {}
But it doesn't help. In fact, unity hangs after I use it.
A coroutine takes a frame to execute the first time. You're basically setting as many coroutines up as there are cameras, but by the time you run the first time, the only active cams are the ones from initActiveCams.
Whet I'm saying is, Update, which starts your coroutines, does not wait at all, because it's no coroutine itself. It just executes straight through.
Your answer
Follow this Question
Related Questions
Sprites getting wierd pixels at the sides 1 Answer
Changing a texture as a float or int drops 2 Answers
Texture position on multiple mesh 0 Answers
I make a timing score script in unity 2D, how i make high score of time?? Script is as follow: 1 Answer
Detect touch gesture in a more than one camera screen rendering 0 Answers