Screenshot Script

Hi everyone, this is my first post on the forums, so my apologies if its in the wrong place.

Anyway, I’m still kind of learning Javascript, and I had been struggling for hours trying to figure out a way to take a screenshot in-game and place it in a folder on the player’s PC named something like, for example, “screenshot”. But if the file already existed, I wanted to add a number to the end of the filename to make it unique. (Like “screenshot_1”.) And if that one also existed, then just keep increasing the number until the new filename was unique. After searching and searching for an explanation as to how to achieve this, I finally decided to try and figure it out for myself. So I looked at different bits of code around the internet, and finally I got it to work. I had seen several other people asking how to do this who never got an answer, so I decided to post my solution on the forums. Now I’m pretty sure this will only work on games made for PC, but I could be wrong. So anyone who would like to chime in and confirm or deny this is certainly welcome to.

Make sure you change the dataPath to the folder you want your screenshots to be saved to! (Remove the “Folder within game root”, but leave the slash.)

Javascript

function TakeScreenshot () {

var i : int = 0;
var dataPath = "**Folder within game root**/";

    while (System.IO.File.Exists(dataPath + "screenshot_" + i + ".png"))
    {
	i++;
    }

    if (!System.IO.File.Exists(dataPath + "screenshot_" + i + ".png"))
    {
	Application.CaptureScreenshot(dataPath + "screenshot_" + i + ".png");
    }
}

Then of course you would call the “TakeScreenshot” function from somewhere else.

C#

public void TakeScreenshot () {//TakeScreenshot must be also the name of the file in c#
public int i = 0;
     while (System.IO.File.Exists(Application.dataPath + "screenshot_" + i + ".png")) {
              i++;
    }
 
    if  (!System.IO.File.Exists(Application.dataPath+ "screenshot_" + i + ".png"))
          Application.CaptureScreenshot(dataPath + "screenshot_" + i + ".png");
}

why not to write

public void TakeScreenshot () {//TakeScreenshot must be also the name of the file in c#
public int i = 0;
     while (System.IO.File.Exists(Application.dataPath + "screenshot_" + i + ".png")) {
              i++;
    }

    if  (!System.IO.File.Exists(Application.dataPath+ "screenshot_" + i + ".png"))
          Application.CaptureScreenshot(dataPath + "screenshot_" + i + ".png");
}

it shhould work, btw u should call it in game so u could write in a generic update function

if(Input.GetKeyDown(KeyCode.f1))
                 TakeScreenshot();