Resources:
Resources文件可以在根目录下,也可以在子目录下,只要叫Resources就好。Resources目录下所有资源将被打包进游戏存放资源的archive中,Resources目录在应用中也就不复存在,但加载时仍使用曾在Resource下的路径。
该目录下所有资源会被压缩,只读不可写,使用Resources.Load()接口加载。
StreamingAssetsPath:
StreamingAssets目录必须在Assets根目录下,该目录下所有资源也会被打包到游戏里,不同于Resources目录,该目录下的资源不会进行压缩,同样是只读不可写的。Unity基本也没有提供从该路径下直接读取资源的方法,只有www可以加载audioClip、texture和二进制文件。但Unity提供了从该目录加载AssetBundle的方法,我们一般直接在这个目录下存放AssetBundle文件。可以通过Application.streamingAssetsPath访问该路径。
各平台StreamingAssets路径打印:
Win:E:/myProj/Assets/StreamingAssets
Mac : /myProj/Assets/StreamingAssets
Andorid:jar:file:///data/app/com.myCompany.myProj-1/base.apk!/assets
iOS: /var/containers/Application/E5543D66-83F3-476D-8A8F-49D5332B3763/myProj.app/Data/Raw
PersistentDataPath:
该目录为应用程序沙盒目录,应用程序安装后才会出现。该目录独特之处在于是可写的,所以我们一般将下载的AssetBundle存放于此。使用Application.persistentDataPath访问。
各平台PersistentDataPath路径打印:
Win:C:/Users/lodypig/Appdata/LocalLow/myCompany/myProj
Mac : /Users/lodypig/Library/Application Support/myCompany/myProj
Andorid:/data/data/com.myCompany.myProj/files
iOS: /var/mobile/Containers/Data/Appliction/A112252D-6B0E-459Z-9D49-CD3EAC6D47D/Documents
DataPath:
应用程序目录,即Assets目录。使用Appliction.dataPath访问。
各平台DataPath路径:
Win:E:/myProj/Assets
Mac : /myProj/Assets/
Andorid:/data/app/com.myCompany.myProj-1/base.apk!
iOS: /var/containers/Application/E5543D66-83F3-476D-8A8F-49D5332B3763/myProj.app/Data
注意:
Unity5.3及之前的版本,安卓平台下Application.streamingAssetsPath以"jar:file://"开头,直接使用Application.streamingAssetsPath进行加载会报cannot open archive的错误,原因是加载时路径不对。可以返回上面看一下,安卓平台下Application.streamingAssetsPath以jar:file://开头,跟其它都不一样。将这段去掉就能够正常加载。而去掉后等价于Application.dataPath + “!assets”,所以在Android下我们用该路径加载。
参考资料
示例:
- public class AssetBundleLoader
- {
- // 根据不同平台,声明StreamingAssetsPath路径
- public static readonly string STREAMING_ASSET_PATH =
- #if UNITY_ANDROID
- Application.dataPath + "!assets"; // 安卓平台
- #else
- Application.streamingAssetsPath; // 其他平台
- #endif
-
- // 从StreamingAssetsPath加载
- public static AssetBundle LoadFromStreamingAssetsPath(string assetbundle)
- {
- return AssetBundle.LoadFromFile(STREAMING_ASSET_PATH + "/" + assetbundle);
- }
- // PersistantDataPath加载
- public static AssetBundle LoadFromPersistantDataPath(string assetbundle)
- {
- return AssetBundle.LoadFromFile(Application.persistentDataPath+ "/" + assetbundle)
- }
- }