1、各路徑的定義:
a、Resources路徑
Resources文件夾是Unity里自動(dòng)識(shí)別的一種文件夾,可在Unity編輯器的Project窗口里創(chuàng)建,并將資源放置在里面。Resources文件夾下的資源不管是否有用,全部會(huì)打包進(jìn).apk或者.ipa,并且打包時(shí)會(huì)將里面的資源壓縮處理。加載方法是Resources.Load<T>(文件名),需要注意:文件名不包括擴(kuò)展名,打包后不能更改Resources下的資源內(nèi)容,但是從Resources文件夾中加載出來(lái)的資源可以更改。
b、Application.dataPath路徑
這個(gè)屬性返回的是程序的數(shù)據(jù)文件所在文件夾的路徑,例如在Editor中就是項(xiàng)目的Assets文件夾的路徑,通過(guò)這個(gè)路徑可以訪問(wèn)項(xiàng)目中任何文件夾中的資源,但是在移動(dòng)端它是完全沒(méi)用。
c、Application.streamingAssetsPath路徑
這個(gè)屬性用于返回流數(shù)據(jù)的緩存目錄,返回路徑為相對(duì)路徑,適合設(shè)置一些外部數(shù)據(jù)文件的路徑。在Unity工程的Assets目錄下起一個(gè)名為“StreamingAssets”的文件夾即可,然后用Application.streamingAssetsPath訪問(wèn),這個(gè)文件夾中的資源在打包時(shí)會(huì)原封不動(dòng)的打包進(jìn)去,不會(huì)壓縮,一般放置一些資源數(shù)據(jù)。在PC/MAC中可實(shí)現(xiàn)對(duì)文件的“增刪改查”等操作,但在移動(dòng)端是一個(gè)只讀路徑。
d、Application.persistentDataPath路徑(推薦使用)
此屬性返回一個(gè)持久化數(shù)據(jù)存儲(chǔ)目錄的路徑,可以在此路徑下存儲(chǔ)一些持久化的數(shù)據(jù)文件。這個(gè)路徑可讀、可寫,但是只能在程序運(yùn)行時(shí)才能讀寫操作,不能提前將數(shù)據(jù)放入這個(gè)路徑。在IOS上是應(yīng)用程序的沙盒,可以被iCloud自動(dòng)備份,可以通過(guò)同步推送一類的助手直接取出文件;在Android上的位置是根據(jù)Project Setting里設(shè)置的Write Access路徑,可以設(shè)置是程序沙盒還是sdcard,注意:如果在Android設(shè)置保存在沙盒中,那么就必須root以后才能用電腦取出文件,因此建議寫入sdcard里。一般情況下,建議將獲得的文件保存在這個(gè)路徑下,例如可以從StreamingAsset中讀取的二進(jìn)制文件或者從AssetBundle讀取的文件寫入PersistentDatapath。
e、Application.temporaryCachePath路徑
此屬性返回一個(gè)臨時(shí)數(shù)據(jù)的緩存目錄,跟Application.persistentDataPath類似,但是在IOS上不能被自動(dòng)備份。
f、/sdcard/..路徑
表示Android手機(jī)的SD卡根目錄。
g、/storage/emulated/0/..路徑(這個(gè)路徑我查找了好久……)
表示Android手機(jī)的內(nèi)置存儲(chǔ)根目錄。
以上各路徑中的資源加載方式都可以用WWW類加載,但要注意各個(gè)平臺(tái)路徑需要加的訪問(wèn)名稱,例如Android平臺(tái)的路徑前要加"jar:file://",其他平臺(tái)使用"file://"。
2、各路徑對(duì)應(yīng)目錄:
PC
persistentDataPath: C:/Users/[Username]/AppData/LocalLow/[CompanyName]/[GameName]
Android平臺(tái)
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平臺(tái)
dataPath: Application/<guid>/xxx.app/Data
streamingAssetsPath: Application/<guid>/xxx.app/Data/Raw
persistentDataPath: /var/mobile/Containers/Data/Application/<guid>/Documents
temporaryCachePath: Application/<guid>/Library/Caches
Windows Web Player
Application.dataPath: file:///D:/MyGame/WebPlayer (即導(dǎo)包后保存的文件夾,html文件所在文件夾)
Application.streamingAssetsPath:
Application.persistentDataPath:
Application.temporaryCachePath:
從以上結(jié)論可知,安卓平臺(tái)下的Application.streamingAssetsPath無(wú)需增加jar:file://字符串。
3、實(shí)踐應(yīng)用:
StreamingAssets文件夾下的只讀不可寫路徑:
以Application.streamingAssetsPath為基準(zhǔn) : jar:file:///data/app/xxx.xxx.xxx.apk/!/assets
安卓讀:filePath = Application.streamingAssetsPath + "/文件名.格式名";
filePath = "jar:file://" + Application.dataPath + "/!/assets" + "/文件名.格式名";
iOS讀: filePath = "file://" + Application.streamingAssetsPath + "/文件名.格式名";
filePath = "file://" + Application.dataPath + "/Raw" + "/文件名.格式名";
PC讀:filePath = "file://" + Application.streamingAssetsPath + "/文件名.格式名";/
Unity內(nèi)部的可讀可寫路徑:
安卓:path = Application.persistentDataPath + "/文件名.格式名";
iOS:path = Application.persistentDataPath + "/文件名.格式名"; //未驗(yàn)證
PC: path = "file://" + Application.persistentDataPath + "/文件名.格式名";//
另外,當(dāng)做下載功能或者加載功能的時(shí)候,這兩個(gè)目錄只能在主線程下去調(diào)用。
如果在異步下載里用到該目錄,當(dāng)用消息者模式進(jìn)行回發(fā)的時(shí)候,執(zhí)行的回調(diào)并不在主線程里,如果需要對(duì)應(yīng)操作GameObject,則會(huì)出現(xiàn)報(bào)錯(cuò)行為,如果要做異步下載,請(qǐng)?jiān)诜莔ono類里面增加Queue<T>的隊(duì)列管理,并將回發(fā)事件寫入隊(duì)列,由mono的Update對(duì)隊(duì)列進(jìn)行監(jiān)聽(tīng)。
這樣每次下載完成,下載的異步線程就會(huì)把消息寫入Queue的隊(duì)列,此時(shí)Update一直在監(jiān)聽(tīng)隊(duì)列的數(shù)量,當(dāng)數(shù)量大于0的時(shí)候,每一次update都會(huì)對(duì)隊(duì)列進(jìn)行一次Dequeue(),每一次Dequeue的時(shí)候,都會(huì)從主線程發(fā)送一條消息去執(zhí)行,消息的回調(diào)當(dāng)調(diào)到GameObejct的時(shí)候也不會(huì)出現(xiàn)報(bào)錯(cuò)。