Unity使用AssetBundle加载资源

1.设置资源的AssetBundle名(有材质的可以给材质也设置,这里材质的AssetBundle名为share)

 2.写个工具类来完成AssetBundle打包

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

打包后

 3.AssetBundle加载

方法一:AssetBundle.LoadFromFile同步加载

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

方法二:AssetBundle.LoadFromFileAsync异步加载

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

方法三:AssetBundle.LoadFromMemory同步加载

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

方法四:AssetBundle.LoadFromMemoryAsync异步加载

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

方法五:WWW异步加载,以本地目录的形式加载(已过时,需要将AssetsBundles文件夹名称改为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);
    }
}

方法六:UnityWebRequestAssetBundle异步加载(使用NetBox2本地服务器测试)

NetBox2:链接:https://pan.baidu.com/s/1YsVFuoLGnukY_DRqiA7eyA?pwd=49ep 
提取码:49ep

下载后,直接双击运行即可(注意:NetBox2要和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);//实例化
    }
}

猜你喜欢

转载自blog.csdn.net/falsedewuxin/article/details/130331913