一:加载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);
}
}
}