1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.Networking;
- public class AssetLoadHelper : MonoBehaviour
- {
- public static AssetLoadHelper _Instance;
- private Dictionary<string, AssetBundle> 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<string, AssetBundle>();
- 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<T>(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<T>(resName));
- }
- else
- {
- obj = abRes_Dir[abName].LoadAsset<T>(resName);
- }
- }
- return obj;
- }
- }
|