AssetBundle使用方法

協程下載AssetBundle方法

    void Start()
    {
        IEnumerator LoadResourceCorotine(string resName, string filePath)
        {
            UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(@"http://localhost/AssetBundles/" + filePath);
            yield return request.SendWebRequest();
           // AssetBundle ab = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
            AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);
            GameObject gameObject = ab.LoadAsset<GameObject>(resName);
        }
    }

 

本地加載方式 

AssetBundle.LoadFromFile

AssetBundle.LoadFromMemoryAsync

通過AssetBundleManifest得到某個包的依賴

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LoadFromFileExample : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        AssetBundle ab = AssetBundle.LoadFromFile("AssetBundles/cube.unity3d");

        AssetBundleManifest manifest = ab.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
        //通過AssetBundleManifest獲取所有AssetBundle名字
        foreach (var name in manifest.GetAllAssetBundles())
        {
            print(name);
        }
        //獲取cube依賴的AssetBundle名字
        string[] strs=manifest.GetAllDependencies("cube.unity3d");
        //加載依賴的包
        foreach (var name in strs)
        {
            print(name);
            AssetBundle.LoadFromFile("AssetBundles/"+name);
        }
        GameObject cube = ab.LoadAsset<GameObject>("Cube");
        Instantiate(cube);
        //卸載AssetBundle true全部卸載,flase卸載不相關的
        ab.Unload(true);
    }
    void Update()
    {

    }
}

AssetBunBrowserTool工具使用戶可以查看和編輯其Unity項目的AssetBundles包的配置

下載地址https://github.com/Unity-Technologies/AssetBundles-Browser

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章