Pages

19/04/2017

Unity3D Screenshot (Take in-game picture)

Here’s a snippet of code for using Unity’s Application.CaptureScreenshot to get in-game screenshots.

The screenshot is taken from the main camera in the screen, so this method may not be suitable when you want a screenshot from another camera’s view. In that case, RenderTexture() and creating a Texture2D is much more flexible. Click here for a good resource on render texture screenshots.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
using UnityEngine;
using System.Collections;

public class Screenshot : MonoBehaviour
{
   public int superSize = 1; //size multiplier (eg 2 is x2 bigger)
   private bool takeHiResShot = false;

   public Camera cam;

   public void TakeHiResShot()
   {
      takeHiResShot = true;
   }

   void LateUpdate()
   {
      takeHiResShot |= Input.GtKeyDown("a");
      if(takeHiResShot)
      {
         Application.CaptureScreenshot("Screenshot.png", superSize);
         takeHiResShot = false;
      }
   }
}

No comments:

Post a Comment