AssetLoadHelper_AB.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. void Start()
  12. {
  13. if (insRoot == null)
  14. {
  15. insRoot = this.transform;
  16. }
  17. TextureLoadHelp._Instance.StartCoroutine(Load());
  18. }
  19. IEnumerator Load()
  20. {
  21. WaitForEndOfFrame wait = new WaitForEndOfFrame();
  22. WWW www = WWW.LoadFromCacheOrDownload($"{Application.streamingAssetsPath}/{ab_name}", 0);
  23. yield return www;
  24. if (www.isDone)
  25. {
  26. var tempAb = www.assetBundle;
  27. GameObject[] objs = tempAb.LoadAllAssets<GameObject>();
  28. for (int i = 0; i < objs.Length; i++)
  29. {
  30. GameObject insObj=Instantiate(objs[i], insRoot);
  31. if (addMat)
  32. {
  33. insObj.GetComponent<MeshRenderer>().material = defulMat;
  34. }
  35. yield return wait;
  36. }
  37. }
  38. else
  39. {
  40. Debug.LogError($"{ab_name}下载失败:{www.error}");
  41. }
  42. www.Dispose();
  43. }
  44. }