Unity elige la ruta de guardado adecuada según sus necesidades reales y la plataforma de destino
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);
Para cambiar la ruta de guardado a la ruta de su escritorio;
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);
Para determinar si la ruta del escritorio existe y seleccionar una ruta para guardar según el resultado, use el siguiente código de ejemplo
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);
La escena del código de la aplicación se encuentra en la captura de pantalla guardada. Si no comprende, vaya a la captura de pantalla anterior para verla.