Unity特殊文件路徑

1、文件路徑
Resources:
Unity在發佈成移動端項目後,其他文件路徑都將不存在,但是如果有一些必要的資源,可以放在Resources文件夾下,因爲這個文件夾下的所有資源是由Unity內部進行
調用,當發佈成移動端後,該路徑將不存在,所以不可寫也不可讀,只能用Unity封裝的方法進行該路徑下的資源加載。
StreamingAssets:
該路徑會在發佈工程時,裏面的資源會原封不動的進行打包到包體中,不過裏面的資源只能採用WWW的方式進行讀取,所以訪問速度有限,或者採用AssetBundle的方式。
但是這樣都會導致最後發佈的包體體積過大,且該路徑下的資源可讀不可寫。
在c#中使用Directory.GetFiles獲取文件夾時,在Application.persistentDataPath路徑下,直接獲取會出現路徑爲空異常。
且在使用Unity中封裝好的Application.streamingAssetsPath時,安卓路徑在前面不需要加"jar:file///",因爲其自己已經自帶了,所以再加的話會訪問不到,或者可以
使用"jar:file://"+Application.dataPath+"!/assets/"
2、出現路徑禁止訪問BUG
我在發佈到Android後,利用Application.persistentDataPath無法訪問到相對應的app安裝路徑,出現unauthorizedaccessexception Access to the path "/jar:file:"is denied
問題。最後解決發現是路徑前面的協議寫錯了,
要是訪問apk內部資源應該在前面加"jar:file://",否則的話就是加"file://"。


Unity中幾個特殊路徑的說明:
dataPath :返回程序的數據文件所在的文件夾的路徑(只讀)。返回路徑爲相對路徑,一般是相對於程序安裝目錄的位置。不同遊戲平臺的數據文件保存路徑不同。


StreamingAssetsPath: 此屬性用於返回數據流的緩存目錄,返回路徑爲相對路徑,適合設置一些外部數據文件的路徑。(只讀)

PersistentDataPath:返回一個持久化數據存儲目錄的路徑(只讀),可以在此路徑下存儲一些持久化的數據文件。對應同一平臺,在不同程序中調用此屬性時,其返回值是相同的,但是在不同的運行平臺下,其返回值會不一樣。


temporaryCachePath:此屬性用於返回一個臨時數據的緩衝目錄(只讀)。對於同一平臺,在不同程序中調用此屬性時,其返回值是相同的,但是在不同的運行平臺下,其返回值是不一樣的。


persistentDataPath和temporaryCachePath的返回值一般是程序所在平臺的固定位置,適合程序在運行過程中產生的數據文件。


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


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


Windows:

Application.dataPath :                      /Assets

Application.streamingAssetsPath :    /Assets/StreamingAssets

Application.persistentDataPath :      C:/Users/xxxx/AppData/LocalLow/CompanyName/ProductName

Application.temporaryCachePath :   C:/Users/xxxx/AppData/Local/Temp/CompanyName/ProductName


Mac:

Application.dataPath :                      /Assets

Application.streamingAssetsPath :    /Assets/StreamingAssets

Application.persistentDataPath :      /Users/xxxx/Library/Caches/CompanyName/Product Name

Application.temporaryCachePath :   /var/folders/57/6b4_9w8113x2fsmzx_yhrhvh0000gn/T/CompanyName/Product Name

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章