TakeScreenshot

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Fixed script to use Input.GetKeyDown().)
m (Minor tweak to match naming conventions in other wiki scripts.)
Line 21: Line 21:
 
public class TakeScreenshot : MonoBehaviour
 
public class TakeScreenshot : MonoBehaviour
 
{     
 
{     
     private int _screenshotCount = 0;
+
     private int screenshotCount = 0;
 
          
 
          
 
     // Check for screenshot key each frame
 
     // Check for screenshot key each frame
Line 32: Line 32:
 
             do
 
             do
 
             {
 
             {
                 _screenshotCount++;
+
                 screenshotCount++;
                 screenshotFilename = "screenshot" + _screenshotCount + ".png";
+
                 screenshotFilename = "screenshot" + screenshotCount + ".png";
  
 
             } while (System.IO.File.Exists(screenshotFilename));
 
             } while (System.IO.File.Exists(screenshotFilename));

Revision as of 00:30, 13 August 2006

Description

Captures sequentially numbered screenshots when a function key is pressed. Existing screenshots are not overwritten.

Usage

Just attach this script to an empty game object.

C# - TakeScreenshot.cs

<csharp>// TODO: // By default, screenshot files are placed next to the executable bundle -- we don't want this in a // shipping game, as it will fail if the user doesn't have write access to the Applications folder. // Instead we should place the screenshots on the user's desktop. However, the ~/ notation doesn't // work, and Unity doesn't have a mechanism to return special paths. Therefore, the correct way to // solve this is probably with a plug-in to return OS specific special paths.

using UnityEngine; using System.Collections;

public class TakeScreenshot : MonoBehaviour {

   private int screenshotCount = 0;
       
   // Check for screenshot key each frame
   void Update()
   {
       // take screenshot on up->down transition of F9 key
       if (Input.GetKeyDown("f9"))
       {        
           string screenshotFilename;
           do
           {
               screenshotCount++;
               screenshotFilename = "screenshot" + screenshotCount + ".png";
           } while (System.IO.File.Exists(screenshotFilename));
           
           Application.CaptureScreenshot(screenshotFilename);
       }
   }

} </csharp>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox