增加UI框架
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e7df15d428a2ca947a677d331d5848b5
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,66 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MuHua {
|
||||
/// <summary>
|
||||
/// 异步网络请求
|
||||
/// </summary>
|
||||
public static class AsyncWebRequest {
|
||||
/// <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); }
|
||||
}
|
||||
public static async void Get(DataRequest request) {
|
||||
string url = request.Url;
|
||||
using UnityWebRequest web = UnityWebRequest.Get(url);
|
||||
await web.SendWebRequest();
|
||||
bool isDone = web.isDone && web.result == UnityWebRequest.Result.Success;
|
||||
request.RequestResultHandle(isDone, web);
|
||||
}
|
||||
public static async void PostForm(DataRequest request) {
|
||||
string url = request.Url;
|
||||
WWWForm form = request.Form;
|
||||
using UnityWebRequest web = UnityWebRequest.Post(url, form);
|
||||
await web.SendWebRequest();
|
||||
bool isDone = web.isDone && web.result == UnityWebRequest.Result.Success;
|
||||
request.RequestResultHandle(isDone, web);
|
||||
}
|
||||
public static async void 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");
|
||||
await web.SendWebRequest();
|
||||
bool isDone = web.isDone && web.result == UnityWebRequest.Result.Success;
|
||||
request.RequestResultHandle(isDone, web);
|
||||
|
||||
}
|
||||
public static async void Texture(DataRequest request) {
|
||||
string url = request.Url;
|
||||
using UnityWebRequest web = UnityWebRequestTexture.GetTexture(url);
|
||||
await web.SendWebRequest();
|
||||
bool isDone = web.isDone && web.result == UnityWebRequest.Result.Success;
|
||||
request.RequestResultHandle(isDone, web);
|
||||
}
|
||||
public static TaskAwaiter<object> GetAwaiter(this UnityWebRequestAsyncOperation op) {
|
||||
var tcs = new TaskCompletionSource<object>();
|
||||
op.completed += (obj) => { tcs.SetResult(null); };
|
||||
return tcs.Task.GetAwaiter();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e7a92b3660a18a24b81b91fbaf67778b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,19 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace MuHua {
|
||||
/// <summary>
|
||||
/// Web请求类型
|
||||
/// </summary>
|
||||
public enum AsyncWebRequestType {
|
||||
/// <summary> GET </summary>
|
||||
GET = 0,
|
||||
/// <summary> POST 表单 </summary>
|
||||
PostForm = 1,
|
||||
/// <summary> POST Json </summary>
|
||||
PostJson = 2,
|
||||
/// <summary> GET 获取图片 </summary>
|
||||
Texture = 3
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8e945331334b0b6479a782aba1d68391
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,23 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
|
||||
namespace MuHua {
|
||||
/// <summary>
|
||||
/// 请求数据
|
||||
/// </summary>
|
||||
public abstract class DataRequest {
|
||||
/// <summary> Web请求地址 </summary>
|
||||
public abstract string Url { get; }
|
||||
/// <summary> Web请求类型 </summary>
|
||||
public abstract AsyncWebRequestType RequestType { get; }
|
||||
/// <summary> 提交json数据 </summary>
|
||||
public virtual string Json { get; }
|
||||
/// <summary> 提交Form表单数据 </summary>
|
||||
public virtual WWWForm Form { get; }
|
||||
|
||||
/// <summary> Web请求结果处理 </summary>
|
||||
public abstract void RequestResultHandle(bool isDone, UnityWebRequest web);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a81036c14ed55ea4aa3beea60ebaa871
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,32 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
|
||||
namespace MuHua {
|
||||
/// <summary>
|
||||
/// Get请求数据
|
||||
/// </summary>
|
||||
public class DataRequestGet : DataRequest {
|
||||
public readonly string url;
|
||||
|
||||
public Action<string> OnError;
|
||||
public Action<string> OnCallback;
|
||||
|
||||
public override string Url => url;
|
||||
public override AsyncWebRequestType RequestType => AsyncWebRequestType.GET;
|
||||
|
||||
/// <summary> Web Get请求数据 </summary>
|
||||
public DataRequestGet(string url, Action<string> OnCallback = null) {
|
||||
this.url = url;
|
||||
this.OnCallback = OnCallback;
|
||||
}
|
||||
|
||||
public override void RequestResultHandle(bool isDone, UnityWebRequest web) {
|
||||
DownloadHandler downloadHandler = web.downloadHandler;
|
||||
if (!isDone) { OnError?.Invoke(downloadHandler.text); return; }
|
||||
OnCallback?.Invoke(downloadHandler.text);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cf1da230bb48a6a4aa51686ff32e9f16
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,46 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
|
||||
namespace MuHua {
|
||||
/// <summary>
|
||||
/// Post请求数据
|
||||
/// </summary>
|
||||
public class DataRequestPost : DataRequest {
|
||||
public readonly string url;
|
||||
public readonly string json;
|
||||
public readonly WWWForm form;
|
||||
public readonly AsyncWebRequestType type;
|
||||
|
||||
public Action<string> OnError;
|
||||
public Action<string> OnCallback;
|
||||
|
||||
public override string Url => url;
|
||||
public override AsyncWebRequestType RequestType => type;
|
||||
public override string Json => json;
|
||||
public override WWWForm Form => form;
|
||||
|
||||
/// <summary> Web Post请求 提交json数据 </summary>
|
||||
public DataRequestPost(string url, string json, Action<string> OnCallback = null) {
|
||||
this.url = url;
|
||||
this.json = json;
|
||||
this.OnCallback = OnCallback;
|
||||
type = AsyncWebRequestType.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;
|
||||
}
|
||||
|
||||
public override void RequestResultHandle(bool isDone, UnityWebRequest web) {
|
||||
DownloadHandler downloadHandler = web.downloadHandler;
|
||||
if (!isDone) { OnError?.Invoke(downloadHandler.text); return; }
|
||||
OnCallback?.Invoke(downloadHandler.text);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f43bea5409c953347974214f4eb5320c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
|
||||
namespace MuHua {
|
||||
/// <summary>
|
||||
/// Get下载 Texture
|
||||
/// </summary>
|
||||
public class DataRequestTexture : DataRequest {
|
||||
public readonly string url;
|
||||
|
||||
public Action<string> OnError;
|
||||
public Action<Texture2D> OnCallback;
|
||||
|
||||
public override string Url => url;
|
||||
public override AsyncWebRequestType RequestType => AsyncWebRequestType.Texture;
|
||||
|
||||
/// <summary> Web Get请求 Texture </summary>
|
||||
public DataRequestTexture(string url, Action<Texture2D> OnCallback = null) {
|
||||
this.url = url;
|
||||
this.OnCallback = OnCallback;
|
||||
}
|
||||
|
||||
public override void RequestResultHandle(bool isDone, UnityWebRequest web) {
|
||||
DownloadHandler downloadHandler = web.downloadHandler;
|
||||
if (!isDone) { OnError?.Invoke(downloadHandler.text); return; }
|
||||
DownloadHandlerTexture dht = downloadHandler as DownloadHandlerTexture;
|
||||
Texture2D texture = dht.texture;
|
||||
bool compress = (texture.width % 2) == 0 && (texture.height % 2) == 0;
|
||||
if (compress) { texture.Compress(true); }
|
||||
else { Debug.LogWarning($"无法压缩的图片:{web.url}"); }
|
||||
OnCallback?.Invoke(texture);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b0407bff8565543469def55f9788ecf8
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"name": "MuHua.Network"
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c91b8ea8cc52e094d8b1e2506189cb24
|
||||
AssemblyDefinitionImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"name": "muhua-network",
|
||||
"version": "1.0.0",
|
||||
"displayName": "MuHua Network",
|
||||
"description": "\u5f02\u6b65web\u8bf7\u6c42\u5b9e\u73b0",
|
||||
"author": {
|
||||
"name": "MuHua",
|
||||
"email": "muhua233@qq.com"
|
||||
},
|
||||
"type": "tool",
|
||||
"samples": [
|
||||
{
|
||||
"displayName": "Example",
|
||||
"description": "An example showing how to use the Network.",
|
||||
"path": "Samples"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 79e879bafe8148846a79f11c726aab21
|
||||
PackageManifestImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user