From 0fa617a09eedf6bdb25eda55fac1d3344859fd93 Mon Sep 17 00:00:00 2001
From: yyl <yyl>
Date: 星期二, 31 三月 2026 19:46:31 +0800
Subject: [PATCH] webgl

---
 Main/Utility/DeviceUtility.cs |   41 +++++++++++++++++++++--------------------
 1 files changed, 21 insertions(+), 20 deletions(-)

diff --git a/Main/Utility/DeviceUtility.cs b/Main/Utility/DeviceUtility.cs
index 86ad894..e9d41e7 100644
--- a/Main/Utility/DeviceUtility.cs
+++ b/Main/Utility/DeviceUtility.cs
@@ -1,13 +1,16 @@
 锘縰sing System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
-using System.Net.NetworkInformation;
 using System;
-using System.Net;
-using System.Net.Sockets;
-using System.IO;
 using System.Text.RegularExpressions;
 using Cysharp.Threading.Tasks;
+using UnityEngine.Networking;
+
+#if !UNITY_WEBGL
+using System.Net.NetworkInformation;
+using System.Net;
+using System.IO;
+#endif
 
 #if UNITY_IOS
 using UnityEngine.iOS;
@@ -20,6 +23,7 @@
     static string mac = string.Empty;
     public static string GetMac()
     {
+#if !UNITY_WEBGL
         if (string.IsNullOrEmpty(mac))
         {
             try
@@ -35,12 +39,13 @@
                 Debug.Log(ex);
             }
         }
-
+#endif
         return mac;
     }
 
     static string ipPattern = "[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}";
     static string ip = string.Empty;
+    public static string GetCachedIp() => ip;
     public static async UniTask<string> GetIp()
     {
         try
@@ -48,24 +53,20 @@
             if (string.IsNullOrEmpty(ip))
             {
                 string url = "http://pv.sohu.com/cityjson";
-                WebRequest wRequest = WebRequest.Create(url);
-                wRequest.Method = "GET";
-                wRequest.ContentType = "text/html;charset=UTF-8";
-                WebResponse wResponse = await wRequest.GetResponseAsync();
-                Stream stream = wResponse.GetResponseStream();
-                StreamReader reader = new StreamReader(stream, System.Text.Encoding.Default);
-                string str = await reader.ReadToEndAsync();
-
-                reader.Close();
-                wResponse.Close();
-
-                var match = Regex.Match(str, ipPattern);
-                if (match != null)
+                using (var request = UnityWebRequest.Get(url))
                 {
-                    ip = match.Value;
+                    await request.SendWebRequest();
+                    if (request.result == UnityWebRequest.Result.Success)
+                    {
+                        string str = request.downloadHandler.text;
+                        var match = Regex.Match(str, ipPattern);
+                        if (match != null)
+                        {
+                            ip = match.Value;
+                        }
+                    }
                 }
             }
-
         }
         catch (Exception ex)
         {

--
Gitblit v1.8.0