diff --git a/.editorconfig b/.editorconfig index e008dee..9b10c4b 100644 --- a/.editorconfig +++ b/.editorconfig @@ -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 \ No newline at end of file diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 4c182e0..0c78944 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -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 diff --git a/Packages/Network/Runtime.meta b/Packages/Network/Runtime.meta new file mode 100644 index 0000000..9c2d2b4 --- /dev/null +++ b/Packages/Network/Runtime.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: e7df15d428a2ca947a677d331d5848b5 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/Network/Runtime/AsyncWebRequest.cs b/Packages/Network/Runtime/AsyncWebRequest.cs new file mode 100644 index 0000000..0edb12a --- /dev/null +++ b/Packages/Network/Runtime/AsyncWebRequest.cs @@ -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 { + /// + /// 异步网络请求 + /// + public static class AsyncWebRequest { + /// 发送请求 + 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 GetAwaiter(this UnityWebRequestAsyncOperation op) { + var tcs = new TaskCompletionSource(); + op.completed += (obj) => { tcs.SetResult(null); }; + return tcs.Task.GetAwaiter(); + } + } +} \ No newline at end of file diff --git a/Packages/Network/Runtime/AsyncWebRequest.cs.meta b/Packages/Network/Runtime/AsyncWebRequest.cs.meta new file mode 100644 index 0000000..18a8d09 --- /dev/null +++ b/Packages/Network/Runtime/AsyncWebRequest.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: e7a92b3660a18a24b81b91fbaf67778b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/Network/Runtime/AsyncWebRequestType.cs b/Packages/Network/Runtime/AsyncWebRequestType.cs new file mode 100644 index 0000000..6b46b81 --- /dev/null +++ b/Packages/Network/Runtime/AsyncWebRequestType.cs @@ -0,0 +1,19 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace MuHua { + /// + /// Web请求类型 + /// + public enum AsyncWebRequestType { + /// GET + GET = 0, + /// POST 表单 + PostForm = 1, + /// POST Json + PostJson = 2, + /// GET 获取图片 + Texture = 3 + } +} \ No newline at end of file diff --git a/Packages/Network/Runtime/AsyncWebRequestType.cs.meta b/Packages/Network/Runtime/AsyncWebRequestType.cs.meta new file mode 100644 index 0000000..3ea33b8 --- /dev/null +++ b/Packages/Network/Runtime/AsyncWebRequestType.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 8e945331334b0b6479a782aba1d68391 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/Network/Runtime/DataRequest.cs b/Packages/Network/Runtime/DataRequest.cs new file mode 100644 index 0000000..17aa05f --- /dev/null +++ b/Packages/Network/Runtime/DataRequest.cs @@ -0,0 +1,23 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.Networking; + +namespace MuHua { + /// + /// 请求数据 + /// + public abstract class DataRequest { + /// Web请求地址 + public abstract string Url { get; } + /// Web请求类型 + public abstract AsyncWebRequestType RequestType { get; } + /// 提交json数据 + public virtual string Json { get; } + /// 提交Form表单数据 + public virtual WWWForm Form { get; } + + /// Web请求结果处理 + public abstract void RequestResultHandle(bool isDone, UnityWebRequest web); + } +} \ No newline at end of file diff --git a/Packages/Network/Runtime/DataRequest.cs.meta b/Packages/Network/Runtime/DataRequest.cs.meta new file mode 100644 index 0000000..2394da7 --- /dev/null +++ b/Packages/Network/Runtime/DataRequest.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: a81036c14ed55ea4aa3beea60ebaa871 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/Network/Runtime/DataRequestGet.cs b/Packages/Network/Runtime/DataRequestGet.cs new file mode 100644 index 0000000..bbb3882 --- /dev/null +++ b/Packages/Network/Runtime/DataRequestGet.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.Networking; + +namespace MuHua { + /// + /// Get请求数据 + /// + public class DataRequestGet : DataRequest { + public readonly string url; + + public Action OnError; + public Action OnCallback; + + public override string Url => url; + public override AsyncWebRequestType RequestType => AsyncWebRequestType.GET; + + /// Web Get请求数据 + public DataRequestGet(string url, Action 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); + } + } +} diff --git a/Packages/Network/Runtime/DataRequestGet.cs.meta b/Packages/Network/Runtime/DataRequestGet.cs.meta new file mode 100644 index 0000000..cab4418 --- /dev/null +++ b/Packages/Network/Runtime/DataRequestGet.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: cf1da230bb48a6a4aa51686ff32e9f16 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/Network/Runtime/DataRequestPost.cs b/Packages/Network/Runtime/DataRequestPost.cs new file mode 100644 index 0000000..86bd76c --- /dev/null +++ b/Packages/Network/Runtime/DataRequestPost.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.Networking; + +namespace MuHua { + /// + /// Post请求数据 + /// + public class DataRequestPost : DataRequest { + public readonly string url; + public readonly string json; + public readonly WWWForm form; + public readonly AsyncWebRequestType type; + + public Action OnError; + public Action OnCallback; + + public override string Url => url; + public override AsyncWebRequestType RequestType => type; + public override string Json => json; + public override WWWForm Form => form; + + /// Web Post请求 提交json数据 + public DataRequestPost(string url, string json, Action OnCallback = null) { + this.url = url; + this.json = json; + this.OnCallback = OnCallback; + type = AsyncWebRequestType.PostJson; + } + /// Web Post请求 提交WWWForm数据 + public DataRequestPost(string url, WWWForm form, Action 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); + } + } +} \ No newline at end of file diff --git a/Packages/Network/Runtime/DataRequestPost.cs.meta b/Packages/Network/Runtime/DataRequestPost.cs.meta new file mode 100644 index 0000000..3cf64cb --- /dev/null +++ b/Packages/Network/Runtime/DataRequestPost.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: f43bea5409c953347974214f4eb5320c +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/Network/Runtime/DataRequestTexture.cs b/Packages/Network/Runtime/DataRequestTexture.cs new file mode 100644 index 0000000..0702597 --- /dev/null +++ b/Packages/Network/Runtime/DataRequestTexture.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.Networking; + +namespace MuHua { + /// + /// Get下载 Texture + /// + public class DataRequestTexture : DataRequest { + public readonly string url; + + public Action OnError; + public Action OnCallback; + + public override string Url => url; + public override AsyncWebRequestType RequestType => AsyncWebRequestType.Texture; + + /// Web Get请求 Texture + public DataRequestTexture(string url, Action 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); + } + } +} \ No newline at end of file diff --git a/Packages/Network/Runtime/DataRequestTexture.cs.meta b/Packages/Network/Runtime/DataRequestTexture.cs.meta new file mode 100644 index 0000000..9694841 --- /dev/null +++ b/Packages/Network/Runtime/DataRequestTexture.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: b0407bff8565543469def55f9788ecf8 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/Network/Runtime/MuHua.Network.asmdef b/Packages/Network/Runtime/MuHua.Network.asmdef new file mode 100644 index 0000000..e5ac627 --- /dev/null +++ b/Packages/Network/Runtime/MuHua.Network.asmdef @@ -0,0 +1,3 @@ +{ + "name": "MuHua.Network" +} diff --git a/Packages/Network/Runtime/MuHua.Network.asmdef.meta b/Packages/Network/Runtime/MuHua.Network.asmdef.meta new file mode 100644 index 0000000..d7d2588 --- /dev/null +++ b/Packages/Network/Runtime/MuHua.Network.asmdef.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: c91b8ea8cc52e094d8b1e2506189cb24 +AssemblyDefinitionImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/Network/package.json b/Packages/Network/package.json new file mode 100644 index 0000000..d562a24 --- /dev/null +++ b/Packages/Network/package.json @@ -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" + } + ] +} \ No newline at end of file diff --git a/Packages/Network/package.json.meta b/Packages/Network/package.json.meta new file mode 100644 index 0000000..07b9f77 --- /dev/null +++ b/Packages/Network/package.json.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 79e879bafe8148846a79f11c726aab21 +PackageManifestImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/UITool/Runtime/Frame.meta b/Packages/UITool/Runtime/Frame.meta new file mode 100644 index 0000000..53b065b --- /dev/null +++ b/Packages/UITool/Runtime/Frame.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 0fec0c5043434164c8a5cb28f3886b1d +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/UITool/Runtime/Frame/UIContainer.cs b/Packages/UITool/Runtime/Frame/UIContainer.cs new file mode 100644 index 0000000..2d06582 --- /dev/null +++ b/Packages/UITool/Runtime/Frame/UIContainer.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.UIElements; + +namespace MuHua { + /// + /// UI容器 + /// + public class UIContainer where Item : UIItem { + public readonly VisualElement container; + public readonly VisualTreeAsset templateAsset; + public readonly Func generate; + public List uiItems = new List(); + /// UI容器 + public UIContainer(VisualElement container, VisualTreeAsset templateAsset, Func generate) { + this.container = container; + this.templateAsset = templateAsset; + this.generate = generate; + } + /// 释放资源 + public void Release() { + container.Clear(); + uiItems.ForEach(obj => obj.Release()); + uiItems = new List(); + } + /// 创建UI项 + public void Create(List datas) { + Release(); + datas.ForEach(Create); + } + /// 创建UI项 + public void Create(Data data) { + VisualElement element = templateAsset.Instantiate(); + Item item = generate(data, element); + container.Add(item.element); + uiItems.Add(item); + } + } +} \ No newline at end of file diff --git a/Packages/UITool/Runtime/Frame/UIContainer.cs.meta b/Packages/UITool/Runtime/Frame/UIContainer.cs.meta new file mode 100644 index 0000000..4858966 --- /dev/null +++ b/Packages/UITool/Runtime/Frame/UIContainer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: b5854bcf80117aa49b327f2c19914ba3 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/UITool/Runtime/Frame/UIItem.cs b/Packages/UITool/Runtime/Frame/UIItem.cs new file mode 100644 index 0000000..e9774a4 --- /dev/null +++ b/Packages/UITool/Runtime/Frame/UIItem.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.UIElements; + +namespace MuHua { + /// + /// UI项 + /// + public abstract class UIItem { + /// 选择事件 + public static event Action OnSelect; + /// 触发事件 + public static void Select(Data data) => OnSelect?.Invoke(data); + + /// 绑定的数据 + public readonly Data value; + /// 绑定的元素 + public readonly VisualElement element; + /// UI项 + public UIItem(Data value, VisualElement element) { + this.value = value; + this.element = element; + OnSelect += UIItem_OnSelect; + } + + /// 侦听选择事件 + public virtual void UIItem_OnSelect(Data obj) { + if (value.Equals(obj)) { SelectState(); } else { DefaultState(); } + } + + /// 触发选择事件 + public virtual void Select() => OnSelect?.Invoke(value); + /// 默认状态 + public virtual void DefaultState() { } + /// 选中状态 + public virtual void SelectState() { } + /// 释放 + public virtual void Release() => OnSelect -= UIItem_OnSelect; + } +} \ No newline at end of file diff --git a/Packages/UITool/Runtime/Frame/UIItem.cs.meta b/Packages/UITool/Runtime/Frame/UIItem.cs.meta new file mode 100644 index 0000000..c1135db --- /dev/null +++ b/Packages/UITool/Runtime/Frame/UIItem.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: dfa6a8682a5ec384d98b88b54ace505f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/packages-lock.json b/Packages/packages-lock.json index c39854c..b1b9471 100644 --- a/Packages/packages-lock.json +++ b/Packages/packages-lock.json @@ -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,