当前位置:   article > 正文

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

loadasset

一:加载Manifest介绍

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

  1. using UnityEngine;
  2. public class LoadFromWeb : MonoBehaviour
  3. {
  4. private void Start()
  5. {
  6. string path = "Assets\AssetBundles\AssetBundles";
  7. AssetBundle assetBundle = AssetBundle.LoadFromFile(path);
  8. AssetBundleManifest manifest = assetBundle.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
  9. }
  10. }

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

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


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

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

  1. using UnityEngine;
  2. public class Test : MonoBehaviour
  3. {
  4. private void Awake()
  5. {
  6. string manifestAB_path = "Assets/AssetBundles/";
  7. AssetBundle manifestAB = AssetBundle.LoadFromFile(manifestAB_path + "AssetBundles");
  8. AssetBundleManifest manifest = manifestAB.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
  9. foreach (string path in manifest.GetAllAssetBundles())
  10. {
  11. string fullPath = manifestAB_path + path;
  12. AssetBundle ab = AssetBundle.LoadFromFile(fullPath);
  13. Object[] o = ab.LoadAllAssets();
  14. foreach (Object temp in o)
  15. {
  16. if (temp is GameObject)
  17. {
  18. Instantiate(temp);
  19. }
  20. }
  21. }
  22. }
  23. }
  1. using UnityEngine;
  2. using UnityEngine.Networking;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. public class Load : MonoBehaviour
  6. {
  7. private IEnumerator Start()
  8. {
  9. UnityWebRequest request = UnityWebRequest.GetAssetBundle(@"http://localhost:51008/AssetBundle/AssetBundle");
  10. yield return request.SendWebRequest();
  11. AssetBundleManifest manifest = DownloadHandlerAssetBundle.GetContent(request).LoadAsset<AssetBundleManifest>("AssetBundleManifest");
  12. string[] str = manifest.GetAllAssetBundles();
  13. List<GameObject> goList = new List<GameObject>();
  14. foreach (string s in str)
  15. {
  16. UnityWebRequest r = UnityWebRequest.GetAssetBundle(@"http://localhost:51008/AssetBundle/" + s);
  17. yield return r.SendWebRequest();
  18. Object[] o = DownloadHandlerAssetBundle.GetContent(r).LoadAllAssets();
  19. foreach (Object temp in o)
  20. {
  21. if (temp is GameObject)
  22. {
  23. goList.Add(temp as GameObject);
  24. }
  25. }
  26. }
  27. foreach (GameObject go in goList)
  28. {
  29. Instantiate(go);
  30. }
  31. }
  32. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/82244
推荐阅读
相关标签
  

闽ICP备14008679号