Unity AB包的打包与加载

打包
首先在unity编辑器中设置好, 在Editor代码中打包

using System.IO;
using UnityEditor;
using UnityEngine;

public class AssetBundleEditor : Editor {

	[MenuItem ("Assetbundle/MakeCubeForWin64")]
	public static void MakeCubeForWin64 () {

		// 制作AB包的位置
		string path = Path.Combine (Application.streamingAssetsPath, "Assetbundles/win64");

		// 判断文件夹是否存在
		if (!Directory.Exists (path)) {
			System.IO.Directory.CreateDirectory (path);
		}

		// 将所有在属性栏设置了AB包属性的预制体作为AB包
		// 参数1位制作的位置, 参数2位压缩方式, 参数3位制作平台
		BuildPipeline.BuildAssetBundles (path, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
	}

	[MenuItem ("Assetbundle/MakeCubeForAndroid")]
	public static void MakeCubeForAndroid () {

		// 制作AB包的位置
		string path = Path.Combine (Application.streamingAssetsPath, "Assetbundles/android");

		// 判断文件夹是否存在
		if (!Directory.Exists (path)) {
			System.IO.Directory.CreateDirectory (path);
		}
		// 将所有在属性栏设置了AB包属性的预制体作为AB包
		// 参数1位制作的位置, 参数2位压缩方式, 参数3位制作平台
		BuildPipeline.BuildAssetBundles (path, BuildAssetBundleOptions.None, BuildTarget.Android);
	}
}

加载

using System;
using System.Collections;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;

public class AssetBundleController : MonoBehaviour
{
	void Start() {
		// LoadAssetBundle ();
		// StartCoroutine (LoadAssetBundleForLocalAsync ());
		StartCoroutine(LoadAssetBundleAsync());
	}

	/// <summary>
	/// 本地异步加载AB包
	/// </summary>
	public IEnumerator LoadAssetBundleForLocalAsync() {
		// 加载Material
		string pathMat = Path.Combine(Application.streamingAssetsPath, @"AssetBundles\win64\asset\mat.ab");
		// AssetBundle assetBundleMat = AssetBundle.LoadFromFile (pathMat);
		Uri uriMat = new Uri(pathMat);
		AssetBundleCreateRequest assetBundleCreateRequestMat = AssetBundle.LoadFromFileAsync(pathMat);
		yield return assetBundleCreateRequestMat;

		// 加载Texture
		string pathTex = Path.Combine(Application.streamingAssetsPath, @"AssetBundles\win64\asset\tex.ab");
		Uri uriTex = new Uri(pathTex);
		AssetBundleCreateRequest assetBundleCreateRequestTex = AssetBundle.LoadFromFileAsync(pathTex);
		// AssetBundle assetBundleTex = AssetBundle.LoadFromFile (pathTex);
		yield return assetBundleCreateRequestTex;

		// 加载Cube
		string pathCube = Path.Combine(Application.streamingAssetsPath, @"AssetBundles\win64\asset\cube.ab");
		Uri uriCube = new Uri(pathCube);

		Debug.Log(uriCube.AbsolutePath);
		Debug.Log(uriCube.AbsoluteUri);
		Debug.Log(pathCube);
		AssetBundleCreateRequest assetBundleCreateRequestCube = AssetBundle.LoadFromFileAsync(pathCube);
		// AssetBundle assetBundleCube = AssetBundle.LoadFromFile (pathCube);
		yield return assetBundleCreateRequestCube;

		// 将加载的包读到内存中
		AssetBundle ab = assetBundleCreateRequestCube.assetBundle;
		// Debug.Log (ab);
		// 从内存的ab包中获取Cube预制体并实例化
		GameObject cubePrefab = ab.LoadAsset<GameObject>("Cube");
		// Debug.Log (cubePrefab);
		// GameObject cubePrefab = assetBundleCube.LoadAsset<GameObject> ("Cube");
		Instantiate(cubePrefab);
	}

	/// <summary>
	/// 异步加载AB包, 此方法可以加载网络包
	/// </summary>
	/// <returns></returns>
	public IEnumerator LoadAssetBundleAsync() {
		// 贴图依赖包路径
		string path1 = Path.Combine(Application.streamingAssetsPath, @"Assetbundles\win64\asset\tex.ab");
		Uri uri1 = new Uri(path1);
		// 异步加载贴图依赖包
		UnityWebRequest unityWebRequest1 = UnityWebRequest.GetAssetBundle(uri1.AbsoluteUri);
		yield return unityWebRequest1.SendWebRequest();
		// 这一句一定要写, 将ab包读到内存中, 否则之后依赖它的包不能正常读取
		AssetBundle ab1 = DownloadHandlerAssetBundle.GetContent(unityWebRequest1);

		// 材质依赖包路径
		string path2 = Path.Combine(Application.streamingAssetsPath, @"Assetbundles\win64\asset\mat.ab");
		Uri uri2 = new Uri(path2);
		UnityWebRequest unityWebRequest2 = UnityWebRequest.GetAssetBundle(uri2.AbsoluteUri);
		yield return unityWebRequest2.SendWebRequest();
		AssetBundle ab2 = DownloadHandlerAssetBundle.GetContent(unityWebRequest2);

		// cube包路径
		string path = Path.Combine(Application.streamingAssetsPath, @"Assetbundles\win64\asset\cube.ab");
		Uri uri = new Uri(path);
		UnityWebRequest unityWebRequest = UnityWebRequest.GetAssetBundle(uri.AbsoluteUri);
		yield return unityWebRequest.SendWebRequest();
		// 加载后, 判断是否加载错误
		if (unityWebRequest.isHttpError || unityWebRequest.isNetworkError) {
			Debug.Log("error");
		} else {
			// 从内存中获取Cube预制体并实例化
			AssetBundle ab = DownloadHandlerAssetBundle.GetContent(unityWebRequest);
			GameObject cubePrefab = ab.LoadAsset<GameObject>("Cube");
			Instantiate(cubePrefab);
		}
	}
}
  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值