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