增加UI框架
This commit is contained in:
+3
-3
@@ -27,13 +27,13 @@ csharp_new_line_before_open_brace = none
|
||||
# } else {
|
||||
# ...
|
||||
# }
|
||||
csharp_new_line_before_else = false
|
||||
#csharp_new_line_before_else = false
|
||||
# try {
|
||||
# ...
|
||||
# } catch (Exception e) {
|
||||
# ...
|
||||
# }
|
||||
csharp_new_line_before_catch = false
|
||||
#csharp_new_line_before_catch = false
|
||||
# try {
|
||||
# ...
|
||||
# } catch (Exception e) {
|
||||
@@ -41,6 +41,6 @@ csharp_new_line_before_catch = false
|
||||
# } finally {
|
||||
# ...
|
||||
# }
|
||||
csharp_new_line_before_finally = false
|
||||
#csharp_new_line_before_finally = false
|
||||
# 错误提示,如果不正常可以删掉
|
||||
# dotnet_diagnostic.IDE0055.severity = error
|
||||
@@ -139,7 +139,7 @@ GameObject:
|
||||
m_Icon: {fileID: 0}
|
||||
m_NavMeshLayer: 0
|
||||
m_StaticEditorFlags: 0
|
||||
m_IsActive: 1
|
||||
m_IsActive: 0
|
||||
--- !u!114 &36355708
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
|
||||
@@ -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:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0fec0c5043434164c8a5cb28f3886b1d
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,41 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace MuHua {
|
||||
/// <summary>
|
||||
/// UI容器
|
||||
/// </summary>
|
||||
public class UIContainer<Item, Data> where Item : UIItem<Data> {
|
||||
public readonly VisualElement container;
|
||||
public readonly VisualTreeAsset templateAsset;
|
||||
public readonly Func<Data, VisualElement, Item> generate;
|
||||
public List<Item> uiItems = new List<Item>();
|
||||
/// <summary> UI容器 </summary>
|
||||
public UIContainer(VisualElement container, VisualTreeAsset templateAsset, Func<Data, VisualElement, Item> generate) {
|
||||
this.container = container;
|
||||
this.templateAsset = templateAsset;
|
||||
this.generate = generate;
|
||||
}
|
||||
/// <summary> 释放资源 </summary>
|
||||
public void Release() {
|
||||
container.Clear();
|
||||
uiItems.ForEach(obj => obj.Release());
|
||||
uiItems = new List<Item>();
|
||||
}
|
||||
/// <summary> 创建UI项 </summary>
|
||||
public void Create(List<Data> datas) {
|
||||
Release();
|
||||
datas.ForEach(Create);
|
||||
}
|
||||
/// <summary> 创建UI项 </summary>
|
||||
public void Create(Data data) {
|
||||
VisualElement element = templateAsset.Instantiate();
|
||||
Item item = generate(data, element);
|
||||
container.Add(item.element);
|
||||
uiItems.Add(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b5854bcf80117aa49b327f2c19914ba3
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,42 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace MuHua {
|
||||
/// <summary>
|
||||
/// UI项
|
||||
/// </summary>
|
||||
public abstract class UIItem<Data> {
|
||||
/// <summary> 选择事件 </summary>
|
||||
public static event Action<Data> OnSelect;
|
||||
/// <summary> 触发事件 </summary>
|
||||
public static void Select(Data data) => OnSelect?.Invoke(data);
|
||||
|
||||
/// <summary> 绑定的数据 </summary>
|
||||
public readonly Data value;
|
||||
/// <summary> 绑定的元素 </summary>
|
||||
public readonly VisualElement element;
|
||||
/// <summary> UI项 </summary>
|
||||
public UIItem(Data value, VisualElement element) {
|
||||
this.value = value;
|
||||
this.element = element;
|
||||
OnSelect += UIItem_OnSelect;
|
||||
}
|
||||
|
||||
/// <summary> 侦听选择事件 </summary>
|
||||
public virtual void UIItem_OnSelect(Data obj) {
|
||||
if (value.Equals(obj)) { SelectState(); } else { DefaultState(); }
|
||||
}
|
||||
|
||||
/// <summary> 触发选择事件 </summary>
|
||||
public virtual void Select() => OnSelect?.Invoke(value);
|
||||
/// <summary> 默认状态 </summary>
|
||||
public virtual void DefaultState() { }
|
||||
/// <summary> 选中状态 </summary>
|
||||
public virtual void SelectState() { }
|
||||
/// <summary> 释放 </summary>
|
||||
public virtual void Release() => OnSelect -= UIItem_OnSelect;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: dfa6a8682a5ec384d98b88b54ace505f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -227,6 +227,12 @@
|
||||
"source": "embedded",
|
||||
"dependencies": {}
|
||||
},
|
||||
"muhua-network": {
|
||||
"version": "file:Network",
|
||||
"depth": 0,
|
||||
"source": "embedded",
|
||||
"dependencies": {}
|
||||
},
|
||||
"muhua-ui-tool": {
|
||||
"version": "file:UITool",
|
||||
"depth": 0,
|
||||
|
||||
Reference in New Issue
Block a user