diff --git a/Assets/Scenes/MainScene.unity b/Assets/Scenes/MainScene.unity index 006e6cc..2c30f77 100644 --- a/Assets/Scenes/MainScene.unity +++ b/Assets/Scenes/MainScene.unity @@ -484,7 +484,10 @@ MonoBehaviour: serverBaseUrls: - https://nextreal.cn/photon-matrix-api - https://api.photonmatrixlab.com - serverBaseUrl: https://nextreal.cn/photon-matrix-api + serverBaseUrl: https://api.photonmatrixlab.com + enableServerBackdoor: 1 + backdoorTapCount: 15 + backdoorTapInterval: 1 defaultTimeout: 30 defaultMaxRetries: 3 useExponentialBackoff: 1 diff --git a/Assets/Scripts/Network/NetworkCtrl.cs b/Assets/Scripts/Network/NetworkCtrl.cs index c19e7b4..0f23099 100644 --- a/Assets/Scripts/Network/NetworkCtrl.cs +++ b/Assets/Scripts/Network/NetworkCtrl.cs @@ -30,10 +30,21 @@ namespace Kill.Network public string streamingPath; #region 网络配置区域 - 直接修改以下配置 + /// + /// 0测试服务器 1正式服务器 + /// public string[] serverBaseUrls; [Header("服务器配置")] [Tooltip("服务器基础URL,例如: https://api.example.com")] - public string serverBaseUrl = "https://api.example.com"; + public string serverBaseUrl; + + [Header("服务器后门(测试/正式切换)")] + [Tooltip("是否启用服务器切换后门")] + public bool enableServerBackdoor = true; + [Tooltip("后门触发:在屏幕左下角区域连续点击次数")] + public int backdoorTapCount = 5; + [Tooltip("后门触发:两次点击间隔超过该秒数则重新计数")] + public float backdoorTapInterval = 2f; [Header("请求配置")] [Tooltip("默认请求超时时间(秒)")] @@ -63,11 +74,106 @@ namespace Kill.Network public string BaseServerUrl { get; set; } = ""; + #region 服务器环境(测试/正式)切换 + + /// + /// 服务器环境持久化Key:0=测试服务器 1=正式服务器(默认) + /// + private const string ServerEnvKey = "network_server_env"; + public const int EnvOfficial = 1; + public const int EnvTest = 0; + + /// + /// 当前服务器环境:0测试 1正式 + /// + public int CurrentServerEnv { get; private set; } = EnvOfficial; + + /// + /// 正式服务器地址(优先级: serverBaseUrls[1] > Inspector配置的serverBaseUrl) + /// + private string officialServerUrl; + + /// + /// 测试服务器地址(优先级: serverBaseUrls[0]) + /// + private string TestServerUrl + { + get + { + if (serverBaseUrls != null && serverBaseUrls.Length > 0 && !string.IsNullOrEmpty(serverBaseUrls[0])) + return serverBaseUrls[0]; + return serverBaseUrl; + } + } + + private int backdoorTapCountCurrent; + private float lastBackdoorTapTime; + + /// + /// 读取本地持久化的服务器环境并应用到 serverBaseUrl + /// + private void ApplySavedServerEnv() + { + officialServerUrl = (serverBaseUrls != null && serverBaseUrls.Length > 1 && !string.IsNullOrEmpty(serverBaseUrls[1])) + ? serverBaseUrls[1] + : serverBaseUrl; + + int savedEnv = PlayerPrefs.GetInt(ServerEnvKey, EnvOfficial); + CurrentServerEnv = savedEnv == EnvTest ? EnvTest : EnvOfficial; + if (CurrentServerEnv == EnvTest && !string.IsNullOrEmpty(TestServerUrl)) + { + serverBaseUrl = TestServerUrl; + } + Debug.Log($"[NetworkCtrl] 服务器环境: {(CurrentServerEnv == EnvTest ? "测试" : "正式")} - {serverBaseUrl}"); + } + + /// + /// 切换服务器环境(测试/正式),持久化到本地,重启App后保持 + /// + public void ToggleServerEnvironment() + { + if (CurrentServerEnv == EnvTest) + SwitchToOfficial(); + else + SwitchToTest(); + } + + private void SwitchToTest() + { + if (string.IsNullOrEmpty(TestServerUrl)) + { + Debug.LogWarning("[NetworkCtrl] 未配置测试服务器地址,无法切换"); + return; + } + CurrentServerEnv = EnvTest; + PlayerPrefs.SetInt(ServerEnvKey, EnvTest); + PlayerPrefs.Save(); + serverBaseUrl = TestServerUrl; + SetServerUrl(serverBaseUrl); + Debug.Log($"[NetworkCtrl] 已切换到测试服务器: {serverBaseUrl}"); + ToastUI.ShowText("已切换到测试服务器"); + } + + private void SwitchToOfficial() + { + CurrentServerEnv = EnvOfficial; + PlayerPrefs.SetInt(ServerEnvKey, EnvOfficial); + PlayerPrefs.Save(); + serverBaseUrl = officialServerUrl; + SetServerUrl(serverBaseUrl); + Debug.Log($"[NetworkCtrl] 已切换到正式服务器: {serverBaseUrl}"); + ToastUI.ShowText("已切换到正式服务器"); + } + + #endregion + void Awake() { Instance = this; localPath = Application.persistentDataPath; streamingPath = Application.streamingAssetsPath; + // 应用持久化的服务器环境(测试/正式),默认正式 + ApplySavedServerEnv(); // 设置目标帧率为 60 FPS Application.targetFrameRate = 120; @@ -91,6 +197,51 @@ namespace Kill.Network await Init(); } + void Update() + { + if (!enableServerBackdoor) return; + + if (!IsTapPressed()) return; + + Vector2 tapPos = GetTapPosition(); + // 后门触发区域:屏幕左下角 10% 区域 + if (tapPos.x > Screen.width * 0.1f || tapPos.y > Screen.height * 0.1f) + return; + + float now = Time.unscaledTime; + if (now - lastBackdoorTapTime > backdoorTapInterval) + { + backdoorTapCountCurrent = 0; + } + lastBackdoorTapTime = now; + backdoorTapCountCurrent++; + + if (backdoorTapCountCurrent >= backdoorTapCount) + { + backdoorTapCountCurrent = 0; + Debug.Log("[NetworkCtrl] 触发服务器切换后门"); + ToggleServerEnvironment(); + } + } + + private bool IsTapPressed() + { +#if UNITY_EDITOR || UNITY_STANDALONE || UNITY_WEBGL + return Input.GetMouseButtonDown(0); +#else + return Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began; +#endif + } + + private Vector2 GetTapPosition() + { +#if UNITY_EDITOR || UNITY_STANDALONE || UNITY_WEBGL + return Input.mousePosition; +#else + return Input.GetTouch(0).position; +#endif + } + public async Task Init() { Debug.Log("初始化网络控制");