TestAB.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. // Update is called once per frame
  13. void Update()
  14. {
  15. }
  16. private IEnumerator DoLoadAssetBundle()
  17. {
  18. string url = Application.streamingAssetsPath + "/AssetBundles/Android/saloonmain";
  19. Debug.Log(url);
  20. using (UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(url))
  21. {
  22. yield return request.SendWebRequest();
  23. if (request.isHttpError || request.isNetworkError)
  24. {
  25. // 下载出错
  26. print(request.error);
  27. }
  28. else
  29. {
  30. // 下载完成
  31. AssetBundle assetBundle = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
  32. GameObject obj = assetBundle.LoadAsset<GameObject>(assetBundle.GetAllAssetNames()[0]);
  33. GameObject obj2 = GameObject.Instantiate(obj);
  34. // 优先释放request 会降低内存峰值
  35. request.Dispose();
  36. }
  37. }
  38. }
  39. }