1、各路徑的定義:
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://"。以下是各路徑在各平台中的具體位置信息:
2、各路徑對應目錄:
Android平台
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://字符串。
3、實踐應用:
StreamingAssets文件夾下的
只讀不可寫路徑:
以Application.streamingAssetsPath為基准 : 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內部的可讀可寫路徑:
安卓: 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進行同步和異步下載。