C# Directory.createDirectory在iOS中创建文件而不是目录
我必须在iOS设备上为Unity游戏本地保存一些数据。统一提供C# Directory.createDirectory在iOS中创建文件而不是目录,c#,ios,iphone,unity3d,directory,C#,Ios,Iphone,Unity3d,Directory,我必须在iOS设备上为Unity游戏本地保存一些数据。统一提供 Application.persistentDataPath 获取公共目录以保存数据。在控制台上打印它表明,为iOS返回的路径是正确的,即/Users/userName/Library/Developer/CoreSimulator/Devices/*********-***-***-***-***-***-***-***-***-***-*********-******-***-******-******-******-
Application.persistentDataPath
获取公共目录以保存数据。在控制台上打印它表明,为iOS返回的路径是正确的,即/Users/userName/Library/Developer/CoreSimulator/Devices/*********-***-***-***-***-***-***-***-***-***-*********-******-***-******-******-******-***-******-******-***-***-***-***-***-***-***-***-***-***-***-***-***-/Documents/
因此,一个函数返回路径,另一个函数检查目录是否存在,如果不存在,它应该创建一个目录,但它创建的文件没有任何扩展名。这是我的密码
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");
}
可能的解决办法:
1Path.Combine(Application.persistentDataPath,“savedpos”)代码>在保存的操作系统之前添加反斜杠,而其他操作系统是正斜杠。这可能会导致iOS出现问题。尝试在不使用路径的情况下连接原始字符串。合并函数
static string poseDirectoryPath() {
return Application.persistentDataPath + "/" + "SavedPoses";
}
2。如果目录
类无法正常工作,请使用目录信息
类
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上的权限问题
您可以在目录中创建文件夹。您有读取和写入此目录的权限
访问此文件的一般方法是使用Application.StreamingAssetPath/
在iOS上,还可以通过Application.dataPath+“/Raw”
访问它
在Android上,还可以使用“”jar:file://“+Application.dataPath+”!/assets/”代码>和适用于Windows和Mac,并带有Application.dataPath+“/StreamingAssets”代码>。就用一个适合你的
对于您的问题,Application.dataPath+“/Raw”+“/SavedPoses”代码>应该这样做。1。这是我的原始代码,在检查另一个问题后进行了更改。正如我所提到的,生成的路径在两种情况下(原始字符串和path.Combine)2都是正确的,这也会产生同样的问题。这是什么Unity版本,你测试它的iOS版本是什么?Unity=5.4.1f1,Xcode=7.3.1,Simulator=iPhone 5S,iOS=9.3。同样的问题也发生在设备上。我正在使用模拟器,以便从Finder查看文档目录。您的版本看起来不错。尝试Application.dataPath+“/Raw”+“/SavedPoses”代码>让我知道。这会影响发布版本吗?不。做了编辑。请看编辑后的答案。这很好地解释了这一点。该位置位于StreamingAssets
文件夹中,您可以对其进行读取和写入。这完全没有问题。这个问题是关于iOS的,这个解决方案只适用于iOS。您可以使用Application.streamingAssetPath
使其在编辑答案中提到的每个平台上工作。