栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Unity AB包的打包与加载

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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());
	}

	/// 
	/// 本地异步加载AB包
	/// 
	public IEnumerator LoadAssetBundleForLocalAsync() {
		// 加载Material
		string pathMat = Path.Combine(Application.streamingAssetsPath, @"AssetBundleswin64assetmat.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, @"AssetBundleswin64assettex.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, @"AssetBundleswin64assetcube.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("Cube");
		// Debug.Log (cubePrefab);
		// GameObject cubePrefab = assetBundleCube.LoadAsset ("Cube");
		Instantiate(cubePrefab);
	}

	/// 
	/// 异步加载AB包, 此方法可以加载网络包
	/// 
	/// 
	public IEnumerator LoadAssetBundleAsync() {
		// 贴图依赖包路径
		string path1 = Path.Combine(Application.streamingAssetsPath, @"Assetbundleswin64assettex.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, @"Assetbundleswin64assetmat.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, @"Assetbundleswin64assetcube.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("Cube");
			Instantiate(cubePrefab);
		}
	}
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/287450.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号