AssetBundle(四)——通过Manifest文件得到依赖

一:加载Manifest介绍

加载的Manifest文件是主的Manifest文件而不是每个AB包的Manifest文件,因为从主Manifest可以访问到所有资源的依赖资源

using UnityEngine;

public class LoadFromWeb : MonoBehaviour
{
    private void Start()
    {
        string path = "Assets\AssetBundles\AssetBundles";
        AssetBundle assetBundle = AssetBundle.LoadFromFile(path);
        AssetBundleManifest manifest = assetBundle.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
    }
}

加载得到Manifest文件后就可以读取到Manifest文件中的依赖,名字等参数
例如加载一个有依赖的预制体

加载后发现预制体材质丢失,因为没有加载出依赖资源
之后通过加载Manifest文件读取到model.v1的所有依赖的名称
再通过遍历字符串数组去加载出依赖即可


二:通过加载Manifest文件加载出所有的资源以及资源的依赖

步骤:
——加载得到Manifest文件,从Manifest文件中得到所有的AB包的路径(包括依赖项)
——利用这些名称获取每个包的AssetBudle对象,从这些AssetBudle对象中加载出各自的所有资源,如果资源是应该实例化出来的物体则实例化,否则只加载即可

using UnityEngine;

public class Test : MonoBehaviour
{
    private void Awake()
    {
        string manifestAB_path = "Assets/AssetBundles/";
        AssetBundle manifestAB = AssetBundle.LoadFromFile(manifestAB_path + "AssetBundles");
        AssetBundleManifest manifest = manifestAB.LoadAsset<AssetBundleManifest>("AssetBundleManifest");

        foreach (string path in manifest.GetAllAssetBundles())
        {
            string fullPath = manifestAB_path + path;
            AssetBundle ab = AssetBundle.LoadFromFile(fullPath);
            Object[] o = ab.LoadAllAssets();
            foreach (Object temp in o)
            {
                if (temp is GameObject)
                {
                    Instantiate(temp);
                }
            }
        }
    }
}
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
using System.Collections.Generic;

public class Load : MonoBehaviour
{
    private IEnumerator Start()
    {
        UnityWebRequest request = UnityWebRequest.GetAssetBundle(@"http://localhost:51008/AssetBundle/AssetBundle");
        yield return request.SendWebRequest();
        AssetBundleManifest manifest = DownloadHandlerAssetBundle.GetContent(request).LoadAsset<AssetBundleManifest>("AssetBundleManifest");

        string[] str = manifest.GetAllAssetBundles();
        List<GameObject> goList = new List<GameObject>();
        foreach (string s in str)
        {
            UnityWebRequest r = UnityWebRequest.GetAssetBundle(@"http://localhost:51008/AssetBundle/" + s);
            yield return r.SendWebRequest();
            Object[] o = DownloadHandlerAssetBundle.GetContent(r).LoadAllAssets();
            foreach (Object temp in o)
            {
                if (temp is GameObject)
                {
                    goList.Add(temp as GameObject);
                }
            }
        }
        foreach (GameObject  go in goList)
        {
            Instantiate(go);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hello Bug.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值