修改网络工具包
增加协程调用方法
This commit is contained in:
@@ -25,7 +25,7 @@ public class AnalysisCollector : Module<AnalysisCollector> {
|
||||
SaveTool.SaveText(FileName.Create(code), json);
|
||||
StockToAnalysis(json, action);
|
||||
};
|
||||
AsyncWebRequest.Execute(request);
|
||||
WebRequestAsync.Execute(request);
|
||||
}
|
||||
|
||||
public void StockToAnalysis(string json, Action<List<DataAnalysis>> action = null) {
|
||||
|
||||
@@ -11,7 +11,7 @@ namespace MuHua {
|
||||
/// <summary> Web请求地址 </summary>
|
||||
public abstract string Url { get; }
|
||||
/// <summary> Web请求类型 </summary>
|
||||
public abstract AsyncWebRequestType RequestType { get; }
|
||||
public abstract WebRequestType RequestType { get; }
|
||||
/// <summary> 提交json数据 </summary>
|
||||
public virtual string Json { get; }
|
||||
/// <summary> 提交Form表单数据 </summary>
|
||||
|
||||
@@ -15,7 +15,7 @@ namespace MuHua {
|
||||
public Action<string> OnCallback;
|
||||
|
||||
public override string Url => url;
|
||||
public override AsyncWebRequestType RequestType => AsyncWebRequestType.GET;
|
||||
public override WebRequestType RequestType => WebRequestType.GET;
|
||||
|
||||
/// <summary> Web Get请求数据 </summary>
|
||||
public DataRequestGet(string url, Action<string> OnCallback = null) {
|
||||
|
||||
@@ -12,13 +12,13 @@ namespace MuHua {
|
||||
public readonly string url;
|
||||
public readonly string json;
|
||||
public readonly WWWForm form;
|
||||
public readonly AsyncWebRequestType type;
|
||||
public readonly WebRequestType type;
|
||||
|
||||
public Action<string> OnError;
|
||||
public Action<string> OnCallback;
|
||||
|
||||
public override string Url => url;
|
||||
public override AsyncWebRequestType RequestType => type;
|
||||
public override WebRequestType RequestType => type;
|
||||
public override string Json => json;
|
||||
public override WWWForm Form => form;
|
||||
|
||||
@@ -27,14 +27,14 @@ namespace MuHua {
|
||||
this.url = url;
|
||||
this.json = json;
|
||||
this.OnCallback = OnCallback;
|
||||
type = AsyncWebRequestType.PostJson;
|
||||
type = WebRequestType.PostJson;
|
||||
}
|
||||
/// <summary> Web Post请求 提交WWWForm数据 </summary>
|
||||
public DataRequestPost(string url, WWWForm form, Action<string> OnCallback = null) {
|
||||
this.url = url;
|
||||
this.form = form;
|
||||
this.OnCallback = OnCallback;
|
||||
type = AsyncWebRequestType.PostForm;
|
||||
type = WebRequestType.PostForm;
|
||||
}
|
||||
|
||||
public override void RequestResultHandle(bool isDone, UnityWebRequest web) {
|
||||
|
||||
@@ -15,7 +15,7 @@ namespace MuHua {
|
||||
public Action<Texture2D> OnCallback;
|
||||
|
||||
public override string Url => url;
|
||||
public override AsyncWebRequestType RequestType => AsyncWebRequestType.Texture;
|
||||
public override WebRequestType RequestType => WebRequestType.Texture;
|
||||
|
||||
/// <summary> Web Get请求 Texture </summary>
|
||||
public DataRequestTexture(string url, Action<Texture2D> OnCallback = null) {
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
|
||||
namespace MuHua {
|
||||
/// <summary>
|
||||
/// 协程网络请求
|
||||
/// </summary>
|
||||
public static class WebRequest {
|
||||
/// <summary> 发送请求 </summary>
|
||||
public static IEnumerator Execute(DataRequest request) {
|
||||
if (request.RequestType == WebRequestType.GET) { yield return Get(request); }
|
||||
if (request.RequestType == WebRequestType.PostForm) { yield return PostForm(request); }
|
||||
if (request.RequestType == WebRequestType.PostJson) { yield return PostJson(request); }
|
||||
if (request.RequestType == WebRequestType.Texture) { yield return Texture(request); }
|
||||
}
|
||||
public static IEnumerator Get(DataRequest request) {
|
||||
string url = request.Url;
|
||||
using UnityWebRequest web = UnityWebRequest.Get(url);
|
||||
yield return web.SendWebRequest();
|
||||
bool isDone = web.isDone && web.result == UnityWebRequest.Result.Success;
|
||||
request.RequestResultHandle(isDone, web);
|
||||
}
|
||||
public static IEnumerator PostForm(DataRequest request) {
|
||||
string url = request.Url;
|
||||
WWWForm form = request.Form;
|
||||
using UnityWebRequest web = UnityWebRequest.Post(url, form);
|
||||
yield return web.SendWebRequest();
|
||||
bool isDone = web.isDone && web.result == UnityWebRequest.Result.Success;
|
||||
request.RequestResultHandle(isDone, web);
|
||||
}
|
||||
public static IEnumerator PostJson(DataRequest request) {
|
||||
string url = request.Url;
|
||||
string json = request.Json;
|
||||
byte[] postBytes = System.Text.Encoding.Default.GetBytes(json);
|
||||
#if UNITY_2022
|
||||
using UnityWebRequest web = UnityWebRequest.PostWwwForm(url, "POST");
|
||||
#else
|
||||
using UnityWebRequest web = UnityWebRequest.Post(url, "POST");
|
||||
#endif
|
||||
web.uploadHandler.Dispose();
|
||||
web.uploadHandler = new UploadHandlerRaw(postBytes);
|
||||
web.SetRequestHeader("Content-Type", "application/json");
|
||||
yield return web.SendWebRequest();
|
||||
bool isDone = web.isDone && web.result == UnityWebRequest.Result.Success;
|
||||
request.RequestResultHandle(isDone, web);
|
||||
|
||||
}
|
||||
public static IEnumerator Texture(DataRequest request) {
|
||||
string url = request.Url;
|
||||
using UnityWebRequest web = UnityWebRequestTexture.GetTexture(url);
|
||||
yield return web.SendWebRequest();
|
||||
bool isDone = web.isDone && web.result == UnityWebRequest.Result.Success;
|
||||
request.RequestResultHandle(isDone, web);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c27ad4aa76c0a044f8ed6b3c3e062cf2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+5
-5
@@ -10,13 +10,13 @@ namespace MuHua {
|
||||
/// <summary>
|
||||
/// 异步网络请求
|
||||
/// </summary>
|
||||
public static class AsyncWebRequest {
|
||||
public static class WebRequestAsync {
|
||||
/// <summary> 发送请求 </summary>
|
||||
public static void Execute(DataRequest request) {
|
||||
if (request.RequestType == AsyncWebRequestType.GET) { Get(request); }
|
||||
if (request.RequestType == AsyncWebRequestType.PostForm) { PostForm(request); }
|
||||
if (request.RequestType == AsyncWebRequestType.PostJson) { PostJson(request); }
|
||||
if (request.RequestType == AsyncWebRequestType.Texture) { Texture(request); }
|
||||
if (request.RequestType == WebRequestType.GET) { Get(request); }
|
||||
if (request.RequestType == WebRequestType.PostForm) { PostForm(request); }
|
||||
if (request.RequestType == WebRequestType.PostJson) { PostJson(request); }
|
||||
if (request.RequestType == WebRequestType.Texture) { Texture(request); }
|
||||
}
|
||||
public static async void Get(DataRequest request) {
|
||||
string url = request.Url;
|
||||
+1
-1
@@ -6,7 +6,7 @@ namespace MuHua {
|
||||
/// <summary>
|
||||
/// Web请求类型
|
||||
/// </summary>
|
||||
public enum AsyncWebRequestType {
|
||||
public enum WebRequestType {
|
||||
/// <summary> GET </summary>
|
||||
GET = 0,
|
||||
/// <summary> POST 表单 </summary>
|
||||
@@ -8,6 +8,7 @@
|
||||
"com.unity.render-pipelines.universal": "14.0.11",
|
||||
"com.unity.textmeshpro": "3.0.6",
|
||||
"com.unity.timeline": "1.7.6",
|
||||
"com.unity.toolchain.win-x86_64-linux-x86_64": "2.0.10",
|
||||
"com.unity.ugui": "1.0.0",
|
||||
"com.unity.visualscripting": "1.9.4",
|
||||
"com.unity.modules.ai": "1.0.0",
|
||||
|
||||
@@ -163,6 +163,22 @@
|
||||
"com.unity.searcher": "4.9.2"
|
||||
}
|
||||
},
|
||||
"com.unity.sysroot": {
|
||||
"version": "2.0.10",
|
||||
"depth": 1,
|
||||
"source": "registry",
|
||||
"dependencies": {},
|
||||
"url": "https://packages.unity.cn"
|
||||
},
|
||||
"com.unity.sysroot.linux-x86_64": {
|
||||
"version": "2.0.9",
|
||||
"depth": 1,
|
||||
"source": "registry",
|
||||
"dependencies": {
|
||||
"com.unity.sysroot": "2.0.10"
|
||||
},
|
||||
"url": "https://packages.unity.cn"
|
||||
},
|
||||
"com.unity.test-framework": {
|
||||
"version": "1.1.33",
|
||||
"depth": 1,
|
||||
@@ -205,6 +221,16 @@
|
||||
},
|
||||
"url": "https://packages.unity.cn"
|
||||
},
|
||||
"com.unity.toolchain.win-x86_64-linux-x86_64": {
|
||||
"version": "2.0.10",
|
||||
"depth": 0,
|
||||
"source": "registry",
|
||||
"dependencies": {
|
||||
"com.unity.sysroot": "2.0.10",
|
||||
"com.unity.sysroot.linux-x86_64": "2.0.9"
|
||||
},
|
||||
"url": "https://packages.unity.cn"
|
||||
},
|
||||
"com.unity.ugui": {
|
||||
"version": "1.0.0",
|
||||
"depth": 0,
|
||||
|
||||
Reference in New Issue
Block a user