From 25eb0e50d4e815efb16d1a9953beac6ea1c7cfc3 Mon Sep 17 00:00:00 2001
From: yyl <yyl>
Date: 星期六, 28 三月 2026 10:14:32 +0800
Subject: [PATCH] webgl merge
---
Assets/Launch/HttpRequest/HttpBehaviour.cs | 50 +++++++++++++++++++++++++++++++++++++++++++++++++-
1 files changed, 49 insertions(+), 1 deletions(-)
diff --git a/Assets/Launch/HttpRequest/HttpBehaviour.cs b/Assets/Launch/HttpRequest/HttpBehaviour.cs
index 18bc6c9..45f11d9 100644
--- a/Assets/Launch/HttpRequest/HttpBehaviour.cs
+++ b/Assets/Launch/HttpRequest/HttpBehaviour.cs
@@ -1,15 +1,17 @@
锘縰sing System.Collections;
using System.Collections.Generic;
using UnityEngine;
+using UnityEngine.Networking;
using System;
using System.Net;
using System.Text;
using System.IO;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
+using Cysharp.Threading.Tasks;
- public class HttpBehaviour : MonoBehaviour
+public class HttpBehaviour : MonoBehaviour
{
string url;
@@ -41,6 +43,52 @@
behaviour.Begin(_url, _method, _content, _contentType, _retry, _result);
}
+ /// <summary>
+ /// Create 鐨勫紓姝ョ増鏈紝浣跨敤 UnityWebRequest 瀹炵幇锛屾敮鎸佹墍鏈夊钩鍙帮紙鍚� WebGL锛夈��
+ /// </summary>
+ public static async UniTask<(bool ok, string message)> CreateAsync(string _url, string _method, string _content, string _contentType, int _retry = 3, Action<bool, string> _result = null)
+ {
+ bool ok = false;
+ string message = string.Empty;
+
+ for (int attempt = 0; attempt <= _retry; attempt++)
+ {
+ UnityWebRequest www;
+ if (_method == "POST")
+ {
+ var bodyRaw = Encoding.UTF8.GetBytes(_content);
+ www = new UnityWebRequest(_url, "POST");
+ www.uploadHandler = new UploadHandlerRaw(bodyRaw);
+ www.downloadHandler = new DownloadHandlerBuffer();
+ www.SetRequestHeader("Content-Type", _contentType);
+ }
+ else
+ {
+ www = UnityWebRequest.Get(_url);
+ www.SetRequestHeader("Content-Type", _contentType);
+ }
+
+ using (www)
+ {
+ await www.SendWebRequest();
+ ok = www.result == UnityWebRequest.Result.Success;
+ message = ok ? www.downloadHandler.text : www.error;
+ }
+
+ if (ok)
+ {
+ ConnectAllTimes = 0;
+ break;
+ }
+
+ ConnectAllTimes++;
+ Debug.LogWarning($"[HttpBehaviour] CreateAsync failed (attempt {attempt + 1}/{_retry + 1}): {message} url={_url}");
+ }
+
+ _result?.Invoke(ok, message);
+ return (ok, message);
+ }
+
void Begin(string _url, string _method, string _content, string _contentType, int _retry = 3, Action<bool, string> _result = null)
{
this.url = _url;
--
Gitblit v1.8.0