AssetLoadHelper_AB.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class AssetLoadHelper_AB : MonoBehaviour
  5. {
  6. public string ab_name = "";
  7. //public string[] assestName;
  8. public Transform insRoot;
  9. public bool addMat = false;
  10. public Material defulMat;
  11. public List<string> ignoreObjs = new List<string>();
  12. void Start()
  13. {
  14. if (insRoot == null)
  15. {
  16. insRoot = this.transform;
  17. }
  18. TextureLoadHelp._Instance.StartCoroutine(Load());
  19. }
  20. IEnumerator Load()
  21. {
  22. WaitForEndOfFrame wait = new WaitForEndOfFrame();
  23. WWW www = WWW.LoadFromCacheOrDownload($"{Application.streamingAssetsPath}/{ab_name}", 0);
  24. yield return www;
  25. if (www.isDone)
  26. {
  27. var tempAb = www.assetBundle;
  28. GameObject[] objs = tempAb.LoadAllAssets<GameObject>();
  29. for (int i = 0; i < objs.Length; i++)
  30. {
  31. if (ignoreObjs.Contains(objs[i].name)) {
  32. continue;
  33. }
  34. GameObject insObj=Instantiate(objs[i], insRoot);
  35. if (addMat)
  36. {
  37. insObj.GetComponent<MeshRenderer>().material = defulMat;
  38. }
  39. yield return wait;
  40. }
  41. }
  42. else
  43. {
  44. Debug.LogError($"{ab_name}下载失败:{www.error}");
  45. }
  46. www.Dispose();
  47. }
  48. }