一、各路径的定义:html
a、Resources路径
Resources文件夹是Unity里自动识别的一种文件夹,可在Unity编辑器的Project窗口里建立,并将资源放置在里面。Resources文件夹下的资源无论是否有用,所有会打包进.apk或者.ipa,而且打包时会将里面的资源压缩处理。加载方法是Resources.Load<T>(文件名),须要注意:文件名不包括扩展名,打包后不能更改Resources下的资源内容,可是从Resources文件夹中加载出来的资源
能够更改。
b、
Application.dataPath
路径
这个属性返回的是程序的数据文件所在文件夹的路径,例如在Editor中就是项目的Assets文件夹的路径,经过这个路径能够访问项目中任何文件夹中的资源,可是在移动端它是彻底没用。
c、
Application.streamingAssetsPath路径
这个属性用于返回流数据的缓存目录,返回路径为相对路径,适合设置一些外部数据文件的路径。在Unity工程的Assets目录下起一个名为“StreamingAssets”的文件夹便可,而后用Application.streamingAssetsPath访问,这个文件夹中的资源在打包时会原封不动的打包进去,不会压缩,通常放置一些资源数据。在PC/MAC中可实现对文件的“增删改查”等操做,但在移动端是一个只读路径。
d、
Application.persistentDataPath路径(
推荐使用)
此属性返回一个持久化数据存储目录的路径,能够在此路径下存储一些持久化的数据文件。这个路径可读、可写,可是只能在程序运行时才能读写操做,不能提早将数据放入这个路径。在IOS上是应用程序的沙盒,能够被iCloud自动备份,能够经过同步推送一类的助手直接取出文件;在Android上的位置是根据Project Setting里设置的Write Access路径,能够设置是程序沙盒仍是sdcard,注意:若是在Android设置保存在沙盒中,那么就必须root之后才能用电脑取出文件,所以建议写入sdcard里。通常状况下,建议将得到的文件保存在这个路径下,例如能够从StreamingAsset中读取的二进制文件或者从AssetBundle读取的文件写入PersistentDatapath。
e、
Application.temporaryCachePath路径
此属性返回一个临时数据的缓存目录,跟Application.persistentDataPath相似,可是在IOS上不能被自动备份。
f、
/sdcard/..路径
表示Android手机的SD卡根目录。
g、
/storage/emulated/0/..
路径(这个路径我查找了很久……)
表示Android手机的内置存储根目录。
以上各路径中的资源加载方式均可以用WWW类加载,但要注意各个平台路径须要加的访问名称,例如Android平台的路径前要加"jar:file://",其余平台使用"file://"。如下是各路径在各平台中的具体位置信息:
二、各路径对应目录:缓存
Android平台app
Application.dataPath : /data/app/xxx.xxx.xxx.apk
Application.streamingAssetsPath : jar:file:///data/app/xxx.xxx.xxx.apk/!/assets
Application.persistentDataPath : /data/data/xxx.xxx.xxx/files
Application.temporaryCachePath : /data/data/xxx.xxx.xxx/cache
IOS平台
Application.dataPath : Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/xxx.app/Data
Application.streamingAssetsPath : Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/xxx.app/Data/Raw
Application.persistentDataPath : Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/Documents
Application.temporaryCachePath : Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/Library/Caches
Windows Web Player
Application.dataPath : file:///D:/MyGame/WebPlayer (即导包后保存的文件夹,html文件所在文件夹)
Application.streamingAssetsPath :
Application.persistentDataPath :
Application.temporaryCachePath :
从以上结论可知,安卓平台下的Application.streamingAssetsPath无需增长jar:file://字符串。
三、实践应用:
StreamingAssets文件夹下的
只读不可写路径:
以Application.streamingAssetsPath为基准 : jar:file:///data/app/xxx.xxx.xxx.apk/!/assets异步
安卓读:filePath = Application.streamingAssetsPath + "/文件名.格式名";编辑器
filePath = "jar:file://" + Application.dataPath + "/!/assets" + "/文件名.格式名";spa
IOS读: filePath = "file://" + Application.streamingAssetsPath + "/文件名.格式名";线程
filePath = "file://" + Application.dataPath + "/Raw" + "/文件名.格式名";htm
PC读:filePath = "file://" + Application.streamingAssetsPath + "/文件名.格式名";/队列
Unity内部的可读可写路径:事件
安卓: path = Application.persistentDataPath + "/文件名.格式名";
IOS: path = Application.persistentDataPath + "/文件名.格式名"; //未验证
PC: path = "file://" + Application.persistentDataPath + "/文件名.格式名";//
另外,当作下载功能或者加载功能的时候,这两个目录只能在主线程下去调用。
若是在异步下载里用到该目录,当用消息者模式进行回发的时候,执行的回调并不在主线程里,若是须要对应操做GameObject,则会出现报错行为,若是要作异步下载,请在非mono类里面增长Queue<T>的队列管理,并将回发事件写入队列,由mono的Update对队列进行监听。
这样每次下载完成,下载的异步线程就会把消息写入Queue的队列,此时Update一直在监听队列的数量,当数量大于0的时候,每一次update都会对队列进行一次Dequeue(),每一次Dequeue的时候,都会从主线程发送一条消息去执行,消息的回调当调到 GameObejct的时候也不会出现报错。
下一篇预告:Unity使用Http进行同步和异步下载。