TestAB.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Networking;
  5. public class TestAB : MonoBehaviour
  6. {
  7. // Start is called before the first frame update
  8. void Start()
  9. {
  10. StartCoroutine(DoLoadAssetBundle());
  11. }
  12. private IEnumerator DoLoadAssetBundle()
  13. {
  14. string url = Application.streamingAssetsPath + "/mrvideo";
  15. Debug.Log(url);
  16. using (UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(url))
  17. {
  18. yield return request.SendWebRequest();
  19. if (request.isHttpError || request.isNetworkError)
  20. {
  21. // 下载出错
  22. print(request.error);
  23. }
  24. else
  25. {
  26. // 下载完成
  27. AssetBundle assetBundle = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
  28. GameObject obj = assetBundle.LoadAsset<GameObject>(assetBundle.GetAllAssetNames()[0]);
  29. GameObject obj2 = GameObject.Instantiate(obj);
  30. obj2.AddComponent<MovieScreen>();
  31. obj2.transform.parent = transform;
  32. obj2.transform.localPosition = Vector3.zero;
  33. // 优先释放request 会降低内存峰值
  34. // request.Dispose();
  35. }
  36. }
  37. }
  38. }