Unity3D移动平台文件路径权限

经常会用到一些路径读写文件,但是各个平台上一些路径对读写的支持有可能不同,下面给出不同平台unity提供的地址的具体路径,以及一些读写权限

1.Editor:

dataPathD:/Documents/Xuporter/Assets

persistentDataPathC:/Users/Administrator/AppData/LocalLow/mj/path

streamingAssetsPathD:/Documents/Xuporter/Assets/StreamingAssets

temporaryCachePathC:/Users/ADMINI~1/AppData/Local/Temp/mj/path

2.Android:

方法文件路径读写权限

dataPath/data/app/com.mi.path-1.apk无权限

persistentDataPath/data/data/com.mi.path/files读写,强推荐

streamingAssetsPathjar:file:///data/app/com.mi.path-1.apk!/assets只读

temporaryCachePath/data/data/com.mi.path/cache读写

我的包名是com.mi.path,可以看出android平台的路径和包名的关系,其实persistentDataPath和temporaryCachePath对应的路径就是sd卡或手机存储的Android/data/com.mi.path/files和Android/data/com.mi.path/cache路径

3.Iphone:

/var/mobile/Containers/Data/Application/FFEEF1E0-E15E-4BC0-9E8F-78084A2085A0/Documents

方法文件路径读写权限

dataPath/var/mobile/Containers/Bundle/Application/AFE239B4-2FE5-48B5-8A31-FC23FEDA0189/ad.app/Data无权限

persistentDataPath/var/mobile/Containers/Data/Application/FFEEF1E0-E15E-4BC0-9E8F-78084A2085A0/Documents读写,强推荐

streamingAssetsPath/var/mobile/Containers/Bundle/Application/AFE239B4-2FE5-48B5-8A31-FC23FEDA0189/ad.app/Data/Raw只读

temporaryCachePath/var/mobile/Containers/Data/Application/FFEEF1E0-E15E-4BC0-9E8F-78084A2085A0/Library/Caches读写

虽然temporaryCachePath目录下也能读写,但是建议只用于缓存临时文件用,因为在磁盘空间不足的情况下,ios会优先删除此处文件

官方描述:

Cached data should be stored in the/Library/Cachesdirectory. Examples of files you should put in theCachesdirectory include (but are not limited to) database cache files and downloadable content, such as that used by magazine, newspaper, and map apps. Your app should be able to gracefully handle situations where cached data is deleted by the system to free up disk space.

https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/PerformanceTips/PerformanceTips.html#//apple_ref/doc/uid/TP40007072-CH7-SW17

persistentDataPath 目录下的内容会自动备份的iCloud,所以最好开始不要有太大的文件放到这,如果需要放置比较大的文件,可以关闭iCloud自动备份,这篇文章有关于怎么关闭自动备份的方法http://blog.csdn.net/yhy2218/article/details/50595023

从dataPath和streamingAssetsPath可以看出,其实streamingAssetsPath还可以根据dataPath得出

#ifUNITY_EDITOR

stringfilepath

= Application.dataPath +"/StreamingAssets"+"/version.txt";

#elif

UNITY_IPHONE

stringfilepath

= Application.dataPath +"/Raw"+"/my.xml";

#elif

UNITY_ANDROID

stringfilepath

=“jar:file://”+

Application.dataPath +"!/assets/"+"/version.txt";

#endif

不过用streamingAssetsPath就能得道的路径为啥要用dataPath啊

StreamingAssets在各个平台上的文本支持读取方式

string path = System.IO.Path.Combine(Application.streamingAssetsPath,“version.txt”);

c#WWW

Editor支持支持

Android不支持支持

Ios支持支持

Editor:

1::System.IO.File.ReadAllText (path);

2:path = “file://”+path;或者path = “file:/”+path;或者path = “file:\”+path或者path = “file:\\”+path;在win7上都可以在mac上path = “file:/”+path不可以外,别的都可以

然后WWW www = new WWW (path);

Android:

1:WWW www = new WWW (path);

iphone:

1:System.IO.File.ReadAllText (path);

2:path = “file://”+path;System.IO.File.ReadAllText (path);

还有一点就是在读取streamingAssets目录下的文本文件时android平台必须要用www来读取哦,因为android平台是被压缩在apk中,所以不能直接用c#去读,ios可以直接用System.IO.File.ReadAllText(filePath)

作者:TakakuraKenSan
链接:https://www.jianshu.com/p/0fc5c4c5f8ed
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

你可能感兴趣的:(Unity)