LoadAssetBundle.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.Networking;
  6. public class LoadAssetBundle : MonoSingleton<LoadAssetBundle>
  7. {
  8. private AssetBundleCreateRequest acr;
  9. // Start is called before the first frame update
  10. void Start()
  11. {
  12. StartCoroutine(DownLoadAssetBundle());
  13. }
  14. // Update is called once per frame
  15. void Update()
  16. {
  17. }
  18. IEnumerator DownLoadAssetBundle()
  19. {
  20. string artUrl = "https://wx-model-1317557471.cos.ap-shanghai.myqcloud.com/HotUpdateLangChao/langchaostart";
  21. yield return DownLoadAssets(artUrl, "HotUpdateAssets", false);
  22. }
  23. public IEnumerator DownLoadAssets(string dllPath, string name, bool isRemote = true)
  24. {
  25. //if (!isRemote)
  26. //{
  27. // dllPath = GetWebRequestPath(name);
  28. //}
  29. UnityWebRequest www = UnityWebRequest.Get(dllPath);
  30. www.SendWebRequest();
  31. while (!www.isDone)
  32. {
  33. yield return null;
  34. }
  35. #if UNITY_2020_1_OR_NEWER
  36. if (www.result != UnityWebRequest.Result.Success)
  37. {
  38. Debug.Log(www.error);
  39. }
  40. #else
  41. if (www.isHttpError || www.isNetworkError)
  42. {
  43. Debug.Log(www.error);
  44. }
  45. #endif
  46. else
  47. {
  48. {
  49. acr = AssetBundle.LoadFromMemoryAsync(www.downloadHandler.data);
  50. while (!acr.isDone)
  51. {
  52. yield return null;
  53. }
  54. //AssetBundleRequest abr = acr.assetBundle.LoadAssetAsync<GameObject>("LangChaoStart");
  55. //while (!abr.isDone)
  56. //{
  57. // yield return null;
  58. //}
  59. //Debug.Log(LoadByte("emqxsl-ca").Length);
  60. //this.gameObject.SetActive(false);
  61. }
  62. }
  63. }
  64. private string GetWebRequestPath(string asset)
  65. {
  66. var path = $"{Application.streamingAssetsPath}/{asset}";
  67. if (!path.Contains("://"))
  68. {
  69. path = "file://" + path;
  70. }
  71. return path;
  72. }
  73. public byte[] LoadByte( string path)
  74. {
  75. acr.assetBundle.LoadAsset<TextAsset>(path);
  76. return (acr.assetBundle.LoadAsset<TextAsset>(path)as TextAsset).bytes;
  77. }
  78. }