Search Unity

prevent a camera switch while taking screenshot by render method

Discussion in 'Scripting' started by vmnoodel, Jul 28, 2014.

  1. vmnoodel

    vmnoodel

    Joined:
    Apr 15, 2014
    Posts:
    9


    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



    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class HiResScreenShot : MonoBehaviour {
    4.     public int resWidth = 2550;
    5.     public int resHeight = 3300;
    6.     private bool takeHiResShot = false;
    7.     public Camera cam;
    8.     void Start(){
    9.         resWidth = Screen.width;
    10.         resHeight = Screen.height;
    11.         //cam = (Camera)GameObject.FindGameObjectsWithTag("SecondCam");
    12.     }
    13.     public static string ScreenShotName(int width, int height) {
    14.         return string.Format("{0}/screenshots/screen_{1}x{2}_{3}.png",
    15.                              Application.dataPath,
    16.                              width, height,
    17.                              System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"));
    18.     }
    19.     public void TakeHiResShot() {
    20.         takeHiResShot = true;
    21.     }
    22.     void LateUpdate() {
    23.         takeHiResShot |= Input.GetKeyDown(KeyCode.Space);
    24.         if (takeHiResShot) {
    25.             RenderTexture rt = new RenderTexture(resWidth, resHeight, 24);
    26.             cam.targetTexture = rt;
    27.             Texture2D screenShot = new Texture2D(resWidth, resHeight, TextureFormat.RGB24, false);
    28.             cam.Render();
    29.             RenderTexture.active = rt;
    30.             screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0);
    31.             cam.targetTexture = null;
    32.             RenderTexture.active = null; // JC: added to avoid errors
    33.             Destroy(rt);
    34.             byte[] bytes = screenShot.EncodeToPNG();
    35.             string filename = ScreenShotName(resWidth, resHeight);
    36.             System.IO.File.WriteAllBytes(Application.persistentDataPath + "/someshot.png", bytes);
    37.             Debug.Log(string.Format("Took screenshot to: {0}", filename));
    38.             takeHiResShot = false;
    39.         }
    40.     }
    41. }
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    I thought the only camera rendering to the main game screen was "main"? I'm not seeing anything in that code which flips the "main" to the secondary camera... the camera reference page even goes as far as saying you can't do render to a texture and to the game screen:
    http://docs.unity3d.com/Manual/class-Camera.html
    are you manipulating the camera's anywhere else?
     
  3. vmnoodel

    vmnoodel

    Joined:
    Apr 15, 2014
    Posts:
    9
    No I am not manipulating cam's elsewhere >: D