Tuesday, December 2, 2014

Unity: Screen Capture in Game

Below is the link to a simple piece of exercise which shows how to do a screen capture in a Unity game.

https://dl.dropboxusercontent.com/u/113201788/Unity3D/Camera.Screen-Capture.zip

Firstly, create a C# script "ScreenTexture.cs" and update its content as follows:

using UnityEngine;
using System.Collections;

public class ScreenTexture : MonoBehaviour {
 public int photoWidth = 50;
 public int photoHeight = 50;
 public int thumbScale=75;
 private int frameWidth;
 private int frameHeight;
 private int screenWidth;
 private int screenHeight;
 public int borderWidth=2;
 public Color borderColor = Color.white;
 private Texture2D texture;
 private Texture2D border;
 private bool shoot = false;

 // Use this for initialization
 void Start () {
  screenWidth = Screen.width;
  screenHeight = Screen.height;
  frameWidth = Mathf.RoundToInt(screenWidth * 0.01f * photoWidth);
  frameHeight = Mathf.RoundToInt(screenHeight * 0.01f * photoHeight);

  texture = new Texture2D (frameWidth, frameHeight, TextureFormat.RGB24, false);
  border = new Texture2D (1, 1, TextureFormat.ARGB32, false);
  border.SetPixel (0, 0, borderColor);
  border.Apply ();
 }
 
 // Update is called once per frame
 void Update () {
  if (Input.GetKeyUp (KeyCode.Mouse0)) 
  {
   StartCoroutine(ScreenCapture());
  }
 }

 IEnumerator ScreenCapture()
 {
  yield return new WaitForEndOfFrame();
  texture.ReadPixels(new Rect(
   screenWidth * 0.5f - frameWidth * 0.5f,
   screenHeight * 0.5f - frameHeight * 0.5f,
   frameWidth,
   frameHeight), 0, 0);
  texture.Apply();
  shoot=true;
 }

 void OnGUI()
 {
  GUI.DrawTexture (new Rect (
   screenWidth * 0.5f - frameWidth * 0.5f,
   screenHeight * 0.5f - frameHeight * 0.5f,
   frameWidth,
   borderWidth), border, ScaleMode.StretchToFill);

  GUI.DrawTexture (new Rect (
   screenWidth * 0.5f + frameWidth * 0.5f,
   screenHeight * 0.5f - frameHeight * 0.5f,
   borderWidth,
   frameHeight), border, ScaleMode.StretchToFill);

  GUI.DrawTexture (new Rect (
   screenWidth * 0.5f - frameWidth * 0.5f,
   screenHeight * 0.5f + frameHeight * 0.5f,
   frameWidth,
   borderWidth), border, ScaleMode.StretchToFill);

  GUI.DrawTexture (new Rect (
   screenWidth * 0.5f - frameWidth * 0.5f,
   screenHeight * 0.5f - frameHeight * 0.5f,
   borderWidth,
   frameHeight), border, ScaleMode.StretchToFill);

  if (shoot) 
  {
   GUI.DrawTexture(new Rect(10, 10, frameWidth * 0.01f * thumbScale, frameHeight * 0.01f * thumbScale), texture, ScaleMode.StretchToFill);
  }
 }
}

Now attach the script to the "Main Camera" in the "Hierarchy" panel in your unity project. Also with the "Main Camera" selected in the "Hierarchy" panel, select "Component->Camera Control->Camera Look" to add the Camera Look component to the "Main Camera". That's it.

The script works as follows. When user click the left mouse, the ScreenCapture() method is runned in a separate thread. In the ScreenCapture method, the texture object is updated (this texture object can be thought of as a in-memory image which holds the screen capture, the actual capturing is done by Texture2D.ReadsPixel() method), and the shoot flag is set to true. In the OnGUI, a frame is drawn at the center of the game screen indicating the area which will be capture, and if the shoot flag is true, then the texture (which now contains the screen capture) will be rendered.

No comments:

Post a Comment