項目中的資源一般我們打包成AssetBundle格式
方便我們加載和熱更
而AssetBundle文件 一般保存在StreamingAssets文件夾或PersistentData文件夾
首先我們看這兩個文件夾有什么區別
| StreamingAssets |
| 可讀取、不可寫入 |
| 工程Assets根目錄下StreamingAssets文件夾 |
| PersistentData |
| 可讀取、可寫入 |
| 沙盒目錄,應用程序安裝運行后才會出現 |
綜合上面的區別我們可以總結
*StreamingAssets 不能寫入 隨包發包 適合存放一些初始化的AssetBundle資源 如 登入頁 加載頁等
*PersistentData 適合存放 運行程序下載的AssetBundle資源
然后我們看下這兩個文件夾在 各平台 運行時 打印的 路徑地址:
| StreamingAssets | |
| WindowsEditor | C:/Users/zq/Desktop/Test/Assets/StreamingAssets(工程目錄/Assets/StreamingAssets) |
| OSXEditor | /Users/zouqiang/Desktop/Test/Assets/StreamingAssets(工程目錄/Assets/StreamingAssets) |
| Android | jar:file:///data/app/com.Company.Test-1.apk!/assets(安裝目錄/assets) |
| IPhonePlayer | /var/containers/Bundle/Application/5C9D81B5-2A21-45BC-B6DC-12B704976D22/Test.app/Data/Raw |
| PersistentData | |
| WindowsEditor | C:/Users/zq/AppData/LocalLow/DefaultCompany/Test |
| OSXEditor | /Users/zouqiang/Library/Application Support/DefaultCompany/Test |
| Android | /storage/emulated/0/Android/data/com.Company.Test/files |
| IPhonePlayer | /var/mobile/Containers/Data/Application/F96BACED-63D5-45AE-B2A7-738A87545A8F/Documents |
StreamingAssets 在各工程路徑(資源更新可直接替換對應的文件 實現快速打包或測試)
| Unity | C:\Users\zq\Desktop\Test\Assets\StreamingAssets |
| Eclipse | C:\Users\zq\Desktop\eclipse\Test\assets |
| Xcode | C:\Users\zq\Desktop\eclipse\Test\assets |
在使用
AssetBundle.LoadFromFile 同步加載
AssetBundle.LoadFromFileAsync 異步加載
StreamingAssets文件夾里面的AssetBundle
要注意
Android 獲得去方式為 Application.dataPath + "!assets"
其他平台 獲取的方式為 Application.streamingAssetsPath
使用
WWW 異步加載
加載StreamingAssets文件夾里面的AssetBundle
要注意
Android 獲得去方式為 Application.streamingAssetsPath
其他平台 獲取的方式為 "file://" + Application.streamingAssetsPath
PersistentData文件夾 獲取的方式都為 Application.persistentDataPath
