CultivateImmortal/Assets/Scripts/Framework/UWQ/UWQResMgr.cs

63 lines
2.2 KiB
C#
Raw Normal View History

2024-11-14 18:15:51 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.Networking;
public class UWQResMgr : SingletonAutoMono<UWQResMgr>
{
/// <summary>
/// <20><><EFBFBD><EFBFBD>UnityWebRequestȥ<74><C8A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ
/// </summary>
/// <typeparam name="T"><3E><><EFBFBD><EFBFBD>ֻ<EFBFBD><D6BB><EFBFBD><EFBFBD>string<6E><67>byte[]<5D><>Texture<72><65>AssetBundle <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Ŀǰ<C4BF><C7B0>֧<EFBFBD><D6A7></typeparam>
/// <param name="path"><3E><>Դ·<D4B4><C2B7><EFBFBD><EFBFBD>Ҫ<EFBFBD>Լ<EFBFBD><D4BC><EFBFBD><EFBFBD><EFBFBD>Э<EFBFBD><D0AD> http<74><70>ftp<74><70>file</param>
/// <param name="callBack"><3E><><EFBFBD>سɹ<D8B3><C9B9>Ļص<C4BB><D8B5><EFBFBD><EFBFBD><EFBFBD></param>
/// <param name="failCallBack"><3E><><EFBFBD><EFBFBD>ʧ<EFBFBD>ܵĻص<C4BB><D8B5><EFBFBD><EFBFBD><EFBFBD></param>
public void LoadRes<T>(string path, UnityAction<T> callBack, UnityAction failCallBack) where T : class
{
StartCoroutine(ReallyLoadRes<T>(path, callBack, failCallBack));
}
private IEnumerator ReallyLoadRes<T>(string path, UnityAction<T> callBack, UnityAction failCallBack) where T:class
{
//string
//byte[]
//Texture
//AssetBundle
Type type = typeof(T);
//<2F><><EFBFBD>ڼ<EFBFBD><DABC>صĶ<D8B5><C4B6><EFBFBD>
UnityWebRequest req = null;
if (type == typeof(string) ||
type == typeof(byte[]))
req = UnityWebRequest.Get(path);
else if (type == typeof(Texture))
req = UnityWebRequestTexture.GetTexture(path);
else if (type == typeof(AssetBundle))
req = UnityWebRequestAssetBundle.GetAssetBundle(path);
else
{
failCallBack?.Invoke();
yield break;
}
yield return req.SendWebRequest();
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>سɹ<D8B3>
if (req.result == UnityWebRequest.Result.Success)
{
if (type == typeof(string))
callBack?.Invoke(req.downloadHandler.text as T);
else if (type == typeof(byte[]))
callBack?.Invoke(req.downloadHandler.data as T);
else if (type == typeof(Texture))
callBack?.Invoke(DownloadHandlerTexture.GetContent(req) as T);
else if (type == typeof(AssetBundle))
callBack?.Invoke(DownloadHandlerAssetBundle.GetContent(req) as T);
}
else
failCallBack?.Invoke();
//<2F>ͷ<EFBFBD>UWQ<57><51><EFBFBD><EFBFBD>
req.Dispose();
}
}