准备与打包AssetBundle:
在Editor内,点击任意prefab或资源文件,在右下角(默认Editor布局)AssetsBundle处可设置此prefab的Assetsbundle属性。
例如在下图中,点“New…"可以创建这个prefab的AssetBundle名称。名称也可以设为路径模式,例如在名字中加入“/”,这样可以将多个AssetBundle更清楚的分类。
资源设置好了AssetBundle名称等属性就可以开始打包了。
下文为一个测试脚本,声明一个静态方法,给Editor上方菜单栏增加一个打包的选项,在方法内调用UnityEditor的打包方法:
using UnityEditor;
public class CreateAssetBundles
{
[MenuItem ("Assets/Build AssetBundles")]
static void BuildAllAssetBundles ()
{
BuildPipeline.BuildAssetBundles ("Assets/AssetBundles", BuildAssetBundleOptions.None, BuildTarget.StandaloneOSXUniversal);
}
}
BuildAssetBundles的第一个参数为导出路径,调用该方法前应保证路径和文件夹名称与权限正确。其他参数可以选择压缩模式与导出AB的应用平台操作系统。
这样在顶部Assets目录中的底部会出现上面MenuItem特性中设好的“Build Assetbundles”选项,点击它就会开始打包了。
打包后,导出路径中(如果之前设置的Assetbundle名称是路径模式,则导出路径中会出现相应的下一级文件夹)会出现一个AssetBundles文件(作用未知,有可能是AssetBundles.manifest依赖的文件)和一个AssetBundles.manifest文件,AssetBundles.manifest文件里写明了所有打包出来的AssetBundle的依赖关系,各个AssetBundle也会生成自己的manifest描述文件,里面有例如crc之类的信息。
用WWW加载AssetBundle:
官网介绍的两种方式
1,下载但不储存到缓存中
using System;
using UnityEngine;
using System.Collections;
class NonCachingLoadExample : MonoBehaviour {
public string BundleURL;
public string AssetName;
IEnumerator Start() {
// Download the file from the URL. It will not be saved in the Cache
using (WWW www = new WWW(BundleURL)) {
yield return www;
if (www.error != null)
throw new Exception("WWW download had an error:" + www.error);
AssetBundle bundle = www.assetBundle;
if (AssetName == "")
Instantiate(bundle.mainAsset);
else
Instantiate(bundle.LoadAsset(AssetName));
// Unload the AssetBundles compressed contents to conserve memory
bundle.Unload(false);
} // memory is freed from the web stream (www.Dispose() gets called implicitly)
}
}
2,下载并储存到缓存中(推荐)
using System;
using UnityEngine;
using System.Collections;
public class CachingLoadExample : MonoBehaviour {
public string BundleURL;
public string AssetName;
public int version;
void Start() {
BundleURL="file://"+Application.dataPath+BundleURL;
StartCoroutine (DownloadAndCache());
}
IEnumerator DownloadAndCache (){
// Wait for the Caching system to be ready
while (!Caching.ready)
yield return null;
// Load the AssetBundle file from Cache if it exists with the same version or download and store it in the cache
using(WWW www = WWW.LoadFromCacheOrDownload (BundleURL, version)){
yield return www;
if (www.error != null)
throw new Exception("WWW download had an error:" + www.error);
AssetBundle bundle = www.assetBundle;
if (AssetName == "")
Instantiate(bundle.mainAsset);
else
foreach(UnityEngine.Object temp in bundle.LoadAllAssets())
{
Instantiate(temp);
}
// Unload the AssetBundles compressed contents to conserve memory
bundle.Unload(false);
} // memory is freed from the web stream (www.Dispose() gets called implicitly)
}
}
两种方法的区别是方法2使用了WWW.LoadFromCacheOrDownload方法,此方法在第一次加载该url的AB后会进行缓存,这样下次使用该方法加载同名同version AB时会直接从缓存中取出。
Unity WWW除了可以用作Http短连接也可以读取本地文件(一些操作系统出于安全考虑已经封杀掉了该方法对本地文件的访问),如果为读取本地文件,路径参数格式为:“file://”+“application.datapath”+“/文件夹名称/文件名”,Http短连接格式为:http://…
方法1中bundle.LoadAsset方法中的参数AssetName为prefab或资源文件的名字。
如果要实例化所有AssetBundle中的物体,可用方法2中的LoadAllAssets()方法(假设都是prefab):
foreach(UnityEngine.Object temp in bundle.LoadAllAssets())
{
Instantiate(temp);
}
维护日志:
2020-8-2:review