This commit is contained in:
2026-08-04 18:17:09 +08:00
parent db93954abd
commit 3359483dfe
2 changed files with 97 additions and 63 deletions
@@ -818,6 +818,7 @@ public class GameMainPanel_FishBase : GameMainPanel_Base
/// <summary>
/// 离房卸载流程:进度条到 0.9 后等待 socket 响应,
/// 收到信号后走到 1.0 并切回 home 界面。
/// ⭐ 用 try-finally 保证无论中间是否异常,最终一定会关闭 GamePanel 并切回 Home
/// </summary>
private async void BeginLeaveRoomUnload()
{
@@ -827,64 +828,91 @@ public class GameMainPanel_FishBase : GameMainPanel_Base
return;
}
JinDuTiao.Show();
JinDuTiao.SetProgress(0.2f);
await FishResourcesData.UnLoadFishAssetsData();
JinDuTiao.SetProgress(0.4f);
await AudioResourcesData.UnLoadAudioGame();
JinDuTiao.SetProgress(0.6f);
await MonsterMoveLine.UnLoadBezier();
JinDuTiao.SetProgress(0.8f);
await Task.Delay(100);
CreateFishManager.Instance.ExitRoom();
CreateFishManager.Instance.isResources = false;
// ⭐ 清空子弹池 + 特效池(鱼池已在 UnLoadFishAssetsData 内清理)
BulletPool.Instance.ClearAll();
EffectPool.Instance.ClearAll();
JinDuTiao.SetProgress(0.9f);
// ⭐ 卡在 0.9 等待 socket 返回 LeaveRoomResult
// - socket 已早返回(比卸载快) → 在 0.9 再等 1 秒(美观)后变 1
// - socket 未返回 → 等待 socket,最多 10 秒,超时强制变 1 退出
if (_leaveRoomResponseTcs != null)
try
{
bool alreadyReceived = _leaveRoomResponseTcs.Task.IsCompleted;
if (alreadyReceived)
if (JinDuTiao != null) JinDuTiao.Show();
if (JinDuTiao != null) JinDuTiao.SetProgress(0.2f);
await FishResourcesData.UnLoadFishAssetsData();
if (JinDuTiao != null) JinDuTiao.SetProgress(0.4f);
await AudioResourcesData.UnLoadAudioGame();
if (JinDuTiao != null) JinDuTiao.SetProgress(0.6f);
await MonsterMoveLine.UnLoadBezier();
if (JinDuTiao != null) JinDuTiao.SetProgress(0.8f);
await Task.Delay(100);
CreateFishManager.Instance.ExitRoom();
CreateFishManager.Instance.isResources = false;
// ⭐ 清空子弹池 + 特效池(鱼池已在 UnLoadFishAssetsData 内清理)
BulletPool.Instance.ClearAll();
EffectPool.Instance.ClearAll();
if (JinDuTiao != null) JinDuTiao.SetProgress(0.9f);
// ⭐ 卡在 0.9 等待 socket 返回 LeaveRoomResult
// - socket 已早返回(比卸载快) → 在 0.9 再等 1 秒(美观)后变 1
// - socket 未返回 → 等待 socket,最多 10 秒,超时强制变 1 退出
if (_leaveRoomResponseTcs != null)
{
// socket 比卸载早返回 → 0.9 停留 1 秒
await Task.Delay(1000);
}
else
{
// socket 未返回 → 等待,最多 10 秒
var timeoutTask = Task.Delay(10000);
var done = await Task.WhenAny(_leaveRoomResponseTcs.Task, timeoutTask);
if (done == timeoutTask)
bool alreadyReceived = _leaveRoomResponseTcs.Task.IsCompleted;
if (alreadyReceived)
{
// 超时 10 秒 → 强制退出,不管结算是否返回
#if UNITY_EDITOR
Debug.LogWarning("[LeaveRoom] 等待 socket 超时 10 秒,强制退出");
#endif
// socket 比卸载早返回 → 0.9 停留 1 秒
await Task.Delay(1000);
}
else
{
// socket 未返回 → 等待,最多 10 秒
var timeoutTask = Task.Delay(10000);
var done = await Task.WhenAny(_leaveRoomResponseTcs.Task, timeoutTask);
if (done == timeoutTask)
{
// 超时 10 秒 → 强制退出,必须执行和 ExitFishingGame 一致的清理
// 否则 game socket 不关闭、isGame 不复位,导致无法再次进入游戏
Debug.LogWarning("[LeaveRoom] 等待 socket 超时 10 秒,强制退出并清理游戏状态");
try
{
WebSocketManager.Instance.isGame = false;
GameManager.LogGoldFlow("ExitRoom", $"超时强制退出 房间={GameManager.Instance.gameData?.currRoomData?.roomId}");
WebSocketManager.Instance.Close(SocketType.game);
APIRequestList.GetUser((user) =>
{
GameManager.Instance.UpdataUserData(user);
});
}
catch (Exception cleanEx)
{
Debug.LogError($"[LeaveRoom] 超时清理异常: {cleanEx}");
}
}
}
}
// 标记已卸载,OnHide 跳过重复卸载
_resourcesUnloaded = true;
if (JinDuTiao != null) JinDuTiao.SetProgress(1);
await Task.Delay(300);
if (JinDuTiao != null) JinDuTiao.Hide();
}
catch (Exception ex)
{
Debug.LogError($"[LeaveRoom] 卸载流程异常: {ex}");
}
finally
{
// ⭐ 无论中间是否异常,都必须关闭 GamePanel 并切回 Home
// 否则 GamePanel 留在 panels 字典里,下次进入游戏时 Get 返回非 null 导致无法创建新面板
_leaveRoomResponseTcs = null;
if (JinDuTiao != null)
{
try { JinDuTiao.Hide(); } catch { }
}
// 进入 home 界面(关闭当前游戏面板)
UIManager.Instance.ShowAsync(UIPanel.HomeMainPanel, _leavingGamePanelType);
}
// 标记已卸载,OnHide 跳过重复卸载
_resourcesUnloaded = true;
JinDuTiao.SetProgress(1);
await Task.Delay(300);
JinDuTiao.Hide();
// 进入 home 界面(关闭当前游戏面板)
UIManager.Instance.ShowAsync(UIPanel.HomeMainPanel, _leavingGamePanelType);
_leaveRoomResponseTcs = null;
}
/// <summary>
@@ -4,6 +4,7 @@ using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.Serialization;
using UnityEngine.UI;
public class UIHome_BY_SettingPanel : SubUIBase
@@ -20,8 +21,10 @@ public class UIHome_BY_SettingPanel : SubUIBase
[Header("帧率")]
public Toggle toggle_Fps30;
[FormerlySerializedAs("toggle_Fps60")]
public Toggle toggle_Fps45;
[FormerlySerializedAs("toggle_FpsUnlimited")]
public Toggle toggle_Fps60;
public Toggle toggle_FpsUnlimited;
public ToggleGroup group_Fps;
private Dictionary<Toggle, Color> labelOriginalColors = new Dictionary<Toggle, Color>();
@@ -73,13 +76,13 @@ public class UIHome_BY_SettingPanel : SubUIBase
// 帧率
CacheToggleColor(toggle_Fps30);
CacheToggleColor(toggle_Fps45);
CacheToggleColor(toggle_Fps60);
CacheToggleColor(toggle_FpsUnlimited);
int fps = PlayerPrefs.GetInt("TargetFps", 1);
int fps = PlayerPrefs.GetInt("TargetFps", 2);
ApplyFps(fps);
toggle_Fps30?.onValueChanged.AddListener(a => { if (a) SetFps(0); });
toggle_Fps60?.onValueChanged.AddListener(a => { if (a) SetFps(1); });
toggle_FpsUnlimited?.onValueChanged.AddListener(a => { if (a) SetFps(2); });
toggle_Fps45?.onValueChanged.AddListener(a => { if (a) SetFps(1); });
toggle_Fps60?.onValueChanged.AddListener(a => { if (a) SetFps(2); });
}
protected override void OnShow(object args = null)
{
@@ -130,16 +133,19 @@ public class UIHome_BY_SettingPanel : SubUIBase
private void ApplyFps(int level)
{
if (toggle_Fps30 != null) toggle_Fps30.isOn = (level == 0);
if (toggle_Fps60 != null) toggle_Fps60.isOn = (level == 1);
if (toggle_FpsUnlimited != null) toggle_FpsUnlimited.isOn = (level == 2);
if (toggle_Fps45 != null) toggle_Fps45.isOn = (level == 1);
if (toggle_Fps60 != null) toggle_Fps60.isOn = (level == 2);
UpdateToggleColor(toggle_Fps30, level == 0);
UpdateToggleColor(toggle_Fps60, level == 1);
UpdateToggleColor(toggle_FpsUnlimited, level == 2);
UpdateToggleColor(toggle_Fps45, level == 1);
UpdateToggleColor(toggle_Fps60, level == 2);
// 关键:vSyncCount != 0 会让 Application.targetFrameRate 失效(被屏幕刷新率覆盖)。
// Balanced/High Fidelity 画质默认 vSyncCount=1,必须强制关闭才能让帧率档位生效。
QualitySettings.vSyncCount = 0;
switch (level)
{
case 0: Application.targetFrameRate = 30; break;
case 1: Application.targetFrameRate = 60; break;
case 2: Application.targetFrameRate = -1; break;
case 1: Application.targetFrameRate = 45; break;
case 2: Application.targetFrameRate = 60; break;
}
}
}