Touch or click to take a screenshot in Unity 3D

Advertisemen

This video will show you guys how to take snap shot of screen in unity when mouse button is clicked.



Script used in the video above -


using UnityEngine;
using System.Collections;

public class screen : MonoBehaviour {
    Texture2D screenCap;
    Texture2D border;
    bool shot = false;

    // Use this for initialization
    void Start () {
        screenCap = new Texture2D(300200TextureFormat.RGB24false); // 1
        border = new Texture2D(22TextureFormat.ARGB32false); // 2
        border.Apply();
    }
    
    // Update is called once per frame
    void Update () {
        if(Input.GetKeyUp(KeyCode.Mouse0)){ // 3
            StartCoroutine("Capture");
            //Capture();
        }
    }

    void OnGUI(){
        GUI.DrawTexture(new Rect(2001003002), borderScaleMode.StretchToFill); // top
        GUI.DrawTexture(new Rect(2003003002), borderScaleMode.StretchToFill); // bottom
        GUI.DrawTexture(new Rect(2001002200), borderScaleMode.StretchToFill); // left
        GUI.DrawTexture(new Rect(5001002200), borderScaleMode.StretchToFill); // right

        if(shot){
            GUI.DrawTexture(new Rect(10106040), screenCapScaleMode.StretchToFill);
        }
    }

    IEnumerator Capture(){
        yield return new WaitForEndOfFrame();
        screenCap.ReadPixels(new Rect(19898298198), 00);
        screenCap.Apply();
        shot = true;
    }
}
mmmmmmmm

Inside the Start function, first line of code describes the texture with 300px of width and 200px of height but this doesn't display anything in the screen yet, second line of code describes the texture with 2px of width and 2px of height which we called it border.

Codes inside the Update function get call when you click left mouse button. On mouse click, it calls Capture function where first line of code commands to wait until the end of frame in the scene. Once scene is loaded it calls ReadPixels function to capture the pixels of the rectangle we described inside the Start function and last line of code converts the value of boolean variable shot to true.

In the OnGUI function, we draw the rectangle with the border texture described before in the Start function and when boolean variable named shot is true, we display texture of screen at 10px on x-axis and 10px on y-axis with the width of 60px and height of 40px.

Advertisemen

Disclaimer: Gambar, artikel ataupun video yang ada di web ini terkadang berasal dari berbagai sumber media lain. Hak Cipta sepenuhnya dipegang oleh sumber tersebut. Jika ada masalah terkait hal ini, Anda dapat menghubungi kami disini.

Tidak ada komentar:

Posting Komentar

© Copyright 2017 Tutorial Unity 3D