目录中.createDirectory在iOS中创建文件而不是目录

本文关键字:文件 iOS createDirectory 创建 | 更新日期: 2023-09-27 18:11:45

我必须为Unity游戏在iOS设备上本地保存一些数据。统一提供

    Application.persistentDataPath

获取保存数据的公共目录。印刷在控制台显示,这条路返回iOS是正确的。/用户/用户名/图书馆/开发/CoreSimulator/设备/********-****-****-****-************/数据/集装箱/数据/应用程序/********-****-****-****-************/文档/

所以一个函数返回路径,另一个检查目录是否存在,如果不存在,它应该创建一个目录,但它创建没有任何扩展名的FILE。这是我的代码

    void savePose(Pose pose){
        if (!Directory.Exists(PoseManager.poseDirectoryPath())){
            Directory.CreateDirectory(PoseManager.poseDirectoryPath());
            //Above line creates a file  
            //by the name of "SavedPoses" without any extension
        }
        // rest of the code goes here
    }
    static string poseDirectoryPath() {
        return Path.Combine(Application.persistentDataPath,"SavedPoses");
    }

目录中.createDirectory在iOS中创建文件而不是目录

可能的解决方案:

1

Path.Combine(Application.persistentDataPath,"SavedPoses");savedPoses前加反斜杠,其他为正斜杠。这可能会在iOS上造成问题。尝试不使用Path.Combine函数的原始字符串连接。

static string poseDirectoryPath() {
    return Application.persistentDataPath + "/" + "SavedPoses";
}
2

。如果Directory类不能正常工作,请使用DirectoryInfo类。

private void savePose(Pose pose)
{
    DirectoryInfo posePath = new DirectoryInfo(PoseManager.poseDirectoryPath());
    if (!posePath.Exists)
    {
        posePath.Create();
        Debug.Log("Directory created!");
    }
}
static string poseDirectoryPath()
{
    return Path.Combine(Application.persistentDataPath, "SavedPoses");
}

编辑:

可能是iOS的权限问题。

您应该在StreamingAssets目录下创建文件夹。您有对该目录进行读写的权限

一般的方法是使用Application.streamingAssetsPath/

在iOS上,它也可以通过Application.dataPath + "/Raw"访问。

在Android上,它也可以用"jar:file://" + Application.dataPath + "!/assets/";访问,在Windows和Mac上用Application.dataPath + "/StreamingAssets";访问。用最适合你的。

对于你的问题,Application.dataPath + "/Raw"+"/SavedPoses";应该做。