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的時候也不會出現報錯。
轉載位址:https://www.cnblogs.com/vsirWaiter/p/5340284.html