I wanted to be able to saved current frames with a key event. I manage to make it work for local builds using `_path= Directory.GetCurrentDirectory();```` and ````screenCapture.CaptureScreenshot(_path)
But after cycle: compress app-upload it to drive-download from drive-uncompress-run fails.
I tried different solutions of paths, and alternative method for CaptureScreenshot, but couldn’t make it work.
I would really like to know what would be the correct option for downloading a screenshot in same directory from where I open the app.
Thanks
Here is the code for ScreenshotHandler, with different things a tried:
public class ScreenshotHandler : MonoBehaviour
{
private int id =0;
public static string GetApplicationRoot()
{
var exePath = new Uri(System.Reflection.
Assembly.GetExecutingAssembly().CodeBase).LocalPath;
return new FileInfo(exePath).DirectoryName;
}
string path;
void Start(){
// Approaches:
/*
1- Building inside Downloads : FAILS
string homeRoot = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
string timestamp = DateTime.Now.ToString("yyyy-MM-dd");
string outputFolder = "/Downloads/ProceduralWorlds-" + timestamp;
string newFolder = .Path.Combine(homeRoot, outputFolder);
path=newFolder;
var info = Directory.CreateDirectory(path);
Debug.Log("Info"+info);
*/
/*
2- Using current directoyry working for local builds: : WORKS ON LOCAL BUILDS, FAILS AFTER CYCLE: COMPRESS-UPLOAD-DOWNLOAD-UNCOMPRESS
path = Directory.GetCurrentDirectory();
*/
/*
3- Using Application.dataPath : FAILS
path = Application.dataPath;
if (Application.platform == RuntimePlatform.OSXPlayer) {
path += "/../..";
}
else if (Application.platform == RuntimePlatform.WindowsPlayer) {
path += "/..";
}
*/
/*
4- Using Reflection.Assembly : FAILS
path =System.Reflection.Assembly.GetExecutingAssembly().Location;
*/
}
IEnumerator captureScreenshot(string path)
{
yield return new WaitForEndOfFrame();
Texture2D screenImage = new Texture2D(Screen.width, Screen.height);
screenImage.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
screenImage.Apply();
byte[] imageBytes = screenImage.EncodeToPNG();
System.IO.File.WriteAllBytes(path, imageBytes);
}
public void Capture()
{
string _path = path +"/Screenshot_"+id+".png";
Debug.Log(_path);
// Use alternative method for screencapture : FAILS
// captureScreenshot(_path);
ScreenCapture.CaptureScreenshot(_path);
id ++;
}
}