Unity 封装的网络请求类
2019-11-22 本文已影响0人
贼噶人
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
namespace Script.Network
{
public class HttpManager : MonoBehaviour
{
private static readonly Dictionary<UnityWebRequest,OnFileDownloadCallback> FileDownloadCallbackDictionary =
new Dictionary<UnityWebRequest, OnFileDownloadCallback>();
private static HttpManager _instance;
private void Awake()
{
if (null == _instance)
{
_instance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
}
public class OnFileDownloadCallback
{
public readonly Action<float> ProgressAction;
public readonly Action<string> StatesAction;
public OnFileDownloadCallback(Action<float> progressAction
, Action<string> statesAction)
{
ProgressAction = progressAction;
StatesAction = statesAction;
}
}
private void Update()
{
foreach (var item in FileDownloadCallbackDictionary)
{
item.Value.ProgressAction?
.Invoke(item.Key.downloadProgress);
}
}
public static UnityWebRequest Download(string uri, OnFileDownloadCallback callback)
{
var request = UnityWebRequest.Get(uri);
request.downloadHandler = new DownloadHandlerFile(Application.streamingAssetsPath +"/m.mp4");
_instance.StartCoroutine(DownloadRequest(request,callback));
return request;
}
private static IEnumerator DownloadRequest(UnityWebRequest request
, OnFileDownloadCallback callback)
{
using (request)
{
FileDownloadCallbackDictionary.Add(request,callback);
yield return request.SendWebRequest();
if (request.isDone && 200 == request.responseCode
&& !request.isHttpError && !request.isNetworkError)
{
callback.ProgressAction?.Invoke(1);
callback.StatesAction?.Invoke(null);
}
else
{
callback.StatesAction?.Invoke(request.error);
}
FileDownloadCallbackDictionary.Remove(request);
}
}
private static IEnumerator Request<T>(UnityWebRequest request
,Action<T> callback) where T : BaseResponse
{
using (request)
{
yield return request.SendWebRequest();
if (request.isHttpError || request.isNetworkError)
{
var response = Activator.CreateInstance<T>();
{
response.result = -1;
response.dec = request.error;
}
callback?.Invoke(response);
}
else if (200 != request.responseCode)
{
var response = Activator.CreateInstance<T>();
{
response.result = (int) request.responseCode;
response.dec = $"{request.responseCode}";
}
callback?.Invoke(response);
}
else
{
try
{
var responseText = request.downloadHandler.text;
callback?.Invoke(JsonUtility.FromJson<T>(responseText));
}
catch (Exception e)
{
var response = Activator.CreateInstance<T>();
{
response.result = -2;
response.dec = $"{e.Message}";
}
callback?.Invoke(response);
}
}
}
}
public static UnityWebRequest DoPost<T>(string uri, Action<T> callback
, Dictionary<string, string> form = null) where T : BaseResponse
{
var request = UnityWebRequest.Post(uri, form);
if (null != _instance)
{
if (!_instance.gameObject.activeSelf)
{
_instance.gameObject.SetActive(true);
}
_instance.StartCoroutine(Request(request,callback));
}
else
{
var response = Activator.CreateInstance<T>();
{
response.result = -3;
response.dec = $"instance not init";
}
callback?.Invoke(response);
}
return request;
}
}
}