x


Take Screen Shot onGUI

Hello Everyone! I have attached the following code below to my camera and am able to take a screenshot by hitting the letter K. But how would I rework it to take a screenshot when the player clicks on a GUI button? Thank you for your input!
K.

var resWidth : int = 4096; 
var resHeight : int = 2232; 

function Update() 
{ 
   if (Input.GetKeyDown ("k")) 
   { 
       var rt = new RenderTexture(resWidth, resHeight, 24);    
       camera.targetTexture = rt; 
       var screenShot = new Texture2D(resWidth, resHeight, TextureFormat.RGB24, false); 
       camera.Render(); 
       RenderTexture.active = rt;
       screenShot.ReadPixels(Rect(0, 0, resWidth, resHeight), 0, 0); 
       RenderTexture.active = null; // JC: added to avoid errors 
       camera.targetTexture = null;
       Destroy(rt);
       var bytes = screenShot.EncodeToPNG(); 
       System.IO.File.WriteAllBytes(Application.dataPath + "/screenshots/screen" + System.DateTime.Now.ToString("dd-MM-yyyy_HH-mm-ss") + ".png", bytes); 
   }    
}

more ▼

asked Feb 06, 2011 at 01:11 AM

user-8437 (google) gravatar image

user-8437 (google)
1 12 8 8

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Instead of Update, use OnGUI, and instead of Input.GetKeyDown, use GUI.Button.

For more info, see docs.

more ▼

answered Feb 06, 2011 at 06:46 AM

yoyo gravatar image

yoyo
8.2k 59 67 124

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x420
x174

asked: Feb 06, 2011 at 01:11 AM

Seen: 2533 times

Last Updated: Feb 06, 2011 at 01:15 AM