1. Set the AssetBundle name of the resource (if there is a material, you can also set it for the material, here the AssetBundle name of the material is share)
2. Write a tool class to complete AssetBundle packaging
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
public class AssetBundleAB
{
[MenuItem("Assets/AssetBundle/BuildAB")]
static void BuildAB()
{
Debug.Log("打包完成");
string path = "Assets/AssetsBundles";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
BuildPipeline.BuildAssetBundles(path, BuildAssetBundleOptions.UncompressedAssetBundle, BuildTarget.StandaloneWindows64);//三个参数分别表示打包路径,打包压缩格式,打包平台
AssetDatabase.Refresh();//刷新
}
}
after packing
3. AssetBundle loading
Method 1: AssetBundle.LoadFromFile synchronous loading
using UnityEngine;
public class LoadResources : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
string path = "Assets/AssetsBundles/prefabs/Cube";
AssetBundle ab = AssetBundle.LoadFromFile(path);//加载预制体AB包
AssetBundle.LoadFromFile("Assets/AssetsBundles/share");//加载预制体的材质
GameObject obj = ab.LoadAsset<GameObject>("Cube");//加载预制体
Instantiate(obj);
}
}
Method 2: AssetBundle.LoadFromFileAsync loads asynchronously
using System.Collections;
using UnityEngine;
public class LoadResources : MonoBehaviour
{
// Start is called before the first frame update
IEnumerator Start()
{
string path = "Assets/AssetsBundles/prefabs/Cube";
AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(path);
yield return request;//等待响应
AssetBundle ab = request.assetBundle;
AssetBundleCreateRequest re = AssetBundle.LoadFromFileAsync("Assets/AssetsBundles/share");
AssetBundle share = re.assetBundle;
GameObject[] objs = ab.LoadAllAssets<GameObject>();
foreach (var item in objs)
{
Instantiate(item);
}
}
}
Method 3: AssetBundle.LoadFromMemory synchronous loading
using UnityEngine;
using System.IO;
public class LoadResources : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
string path = "Assets/AssetsBundles/prefabs/Cube";
AssetBundle cube = AssetBundle.LoadFromMemory(File.ReadAllBytes(path));
AssetBundle mat = AssetBundle.LoadFromMemory(File.ReadAllBytes("Assets/AssetsBundles/share"));
GameObject obj = cube.LoadAsset<GameObject>("Cube");
Instantiate(obj);
}
}
Method 4: AssetBundle.LoadFromMemoryAsync asynchronous loading
using UnityEngine;
using System.IO;
using System.Collections;
public class LoadResources : MonoBehaviour
{
// Start is called before the first frame update
IEnumerator Start()
{
string path = "Assets/AssetsBundles/prefabs/Cube";
AssetBundleCreateRequest request = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path));
yield return request;
AssetBundle cube = request.assetBundle;
AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes("Assets/AssetsBundles/share"));
GameObject obj = cube.LoadAsset<GameObject>("Cube");
Instantiate(obj);
}
}
Method 5: WWW asynchronous loading, loading in the form of a local directory (outdated, need to change the AssetsBundles folder name to StreamingAssets)
using UnityEngine;
using System.Collections;
public class LoadResources : MonoBehaviour
{
// Start is called before the first frame update
IEnumerator Start()
{
WWW www = new WWW("file:/"+Application.streamingAssetsPath+"/prefabs/cube");//本地目录需要加上"file://"
WWW wwwMat = new WWW("file:/" + Application.streamingAssetsPath + "/share");
yield return www;
yield return wwwMat;
AssetBundle cube = www.assetBundle;//获取ab包
AssetBundle mat= wwwMat.assetBundle;//加载Asset
GameObject obj = cube.LoadAsset<GameObject>("Cube");
Instantiate(obj);
}
}
Method 6: Asynchronous loading of UnityWebRequestAssetBundle (tested with NetBox2 local server)
NetBox2: Link: https://pan.baidu.com/s/1YsVFuoLGnukY_DRqiA7eyA?pwd=49ep
Extraction code: 49ep
After downloading, just double-click to run it (Note: NetBox2 should be placed in the same directory as AssetsBundles)
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class LoadResources : MonoBehaviour
{
// Start is called before the first frame update
IEnumerator Start()
{
UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle("http://localhost/AssetsBundles/prefabs/cube");
UnityWebRequest requestMat = UnityWebRequestAssetBundle.GetAssetBundle("http://localhost/AssetsBundles/share");
yield return request.SendWebRequest();
yield return requestMat.SendWebRequest();
AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);//获取ab包
AssetBundle mat = DownloadHandlerAssetBundle.GetContent(requestMat);
GameObject obj = ab.LoadAsset<GameObject>("cube");//加载Asset
Instantiate(obj);//实例化
}
}