Unity は、実際のニーズとターゲット プラットフォームに基づいて適切な保存パスを選択します。
string Name = DateTime.Now.ToString("yyyyMMddHHmmss");
string streamingAssetsPath = Application.streamingAssetsPath;
string filePath = Path.Combine(streamingAssetsPath, Name + ".png");
#if UNITY_EDITOR || UNITY_STANDALONE
// 如果是在Unity编辑器或Standalone平台上运行,将StreamingAssets路径更改为应用程序的可写入路径
filePath = Path.Combine(Application.persistentDataPath, Name + ".png");
#endif
File.WriteAllBytes(filePath, bytes);
Debug.Log("截图已保存至:" + filePath);
保存パスをデスクトップ上のパスに変更するには;
string Name = DateTime.Now.ToString("yyyyMMddHHmmss");
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string filePath = Path.Combine(desktopPath, Name + ".png");
File.WriteAllBytes(filePath, bytes);
Debug.Log("截图已保存至:" + filePath);
デスクトップ パスが存在するかどうかを確認し、その結果に基づいて保存パスを選択するには、次のコード例を使用します。
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string filePath;
if (Directory.Exists(desktopPath))
{
filePath = Path.Combine(desktopPath, Name + ".png");
}
else
{
string streamingAssetsPath = Application.streamingAssetsPath;
filePath = Path.Combine(streamingAssetsPath, Name + ".png");
}
File.WriteAllBytes(filePath, bytes);
Debug.Log("截图已保存至:" + filePath);
アプリケーションコードのシーンはスクリーンショットに保存されているので、分からない場合は前のスクリーンショットを確認してください。