TakeScreenshot

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(C# - TakeScreenshot.cs)
(C# - TakeScreenshot.cs)
Line 41: Line 41:
 
             Application.CaptureScreenshot(screenshotFilename);
 
             Application.CaptureScreenshot(screenshotFilename);
 
         }
 
         }
 +
    }
 +
}
 +
</csharp>
 +
 +
 +
==C# - Screenshot.cs==
 +
 +
<csharp>
 +
// Default method have some problem, when you take a Screen shot for your game.
 +
// So add this script.
 +
// CF Page : http://technology.blurst.com/unity-jpg-encoding-javascript/
 +
// made by Jerry ( sdragoon@nate.com )
 +
 +
using UnityEngine;
 +
using System.Collections;
 +
using System.IO;
 +
 +
public class ScreenShot : MonoBehaviour
 +
{
 +
    private int count = 0;
 +
 +
    void Update()
 +
    {
 +
        if (Input.GetKeyDown("k"))
 +
            StartCoroutine(ScreenshotEncode());
 +
    }
 +
 +
    IEnumerator ScreenshotEncode()
 +
    {
 +
        // wait for graphics to render
 +
        yield return new WaitForEndOfFrame();
 +
 +
        // create a texture to pass to encoding
 +
        Texture2D texture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
 +
 +
        // put buffer into texture
 +
        texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
 +
        texture.Apply();
 +
 +
        // split the process up--ReadPixels() and the GetPixels() call inside of the encoder are both pretty heavy
 +
        yield return 0;
 +
 +
        byte[] bytes = texture.EncodeToPNG();
 +
 +
        // save our test image (could also upload to WWW)
 +
        File.WriteAllBytes(Application.dataPath + "/../testscreen-" + count + ".png", bytes);
 +
        count++;
 +
 +
        //Debug.Log( Application.dataPath + "/../testscreen-" + count + ".png" );
 
     }
 
     }
 
}
 
}
 
</csharp>
 
</csharp>

Revision as of 07:18, 11 January 2011

Contents

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.

// Mono/.NET has functions to get special paths... see discussion page. --Aarku

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>


C# - Screenshot.cs

<csharp> // Default method have some problem, when you take a Screen shot for your game. // So add this script. // CF Page : http://technology.blurst.com/unity-jpg-encoding-javascript/ // made by Jerry ( sdragoon@nate.com )

using UnityEngine; using System.Collections; using System.IO;

public class ScreenShot : MonoBehaviour {

   private int count = 0;
   void Update()
   {
       if (Input.GetKeyDown("k"))
           StartCoroutine(ScreenshotEncode());
   }
   IEnumerator ScreenshotEncode()
   {
       // wait for graphics to render
       yield return new WaitForEndOfFrame();
       // create a texture to pass to encoding
       Texture2D texture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
       // put buffer into texture
       texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
       texture.Apply();
       // split the process up--ReadPixels() and the GetPixels() call inside of the encoder are both pretty heavy
       yield return 0;
       byte[] bytes = texture.EncodeToPNG();
       // save our test image (could also upload to WWW)
       File.WriteAllBytes(Application.dataPath + "/../testscreen-" + count + ".png", bytes);
       count++;
       //Debug.Log( Application.dataPath + "/../testscreen-" + count + ".png" );
   }

} </csharp>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox