- Home /
saving a screenshot to the recources folder
Hello, I am trying to take a screen cap. of my game and save it in a folder which I can later Access. How do I save a screen Capture with
ScreenCapture.CaptureScreenshot("Screenshot.png");
and save it to the Resources folder or even the Application.persistentDataPath. I know this is a verry stupid and easy quastion but I can't seem to find a simple answer. Thanks for your help in advanced .
Answer by KittenSnipes · Jan 28, 2018 at 05:56 AM
Well you should not be ashamed for asking for help. Here is a simple example script. Hope it helps and cheers mate.
using UnityEngine;
//System.IO;: You need this to find the files in your Directory
using System.IO;
public class Your Script Name Here: MonoBehaviour {
//screenCaptureKey: Change this to any key you want. It is easily changeable in the editor
public KeyCode screenCaptureKey = KeyCode.F2;
//ScreenCapDirectory: If you want a
//specific directory do something like this: "C:\\Users\\YourUserNameGoesHere\\Documents\\"
//Or if you want to use Application.persistentDataPath put it in the
//void Start() method and leave this string empty.
public string ScreenCapDirectory = "C:\\Users\\YourUserNameGoesHere\\Documents\\";
//The name of the screen capture taken. Put anything you want
public string ScreenCapName = "CoolPic";
//This will be what type of file your screen capture is. Another type besides png is jpg.
public string fileType = ".png";
//Private variables to get how many screenshots exist
private int count;
private int ScreenCaps;
void Start()
{
//Set them both to 0 at start
count = 0;
ScreenCaps = 0;
//This file path is C:\Users\YourUserNameHere\AppData\LocalLow\YourEnteredCompanyNameHere
ScreenCapDirectory = Application.persistentDataPath;
}
void Update () {
//ScreenCaps: Say we have 2 files with the same name as your ScreenCapName,
// Well then this would just tell us 2 of those files exist.
// Then we add that value to our ScreenCaps number to reference later.
ScreenCaps = (FindScreenCaptures(ScreenCapDirectory, ScreenCapName));
//If we press our capture key
if (Input.GetKeyDown(screenCaptureKey))
{
//This is how you save the screenshot to a certain directory and a certain name
//(ScreenCaps + 1): We reference this from above and use it for our picture name
// So if we know 2 files exist we add 1 to our value so it is a new picture.
ScreenCapture.CaptureScreenshot(ScreenCapDirectory + ScreenCapName + (ScreenCaps + 1) + fileType);
Debug.Log("ScreenCapture Taken!");
Debug.Log("ScreenCap Location: " + ScreenCapDirectory);
}
}
//This gets all the existing files from the Directory (DirectoryPath)
//with the FileName(FileName).
int FindScreenCaptures(string DirectoryPath, string FileName)
{
//Set count to 0 at every run so we count up from 0 to
//how many files exist with the FileName entered
count = 0;
//This loops through the files in your entered Directory
for (int i = 0; i < Directory.GetFiles(DirectoryPath).Length; i++)
{
//If any file has the same name as your picture
if (Directory.GetFiles(DirectoryPath)[i].Contains(FileName))
{
//Add 1 to the count because we need to know how many
//files with the same name exist
count += 1;
}
}
//Once we know we return that amount
return count;
}
}
Thanksssss a lot!!! Your so nice. Is the Application.persistentDataPath a permenent directory? SO if I try to access it when the player turns the game on for the sacond time, will the png file still be there?
The Application.persistentDataPath is a permanent path yes but the image will be saved over due to the name being the same. You can check to see if the image exists and if it does just add a 1 to it. I can help you with that if needed.
I fixed the script. Try it out and see if it works please. :D If it does make sure to mark this as correct. Cheers mate.
Your answer
Follow this Question
Related Questions
saving the game in position 1 Answer
PlayerPrefs Lmitations 2 Answers
Problem about reading and writing from text files 1 Answer
Where to find saves in the registry? 1 Answer
Working With GUI 1 Answer