打包 首先在unity编辑器中设置好, 在Editor代码中打包
using System.IO;
using UnityEditor;
using UnityEngine;
public class AssetBundleEditor : Editor {
[MenuItem ("Assetbundle/MakeCubeForWin64")]
public static void MakeCubeForWin64 () {
string path = Path.Combine (Application.streamingAssetsPath, "Assetbundles/win64");
if (!Directory.Exists (path)) {
System.IO.Directory.CreateDirectory (path);
}
BuildPipeline.BuildAssetBundles (path, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
}
[MenuItem ("Assetbundle/MakeCubeForAndroid")]
public static void MakeCubeForAndroid () {
string path = Path.Combine (Application.streamingAssetsPath, "Assetbundles/android");
if (!Directory.Exists (path)) {
System.IO.Directory.CreateDirectory (path);
}
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() {
StartCoroutine(LoadAssetBundleAsync());
}
public IEnumerator LoadAssetBundleForLocalAsync() {
string pathMat = Path.Combine(Application.streamingAssetsPath, @"AssetBundles\win64\asset\mat.ab");
Uri uriMat = new Uri(pathMat);
AssetBundleCreateRequest assetBundleCreateRequestMat = AssetBundle.LoadFromFileAsync(pathMat);
yield return assetBundleCreateRequestMat;
string pathTex = Path.Combine(Application.streamingAssetsPath, @"AssetBundles\win64\asset\tex.ab");
Uri uriTex = new Uri(pathTex);
AssetBundleCreateRequest assetBundleCreateRequestTex = AssetBundle.LoadFromFileAsync(pathTex);
yield return assetBundleCreateRequestTex;
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);
yield return assetBundleCreateRequestCube;
AssetBundle ab = assetBundleCreateRequestCube.assetBundle;
GameObject cubePrefab = ab.LoadAsset<GameObject>("Cube");
Instantiate(cubePrefab);
}
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();
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);
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 {
AssetBundle ab = DownloadHandlerAssetBundle.GetContent(unityWebRequest);
GameObject cubePrefab = ab.LoadAsset<GameObject>("Cube");
Instantiate(cubePrefab);
}
}
}
|