unity打开文件

这段代码展示了如何使用C#在不同平台上检测文件夹路径是否存在,并根据平台启动文件资源管理器以打开它。针对Windows和OSX编辑器/玩家,使用explorer或open命令,对其他平台则给出警告。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public static void OpenFolder(string filePath)
{
    var folderPath = Path.GetDirectoryName(filePath);
    // 检查文件夹路径是否存在
    if (Directory.Exists(folderPath))
    {
        // 根据当前平台选择打开文件资源管理器的命令
        string explorerCommand = "";
        if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer)
        {
            explorerCommand = "explorer";
        }
        else if (Application.platform == RuntimePlatform.OSXEditor || Application.platform == RuntimePlatform.OSXPlayer)
        {
            explorerCommand = "open";
            // 添加 -R 参数以打开文件夹
            folderPath = "-R \"" + folderPath + "\"";
        }
        else
        {
            Debug.LogWarning("该平台不支持打开文件夹");
            return;
        }

        // 打开文件资源管理器并指定文件夹路径
        System.Diagnostics.Process.Start(explorerCommand, folderPath);
    }
    else
    {
        Debug.LogError("文件夹路径不存在:" + folderPath);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值