LoadDLL2.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.Networking;
  6. public class LoadDll2
  7. {
  8. private static string GetWebRequestPath(string asset)
  9. {
  10. var path = $"{Application.streamingAssetsPath}/{asset}";
  11. if (!path.Contains("://"))
  12. {
  13. path = "file://" + path;
  14. }
  15. return path;
  16. }
  17. static Dictionary<string, AssetBundle> ablist = new Dictionary<string, AssetBundle>();
  18. public static IEnumerator DownLoadAssets(Action<AssetBundle> callback)
  19. {
  20. if (ablist.ContainsKey("obe"))
  21. {
  22. callback.Invoke(ablist["obe"]);
  23. yield return null;
  24. }
  25. else
  26. {
  27. string dllPath = GetWebRequestPath("obe");
  28. Debug.Log($"start download asset:{dllPath}");
  29. UnityWebRequest www = UnityWebRequest.Get(dllPath);
  30. yield return www.SendWebRequest();
  31. #if UNITY_2020_1_OR_NEWER
  32. if (www.result != UnityWebRequest.Result.Success)
  33. {
  34. Debug.Log(www.error);
  35. }
  36. #else
  37. if (www.isHttpError || www.isNetworkError)
  38. {
  39. Debug.Log(www.error);
  40. }
  41. #endif
  42. else
  43. {
  44. if (ablist.ContainsKey("obe"))
  45. {
  46. callback.Invoke(ablist["obe"]);
  47. }
  48. else
  49. {
  50. Debug.Log("assets===>" + "obe");
  51. // Or retrieve results as binary data
  52. byte[] assetData = www.downloadHandler.data;
  53. AssetBundle ab = AssetBundle.LoadFromMemory(assetData);
  54. ablist.Add("obe", ab);
  55. callback.Invoke(ab);
  56. UnityEngine.Object[] assets = ab.LoadAllAssets();
  57. for (int i = 0; i < assets.Length; i++)
  58. {
  59. Debug.Log("assets===>" + assets[i].name);
  60. yield return null;
  61. }
  62. }
  63. }
  64. }
  65. }
  66. }