- Home /
Application.CaptureScreenshot() Fails on PC/Mac
I have an app with a screenshot button which calls Application.CaptureScreenshot(), with an oversize of 4. It works fine within the editor but in my builds no screenshot file is created. The app pauses while taking a screen (so I know the code is still called), but no file appears to be created anywhere that I can locate.
I've tried simply providing a filename itself (which in the editor causes the file to be created in the project root folder), and appending Application.persistentDataPath (which in the editor causes the file to be created in the same place as all other save data). In neither case does any file appear when running a build.
Unity 5.1.1, Windows and Mac.
Anyone have any ideas on this?
Do you get any errors in the log file? Are we talking about web or standalone build?
Standalone builds for either $$anonymous$$ac OS or Windows.
I hadn't thought about the player logs, but I checked now and don't see any messages about it.
Could you try to build your app with "Development build" option enabled?
I tried this script and works fine (I'm using $$anonymous$$ac):
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Answer_1011955 : $$anonymous$$onoBehaviour {
static int numScreenshot = 1;
public Text pathText = null;
public void TakeScreenshot(){
Application.CaptureScreenshot(Application.dataPath+"screenshot"+numScreenshot.ToString()+".png" , 4);
pathText.text = "Screenshot saved into " + Application.dataPath+"screenshot"+numScreenshot.ToString()+".png";
numScreenshot++;
}
}
Hi Jordi & Paulius,
After lots of testing I've realised that I was mistaken about what was happening in Windows (just got mixed up) and the problem is centred around $$anonymous$$ac OSX.
For each platform I tried saving 3 ways: by appending Application.dataPath (i.e. Application.dataPath + "/" + "filename.png"), by appending Application.persistentDataPath, and with just the filename with no folder appended up front.
Here were my results...
Windows:
dataPath: goes to the data folder under the .exe — not an ideal place for a screenshot, but okay
persistentDataPath: goes into the persistent data folder as expected! (good)
no folder: goes to the data folder under the .exe
$$anonymous$$ac:
dataPath: puts the screen inside the .app package, which is indeed where the data is, although this is not a location users should ever have to look for screenshots
persistentDataPath: goes into a persistent data path, BUT this is a different persistent data path than the one used when running in Editor!
no folder: file cannot be found anywhere! (no messages in log)
So, in short, there are two issues here, both of them $$anonymous$$ac-specific:
The value of Application.persistentDataPath changes between Editor mode and a stand alone build.
This seems unintentional. In my case (company: Crafted Reveries Limited, app name: Shu's Garden) Editor mode resolves the path as "Library/Application Support/Crafted Reveries Limited/Shu_s Garden/". However, the stand alone build resolves it as "Library/Application Support/unity.Crafted Reveries Limited.Shu's Garden/".
That last bit seems like a bug in Unity.
When the full path isn't specified, the file doesn't go to a default location.
It seems to go nowhere.
Try using this;
Application.CaptureScreenshot(Path.Combine(Application.persistentDataPath, fileName));
Working in my Unity Editor 5.2.1
Answer by Jordi-Bonastre · Jul 24, 2015 at 08:35 AM
Hi Jason. I am running Unity 5.1.2p1 and if I use this code, Unity saves the screenshot on the project folder.
Application.CaptureScreenshot("ScreenshotNEW.png" , 4);
PersistentDataPath is a directory path where data expected to be kept between runs can be stored http://docs.unity3d.com/ScriptReference/Application-persistentDataPath.html
dataPath contains the path to the game data folder http://docs.unity3d.com/ScriptReference/Application-dataPath.html
Could you try CaptureScreenshot function only using the name of the screenshot and tell me which version of Unity are you running?
It does that when running in Editor mode, yes. But as I said above, this isn't the behaviour in stand alone builds. On Windows, it is saved in the data folder in this case, and on $$anonymous$$ac I cannot locate the file anywhere.
Answer by Aqibsadiq · Apr 12, 2016 at 04:44 AM
@Jordi Bonastre @Jason RT Bond Try This Guys Here is simple Code for capturing screen shot, and showing the captured image on screen This Code is For Andriod and IPhone.For PC Just Give the Image Path
public RawImage image;
void Start()
{
Photo();
Invoke("GetPhoto",1f);
}
public void Photo()
{
Application.CaptureScreenshot(Variables.ImageName);
}
public void GetPhoto()
{
#if !UNITY_EDITOR
string url = Application.persistentDataPath +"/"+Variables.ImageName;
var bytes = File.ReadAllBytes( url );
Texture2D texture = new Texture2D( 73, 73 );
texture.LoadImage( bytes );
image.texture= texture ;
#endif
}