打包
首先在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);
}
}
}