Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Screenshot of area in 3D view

Discussion in 'Scripting' started by alvifarooq, May 1, 2016.

  1. alvifarooq

    alvifarooq

    Joined:
    Sep 14, 2015
    Posts:
    21
    Hi all,

    I've been trying this over a few weeks now and just when I think the code's working, I see new issues develop. Briefly, I'm taking a screenshot of a particular area in my 3D game. The screenshot is accurate when I play it through the editor but when I deploy it to my devices, the displayed area shifts down. I've used Debug.Log to check the values for positions on both devices (and the editor) and they're all accurate. Could someone advice where I'm going wrong? For reference, I'm using an orthographic camera which is rotated on X by 28 degrees.

    Code (CSharp):
    1. int samplingFactor = 2;
    2.         int yMin = (int)(Camera.main.WorldToScreenPoint (gameManager.horizonPoint.position)).y;
    3.  
    4.         int yMax = (int)(Camera.main.WorldToScreenPoint (gameManager.apexPoint.position)).y;
    5.  
    6.         int height = yMax - yMin;
    7.  
    8.         //create texture with the same scaling as the image
    9.         Texture2D tex = new Texture2D (Screen.width * samplingFactor, height * samplingFactor, TextureFormat.RGB24, false);
    10.  
    11.         //render the entire screen into this texture
    12.         RenderTexture snapshotRT = new RenderTexture (Screen.width * samplingFactor, Screen.height * samplingFactor, 24, RenderTextureFormat.ARGB32);
    13.         RenderTexture.active = snapshotRT;
    14.         Camera.main.targetTexture = snapshotRT;
    15.         Camera.main.Render ();
    16.  
    17.         Rect captureRect = new Rect (0.0f, yMin * samplingFactor, (float) Screen.width * samplingFactor, (float) (height * samplingFactor));
    18.         tex.ReadPixels (captureRect, 0, 0);
    19.         tex.Apply();
    20.  
    21.         byte[] val = tex.EncodeToPNG();
    22.  
    23.         //THIS IS WHERE I DISPLAY THE TEXTURE
    24.  
    25.         RenderTexture.active = null;
    26.         Camera.main.targetTexture = null;
    27.         Destroy (tex);
    Regards,

    Farooq
     
  2. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    Dunno but you might want to put the function in a Coroutine and add some yield return new WaitForEndOfFrame() between creating the render texture, the render, and the readPixels function.
     
  3. alvifarooq

    alvifarooq

    Joined:
    Sep 14, 2015
    Posts:
    21
    Oops - I didn't post the whole method. This method is called from a coroutine only after a yield return new WaitForEndOfFrame().
    Thanks for the message.
     
  4. alvifarooq

    alvifarooq

    Joined:
    Sep 14, 2015
    Posts:
    21
    I found this comment by atti at this link: http://answers.unity3d.com/questions/22954/how-to-save-a-picture-take-screenshot-from-a-camer.html

    So, I'm trying to see if aspect ratio is causing the problems at the point where I assigned the snapshotRT to the camera. Anyone has some idea if this could be the case?
     
  5. alvifarooq

    alvifarooq

    Joined:
    Sep 14, 2015
    Posts:
    21
    So just in case someone's looking for something similar: I managed to fix this but it's still a little strange. I use the coordinate inversions i.e. rect start from 0,0 on the top-left and screen coordinates starting from 0,0 on the bottom left. After adjusting for this, the editor window shows the weird result I was getting on my device before but the device shows the screenshot accurately!
     
  6. alvifarooq

    alvifarooq

    Joined:
    Sep 14, 2015
    Posts:
    21
    I think this could've been a Unity bug. I'm deploying now to iOS10 and have reverted back to the original code and it works. Just thought I'd drop an update for anyone looking for a solution.