From 32fbb971e7fcc7de96d5515607b1a8d9622f4882 Mon Sep 17 00:00:00 2001
From: Alex-Rachel <574809918@qq.com>
Date: Sat, 8 Mar 2025 14:18:35 +0800
Subject: [PATCH] Utility.Http
---
.../Runtime/Core/Utility/Utility.Http.cs | 166 ++++++++++++++++++
.../Runtime/Core/Utility/Utility.Http.cs.meta | 3 +
2 files changed, 169 insertions(+)
create mode 100644 UnityProject/Assets/TEngine/Runtime/Core/Utility/Utility.Http.cs
create mode 100644 UnityProject/Assets/TEngine/Runtime/Core/Utility/Utility.Http.cs.meta
diff --git a/UnityProject/Assets/TEngine/Runtime/Core/Utility/Utility.Http.cs b/UnityProject/Assets/TEngine/Runtime/Core/Utility/Utility.Http.cs
new file mode 100644
index 00000000..a0023ce0
--- /dev/null
+++ b/UnityProject/Assets/TEngine/Runtime/Core/Utility/Utility.Http.cs
@@ -0,0 +1,166 @@
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+using UnityEngine.Networking;
+
+namespace TEngine
+{
+ public static partial class Utility
+ {
+ ///
+ /// 提供HTTP相关操作的实用工具类。
+ /// 封装了常用的GET/POST请求、文件下载、多媒体资源获取等功能。
+ ///
+ public static class Http
+ {
+ ///
+ /// 发起HTTP GET请求。
+ ///
+ /// 请求的目标URL。
+ /// 请求完成后的回调,接收UnityWebRequest对象用于处理结果。
+ public static void Get(string url, Action actionResult)
+ {
+ Unity.StartCoroutine(OnGet(url, actionResult));
+ }
+
+ ///
+ /// 下载文件到本地路径。
+ ///
+ /// 文件下载URL。
+ /// 文件保存路径(包含文件名)。
+ /// 下载完成后的回调,接收UnityWebRequest对象用于处理结果。
+ public static void DownloadFile(string url, string downloadFilePathAndName, Action actionResult)
+ {
+ Unity.StartCoroutine(OnDownloadFile(url, downloadFilePathAndName, actionResult));
+ }
+
+ ///
+ /// 获取网络纹理资源。
+ ///
+ /// 纹理资源URL。
+ /// 获取完成后的回调,成功时返回Texture2D对象,失败返回null。
+ public static void GetTexture(string url, Action actionResult)
+ {
+ Unity.StartCoroutine(OnGetTexture(url, actionResult));
+ }
+
+ ///
+ /// 获取网络音频资源。
+ ///
+ /// 音频资源URL。
+ /// 获取完成后的回调,成功时返回AudioClip对象。
+ /// 音频类型(默认WAV格式)。
+ public static void GetAudioClip(string url, Action actionResult, UnityEngine.AudioType audioType = UnityEngine.AudioType.WAV)
+ {
+ Unity.StartCoroutine(OnGetAudioClip(url, actionResult, audioType));
+ }
+
+ ///
+ /// 发起HTTP POST请求。
+ ///
+ /// 请求的目标URL。
+ /// POST表单数据列表。
+ /// 请求完成后的回调,接收UnityWebRequest对象用于处理结果。
+ public static void Post(string serverURL, List lstformData, Action actionResult)
+ {
+ Unity.StartCoroutine(OnPost(serverURL, lstformData, actionResult));
+ }
+
+ ///
+ /// 使用PUT方法上传二进制数据。
+ ///
+ /// 上传目标URL。
+ /// 要上传的二进制内容。
+ /// 上传完成后的回调,返回是否成功。
+ public static void UploadByPut(string url, byte[] contentBytes, Action actionResult)
+ {
+ Unity.StartCoroutine(OnUploadByPut(url, contentBytes, actionResult, ""));
+ }
+
+ #region Private Coroutine Implementations
+
+ ///
+ /// GET请求协程实现。
+ ///
+ private static IEnumerator OnGet(string url, Action actionResult)
+ {
+ using UnityWebRequest uwr = UnityWebRequest.Get(url);
+ yield return uwr.SendWebRequest();
+ actionResult?.Invoke(uwr);
+ }
+
+ ///
+ /// 文件下载协程实现。
+ ///
+ /// 使用DownloadHandlerFile直接保存到本地路径。
+ private static IEnumerator OnDownloadFile(string url, string downloadFilePathAndName, Action actionResult)
+ {
+ UnityWebRequest uwr = new UnityWebRequest(url, "GET");
+ uwr.downloadHandler = new DownloadHandlerFile(downloadFilePathAndName);
+ yield return uwr.SendWebRequest();
+ actionResult?.Invoke(uwr);
+ }
+
+ ///
+ /// 获取纹理协程实现。
+ ///
+ /// 使用DownloadHandlerTexture自动处理纹理数据。
+ private static IEnumerator OnGetTexture(string url, Action actionResult)
+ {
+ UnityWebRequest uwr = new UnityWebRequest(url);
+ DownloadHandlerTexture downloadTexture = new DownloadHandlerTexture(true);
+ uwr.downloadHandler = downloadTexture;
+ yield return uwr.SendWebRequest();
+ Texture2D texture2D = null;
+ if (uwr.result == UnityWebRequest.Result.Success)
+ {
+ texture2D = downloadTexture.texture;
+ }
+ actionResult?.Invoke(texture2D);
+ }
+
+ ///
+ /// 获取音频剪辑协程实现。
+ ///
+ /// 使用UnityWebRequestMultimedia处理音频流。
+ private static IEnumerator OnGetAudioClip(string url, Action actionResult, UnityEngine.AudioType audioType = UnityEngine.AudioType.WAV)
+ {
+ using UnityWebRequest uwr = UnityWebRequestMultimedia.GetAudioClip(url, audioType);
+ yield return uwr.SendWebRequest();
+ if (uwr.result == UnityWebRequest.Result.Success && actionResult != null)
+ {
+ actionResult(DownloadHandlerAudioClip.GetContent(uwr));
+ }
+ }
+
+ ///
+ /// POST请求协程实现。
+ ///
+ /// 支持多部分表单数据提交。
+ private static IEnumerator OnPost(string serverURL, List lstformData, Action actionResult)
+ {
+ UnityWebRequest uwr = UnityWebRequest.Post(serverURL, lstformData);
+ yield return uwr.SendWebRequest();
+ actionResult?.Invoke(uwr);
+ }
+
+ ///
+ /// PUT上传协程实现。
+ ///
+ /// 使用UploadHandlerRaw处理二进制数据上传。
+ private static IEnumerator OnUploadByPut(string url, byte[] contentBytes, Action actionResult, string contentType = "application/octet-stream")
+ {
+ UnityWebRequest uwr = new UnityWebRequest();
+ UploadHandler uploadHandler = new UploadHandlerRaw(contentBytes);
+ uploadHandler.contentType = contentType;
+ uwr.uploadHandler = uploadHandler;
+ yield return uwr.SendWebRequest();
+ bool flag = uwr.result == UnityWebRequest.Result.Success;
+ actionResult?.Invoke(flag);
+ }
+
+ #endregion
+ }
+ }
+}
diff --git a/UnityProject/Assets/TEngine/Runtime/Core/Utility/Utility.Http.cs.meta b/UnityProject/Assets/TEngine/Runtime/Core/Utility/Utility.Http.cs.meta
new file mode 100644
index 00000000..49950a8b
--- /dev/null
+++ b/UnityProject/Assets/TEngine/Runtime/Core/Utility/Utility.Http.cs.meta
@@ -0,0 +1,3 @@
+fileFormatVersion: 2
+guid: 8c1cf0217e4b49038d8f7686f28a613a
+timeCreated: 1741414112
\ No newline at end of file