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

该代码示例展示了在Unity中如何根据运行环境选择图片的保存路径。如果在编辑器或独立应用环境下,图片会被保存到持久化数据路径;否则,将保存到用户的桌面。首先尝试保存到桌面,如果桌面路径不存在,则回退到StreamingAssetsPath进行保存。
摘要由CSDN通过智能技术生成

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

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

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值