TakeScreenshot

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(C# - TakeScreenshot.cs)
m (Text replace - "</csharp>" to "</syntaxhighlight>")
 
(3 intermediate revisions by 3 users not shown)
Line 9: Line 9:
 
==C# - TakeScreenshot.cs==
 
==C# - TakeScreenshot.cs==
  
<csharp>// TODO:
+
<syntaxhighlight lang="csharp">// TODO:
 
// By default, screenshot files are placed next to the executable bundle -- we don't want this in a
 
// 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.
 
// shipping game, as it will fail if the user doesn't have write access to the Applications folder.
Line 43: Line 43:
 
     }
 
     }
 
}
 
}
</csharp>
+
</syntaxhighlight>
  
  
 
==C# - Screenshot.cs==
 
==C# - Screenshot.cs==
  
<csharp>
+
<syntaxhighlight lang="csharp">
 +
// ******  Notice : It doesn't works in Wep Player environment.  ******
 +
// ******    It works in PC environment.                        ******
 
// Default method have some problem, when you take a Screen shot for your game.  
 
// Default method have some problem, when you take a Screen shot for your game.  
 
// So add this script.
 
// So add this script.
Line 88: Line 90:
 
         File.WriteAllBytes(Application.dataPath + "/../testscreen-" + count + ".png", bytes);
 
         File.WriteAllBytes(Application.dataPath + "/../testscreen-" + count + ".png", bytes);
 
         count++;
 
         count++;
 +
 
 +
        // Added by Karl. - Tell unity to delete the texture, by default it seems to keep hold of it and memory crashes will occur after too many screenshots.
 +
        DestroyObject( texture );
  
 
         //Debug.Log( Application.dataPath + "/../testscreen-" + count + ".png" );
 
         //Debug.Log( Application.dataPath + "/../testscreen-" + count + ".png" );
 
     }
 
     }
 
}
 
}
</csharp>
+
</syntaxhighlight>

Latest revision as of 20:47, 10 January 2012

Contents

[edit] Description

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

[edit] Usage

Just attach this script to an empty game object.

[edit] C# - TakeScreenshot.cs

// 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);
        }
    }
}


[edit] C# - Screenshot.cs

// ******  Notice : It doesn't works in Wep Player environment.  ******
// ******    It works in PC environment.                         ******
// 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++;
 
        // Added by Karl. - Tell unity to delete the texture, by default it seems to keep hold of it and memory crashes will occur after too many screenshots.
        DestroyObject( texture );
 
        //Debug.Log( Application.dataPath + "/../testscreen-" + count + ".png" );
    }
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox