From 6e73118bd96e7e199b437cab530f0e01a9ed124e Mon Sep 17 00:00:00 2001 From: liyang <390792612@qq.com> Date: Wed, 11 Feb 2026 15:49:23 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9HttpServer=EF=BC=8C=E8=BF=9B?= =?UTF-8?q?=E5=85=A5=E9=80=9F=E5=BA=A6=E5=8A=A0=E5=BF=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/_Zombie/Scripts/HttpServer.cs | 25 +++++++++++++++-------- ProjectSettings/EditorBuildSettings.asset | 4 ++-- ProjectSettings/ProjectSettings.asset | 2 +- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/Assets/_Zombie/Scripts/HttpServer.cs b/Assets/_Zombie/Scripts/HttpServer.cs index 68b65271..b6090a1f 100644 --- a/Assets/_Zombie/Scripts/HttpServer.cs +++ b/Assets/_Zombie/Scripts/HttpServer.cs @@ -6,6 +6,7 @@ using System.Threading; using System.Collections.Concurrent; using UnityEngine; using Valheim; +using System.Threading.Tasks; [Serializable] public class IntentMessage @@ -42,12 +43,13 @@ public class HttpServer : MonoBehaviour void Awake() { - DontDestroyOnLoad(gameObject); + Task.Run(StartServer); + } void Start() { - StartServer(); + } #region HTTP Server @@ -57,15 +59,11 @@ public class HttpServer : MonoBehaviour try { listener = new HttpListener(); - listener.Prefixes.Add(SERVER_URL); + listener.Prefixes.Add($"http://{GetLocalIP()}:12345/"); listener.Start(); isRunning = true; - serverThread = new Thread(ListenLoop) - { - IsBackground = true - }; - serverThread.Start(); + ListenLoop(); Debug.Log($"✅ HTTP Server 启动成功:{SERVER_URL}"); } @@ -75,6 +73,17 @@ public class HttpServer : MonoBehaviour } } + private string GetLocalIP() + { + var host = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()); + foreach (var ip in host.AddressList) + { + if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) + return ip.ToString(); + } + return "127.0.0.1"; + } + private void ListenLoop() { while (isRunning && listener.IsListening) diff --git a/ProjectSettings/EditorBuildSettings.asset b/ProjectSettings/EditorBuildSettings.asset index b99c3d1d..5110d803 100644 --- a/ProjectSettings/EditorBuildSettings.asset +++ b/ProjectSettings/EditorBuildSettings.asset @@ -227,7 +227,7 @@ EditorBuildSettings: - enabled: 0 path: Assets/_Zombie/Scenes/Liaoning_Panjin_Xinglongtai_Shuiyoucheng.unity guid: 796a76765ad2af047b0a8bc7fd6cfbb9 - - enabled: 0 + - enabled: 1 path: Assets/_Zombie/Scenes/Shanxi_Yulin_Shenmu_Liuta_Yuyuecheng.unity guid: 79ef7bd94ba23834d900bf99028ce951 - enabled: 0 @@ -245,7 +245,7 @@ EditorBuildSettings: - enabled: 0 path: Assets/_Zombie/Scenes/Ningxia_Yinchuan_Xingqing_XinhuaBaihuo_Shiwai.unity guid: 8d49e8d3356159249b0895ad9486ede4 - - enabled: 1 + - enabled: 0 path: Assets/_Zombie/Scenes/Ningxia_Wuzhong_Litong_YiwuShangmaocheng.unity guid: fbfd3e21f4f625f439981c19a7d8b822 m_configObjects: diff --git a/ProjectSettings/ProjectSettings.asset b/ProjectSettings/ProjectSettings.asset index 5fc5da00..4db46709 100644 --- a/ProjectSettings/ProjectSettings.asset +++ b/ProjectSettings/ProjectSettings.asset @@ -163,7 +163,7 @@ PlayerSettings: androidSupportedAspectRatio: 1 androidMaxAspectRatio: 2.1 applicationIdentifier: - Android: com.pineappletech.zombie.ningxiawuzhonglitongyiwushangmaocheng + Android: com.pineappletech.zombie.shanxiyulinshenmuliutayuyuecheng Standalone: com.DefaultCompany.com.unity.template.ar buildNumber: Standalone: 0