Application.Capturescreenshot(fileName)保存到特定路径

时间:2014-07-20 13:40:18

标签: unity3d screenshot

我在我的Unity项目中使用这种将摄像机视口保存到文件系统的方法:

Application.Capturescreenshot(fileName) 

它工作得很好,但我希望它能在特定的路径下保存。 例如:Application.Capturescreenshot(c:/screenshots/filename); 我怎么能管理这个? thnaks!

2 个答案:

答案 0 :(得分:3)

您可以通过将其作为字符串传递来轻松使用绝对文件路径。在您的情况下,使用此语句应该可以正常工作:

Application.CaptureScreenshot("C:/screenshots/filename");

请注意,如果 screeshots 文件夹不存在,则会出错。因此,如果您不确定,可以相应地修改代码:

// File path
string folderPath = "C:/screenshots/";
string fileName = "filename";

// Create the folder beforehand if not exists
if(!System.IO.Directory.Exists(folderPath))
    System.IO.Directory.CreateDirectory(folderPath);

// Capture and store the screenshot
Application.CaptureScreenshot(folderPath + fileName);

最后,如果要使用相对路径而不是绝对路径,Unity会提供dataPathpersistentDataPath变量来访问项目的数据文件夹路径。因此,如果要将屏幕截图存储在数据文件夹中,可以相应地更改上面的 folderPath

string folderPath = Application.dataPath + "/screenshots/"; 

答案 1 :(得分:1)

        var nameScreenshot = "Screenshot_" + DateTime.Now.ToString("dd-mm-yyyy-hh-mm-ss") + ".png";
        ScreenCapture.CaptureScreenshot(nameScreenshot);

试试这个!

相关问题