修复框架
This commit is contained in:
+19
-14
@@ -7,19 +7,14 @@ using System.Runtime.CompilerServices;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
/// <summary>
|
||||
/// 网络请求工具
|
||||
/// Web请求执行模块
|
||||
/// </summary>
|
||||
public static class WebRequest {
|
||||
public static TaskAwaiter<object> GetAwaiter(this UnityWebRequestAsyncOperation op) {
|
||||
var tcs = new TaskCompletionSource<object>();
|
||||
op.completed += (obj) => { tcs.SetResult(null); };
|
||||
return tcs.Task.GetAwaiter();
|
||||
}
|
||||
public class ExecuteWebRequest : ModuleExecute<DataRequest> {
|
||||
/// <summary> 发送请求 </summary>
|
||||
public static void Send(DataRequest request) {
|
||||
public void Execute(DataRequest 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.PostForm) { PostForm(request); }
|
||||
if (request.RequestType == WebRequestType.PostJson) { PostJson(request); }
|
||||
if (request.RequestType == WebRequestType.Texture) { Texture(request); }
|
||||
}
|
||||
public static async void Get(DataRequest request) {
|
||||
@@ -63,15 +58,25 @@ public static class WebRequest {
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Web请求执行模块工具
|
||||
/// </summary>
|
||||
public static class ExecuteWebRequestTool {
|
||||
public static TaskAwaiter<object> GetAwaiter(this UnityWebRequestAsyncOperation op) {
|
||||
var tcs = new TaskCompletionSource<object>();
|
||||
op.completed += (obj) => { tcs.SetResult(null); };
|
||||
return tcs.Task.GetAwaiter();
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Web请求类型
|
||||
/// </summary>
|
||||
public enum WebRequestType {
|
||||
/// <summary> GET </summary>
|
||||
GET = 0,
|
||||
/// <summary> POST 表单 </summary>
|
||||
POSTFORM = 1,
|
||||
PostForm = 1,
|
||||
/// <summary> POST Json </summary>
|
||||
POSTJSON = 2,
|
||||
PostJson = 2,
|
||||
/// <summary> GET 获取图片 </summary>
|
||||
Texture = 3
|
||||
}
|
||||
@@ -136,14 +141,14 @@ public class DataRequestPost : DataRequest {
|
||||
this.url = url;
|
||||
this.json = json;
|
||||
this.OnCallback = OnCallback;
|
||||
type = WebRequestType.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 = WebRequestType.POSTFORM;
|
||||
type = WebRequestType.PostForm;
|
||||
}
|
||||
|
||||
public override void RequestResultHandle(bool isDone, DownloadHandler downloadHandler) {
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fdd231128f69bd54a915b218d734fef6
|
||||
guid: 031fdf250828c30419ed1a945eb538f8
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
@@ -7,5 +7,5 @@ using UnityEngine;
|
||||
/// </summary>
|
||||
public interface ModuleExecute<Data> {
|
||||
/// <summary> 执行 </summary>
|
||||
public void Compute(Data data);
|
||||
public void Execute(Data data);
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// 固定数据物体模块
|
||||
/// 场景中的固定模块
|
||||
/// </summary>
|
||||
public abstract class ModuleFixed : MonoBehaviour {
|
||||
/// <summary> 核心模块 </summary>
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fcb15023389a9c141b1b07fe53f06e5c
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>json解析与保存</summary>
|
||||
public static class JsonTool {
|
||||
/// <summary> 内部包装类 </summary>
|
||||
private class Pack<T> { public T data; }
|
||||
/// <summary> 把对象转换为Json字符串 </summary>
|
||||
/// <param name="obj">对象</param>
|
||||
public static string ToJson<T>(T obj) {
|
||||
if (obj == null) return "null";
|
||||
if (obj.GetType().GetInterface("IList") != null) {
|
||||
Pack<T> pack = new Pack<T>();
|
||||
pack.data = obj;
|
||||
string json = JsonUtility.ToJson(pack);
|
||||
return json.Substring(8, json.Length - 9);
|
||||
}
|
||||
return JsonUtility.ToJson(obj);
|
||||
}
|
||||
/// <summary> 解析Json </summary>
|
||||
/// <typeparam name="T">类型</typeparam>
|
||||
/// <param name="json">Json字符串</param>
|
||||
public static T FromJson<T>(string json) {
|
||||
if (json == "null" && typeof(T).IsClass) return default(T);
|
||||
if (typeof(T).GetInterface("IList") != null) {
|
||||
json = "{\"data\":{data}}".Replace("{data}", json);
|
||||
Pack<T> Pack = JsonUtility.FromJson<Pack<T>>(json);
|
||||
return Pack.data;
|
||||
}
|
||||
return JsonUtility.FromJson<T>(json);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 007e3acdca87e1b49b38f504938206cb
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -6,14 +6,16 @@ using UnityEngine;
|
||||
/// <summary>
|
||||
/// 单例模块
|
||||
/// </summary>
|
||||
public abstract class ModuleSingle<Data> : MonoBehaviour {
|
||||
public abstract class ModuleSingle<T> : MonoBehaviour where T : ModuleSingle<T> {
|
||||
/// <summary> 模块单例 </summary>
|
||||
public static ModuleSingle<Data> I => instance;
|
||||
public static T I => instance;
|
||||
/// <summary> 模块单例 </summary>
|
||||
protected static ModuleSingle<Data> instance;
|
||||
protected static T instance;
|
||||
/// <summary> 核心模块 </summary>
|
||||
protected virtual ModuleCore ModuleCore => ModuleCore.I;
|
||||
/// <summary> 初始化 </summary>
|
||||
protected virtual void Awake() {
|
||||
if (instance != null) { Destroy(instance.gameObject); }
|
||||
instance = this;
|
||||
instance = (T)this;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user