经常会用到一些路径读写文件,但是各个平台上一些路径对读写的支持有可能不同,下面给出不同平台unity提供的地址的具体路径,以及一些读写权限
1.Editor:
dataPath | D:/Documents/Xuporter/Assets |
persistentDataPath | C:/Users/Administrator/AppData/LocalLow/mj/path |
streamingAssetsPath | D:/Documents/Xuporter/Assets/StreamingAssets |
temporaryCachePath | C:/Users/ADMINI~1/AppData/Local/Temp/mj/path |
方法 | 文件路径 | 读写权限 |
---|---|---|
dataPath | /data/app/com.mi.path-1.apk | 无权限 |
persistentDataPath | /data/data/com.mi.path/files | 读写,强推荐 |
streamingAssetsPath | jar: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 | 读写 |
官方描述:
-
Cached data should be stored in the <Application_Data>
/Library/Caches
directory. Examples of files you should put in theCaches
directory 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.youkuaiyun.com/yhy2218/article/details/50595023
从dataPath和streamingAssetsPath可以看出,其实streamingAssetsPath还可以根据dataPath得出
#
if
UNITY_EDITOR
string
filepath = Application.dataPath +
"/StreamingAssets"
+
"/version.txt"
;
#elif UNITY_IPHONE
string
filepath = Application.dataPath +
"/Raw"
+
"/my.xml"
;
#elif UNITY_ANDROID
string
filepath =
"jar:file://"
+ Application.dataPath +
"!/assets/"
+"/version.txt";
#endif
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);