123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.Networking;
- public class LoadDll2
- {
- private static string GetWebRequestPath(string asset)
- {
- var path = $"{Application.streamingAssetsPath}/{asset}";
- if (!path.Contains("://"))
- {
- path = "file://" + path;
- }
- return path;
- }
- static Dictionary<string, AssetBundle> ablist = new Dictionary<string, AssetBundle>();
- public static IEnumerator DownLoadAssets(Action<AssetBundle> callback)
- {
- if (ablist.ContainsKey("obe"))
- {
- callback.Invoke(ablist["obe"]);
- yield return null;
- }
- else
- {
- string dllPath = GetWebRequestPath("obe");
- Debug.Log($"start download asset:{dllPath}");
- UnityWebRequest www = UnityWebRequest.Get(dllPath);
- yield return www.SendWebRequest();
- #if UNITY_2020_1_OR_NEWER
- if (www.result != UnityWebRequest.Result.Success)
- {
- Debug.Log(www.error);
- }
- #else
- if (www.isHttpError || www.isNetworkError)
- {
- Debug.Log(www.error);
- }
- #endif
- else
- {
- if (ablist.ContainsKey("obe"))
- {
- callback.Invoke(ablist["obe"]);
- }
- else
- {
- Debug.Log("assets===>" + "obe");
- // Or retrieve results as binary data
- byte[] assetData = www.downloadHandler.data;
- AssetBundle ab = AssetBundle.LoadFromMemory(assetData);
- ablist.Add("obe", ab);
- callback.Invoke(ab);
- UnityEngine.Object[] assets = ab.LoadAllAssets();
- for (int i = 0; i < assets.Length; i++)
- {
- Debug.Log("assets===>" + assets[i].name);
- yield return null;
- }
- }
- }
- }
- }
- }
|