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(300, 200, TextureFormat.RGB24, false); // 1
border = new Texture2D(2, 2, TextureFormat.ARGB32, false); // 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(200, 100, 300, 2), border, ScaleMode.StretchToFill); // top
GUI.DrawTexture(new Rect(200, 300, 300, 2), border, ScaleMode.StretchToFill); // bottom
GUI.DrawTexture(new Rect(200, 100, 2, 200), border, ScaleMode.StretchToFill); // left
GUI.DrawTexture(new Rect(500, 100, 2, 200), border, ScaleMode.StretchToFill); // right
if(shot){
GUI.DrawTexture(new Rect(10, 10, 60, 40), screenCap, ScaleMode.StretchToFill);
}
}
IEnumerator Capture(){
yield return new WaitForEndOfFrame();
screenCap.ReadPixels(new Rect(198, 98, 298, 198), 0, 0);
screenCap.Apply();
shot = true;
}
}mmmmmmmm
using System.Collections;
public class screen : MonoBehaviour {
Texture2D screenCap;
Texture2D border;
bool shot = false;
// Use this for initialization
void Start () {
screenCap = new Texture2D(300, 200, TextureFormat.RGB24, false); // 1
border = new Texture2D(2, 2, TextureFormat.ARGB32, false); // 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(200, 100, 300, 2), border, ScaleMode.StretchToFill); // top
GUI.DrawTexture(new Rect(200, 300, 300, 2), border, ScaleMode.StretchToFill); // bottom
GUI.DrawTexture(new Rect(200, 100, 2, 200), border, ScaleMode.StretchToFill); // left
GUI.DrawTexture(new Rect(500, 100, 2, 200), border, ScaleMode.StretchToFill); // right
if(shot){
GUI.DrawTexture(new Rect(10, 10, 60, 40), screenCap, ScaleMode.StretchToFill);
}
}
IEnumerator Capture(){
yield return new WaitForEndOfFrame();
screenCap.ReadPixels(new Rect(198, 98, 298, 198), 0, 0);
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
Tidak ada komentar:
Posting Komentar