feat(network): 添加服务器环境切换后门功能

1. 新增服务器后门触发逻辑,支持在屏幕左下角连续点击切换测试/正式服务器
2. 新增本地持久化服务器环境配置,重启应用后保持当前环境
3. 调整初始服务器地址为正式服务器地址,新增多服务器URL配置支持
4. 为网络配置添加详细注释和编辑器表头说明
This commit is contained in:
“虞渠成” 2026-08-05 13:30:32 +08:00
parent 9658ee4c0f
commit 954cb33e71
2 changed files with 156 additions and 2 deletions

View File

@ -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

View File

@ -30,10 +30,21 @@ namespace Kill.Network
public string streamingPath;
#region -
/// <summary>
/// 0测试服务器 1正式服务器
/// </summary>
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 (/)
/// <summary>
/// 服务器环境持久化Key0=测试服务器 1=正式服务器(默认)
/// </summary>
private const string ServerEnvKey = "network_server_env";
public const int EnvOfficial = 1;
public const int EnvTest = 0;
/// <summary>
/// 当前服务器环境0测试 1正式
/// </summary>
public int CurrentServerEnv { get; private set; } = EnvOfficial;
/// <summary>
/// 正式服务器地址(优先级: serverBaseUrls[1] > Inspector配置的serverBaseUrl)
/// </summary>
private string officialServerUrl;
/// <summary>
/// 测试服务器地址(优先级: serverBaseUrls[0])
/// </summary>
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;
/// <summary>
/// 读取本地持久化的服务器环境并应用到 serverBaseUrl
/// </summary>
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}");
}
/// <summary>
/// 切换服务器环境(测试/正式)持久化到本地重启App后保持
/// </summary>
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("初始化网络控制");