using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking; public class AssetLoadHelper : MonoBehaviour { public static AssetLoadHelper _Instance; private Dictionary abRes_Dir; string[] resList = { "weathertool", "model" }; public bool isLoad { get { for (int i = 0; i < resList.Length; i++) { if (!abRes_Dir.ContainsKey(resList[i])) { return false; } } return true; } } private void Awake() { _Instance = this; LoadAB_res(); } private void LoadAB_res() { abRes_Dir = new Dictionary(); foreach (var t in resList) { StartCoroutine(LoadAB(t)); } } IEnumerator LoadAB(string _name) { string abPath = "/AB/"; if (Application.isEditor) { abPath = "/ABW/"; } UnityWebRequest www = UnityWebRequestAssetBundle.GetAssetBundle(Application.streamingAssetsPath + abPath + _name); yield return www.SendWebRequest(); if (www.result != UnityWebRequest.Result.Success) { Debug.LogError(www.result); } else { AssetBundle ab= DownloadHandlerAssetBundle.GetContent(www); if (ab != null) { abRes_Dir.TryAdd(_name, ab); } } www.Dispose(); } public T LoadAssets(string abName, string resName) where T : UnityEngine.Object { T obj = null; if (!abRes_Dir.ContainsKey(abName)) { Debug.LogError($"{abName}丢失"); } else { if (typeof(T) == typeof(GameObject)) { obj = Instantiate(abRes_Dir[abName].LoadAsset(resName)); } else { obj = abRes_Dir[abName].LoadAsset(resName); } } return obj; } }