Unity将资源保存到桌面路径,若pc桌面路径不存在,则保存到streamingAssetsPath;

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);

应用代码场景在截图保存里,请不理解的到上一篇截图查看

猜你喜欢

转载自blog.csdn.net/weixin_44047050/article/details/130924021