Unity5.X打包与加载AssetBundle简单示例

准备与打包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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值