This commit is contained in:
2026-05-01 18:10:32 +07:00
parent b0fab4bb6a
commit 3226eab649
4 changed files with 61 additions and 52 deletions

View File

@@ -16,6 +16,7 @@ namespace Hallucinate.UI
public NetworkRunner Runner => _runner;
private bool _isStarting = false;
private bool _isInternalShutdown = false;
public event Action<List<SessionInfo>> OnSessionListUpdatedEvent;
public event Action<string> OnShutdownEvent;
@@ -47,18 +48,25 @@ namespace Hallucinate.UI
{
if (_runner != null)
{
if (_runner.IsRunning)
_isInternalShutdown = true;
try
{
Debug.Log("[BasicSpawner] Shutting down existing runner before recreation.");
await _runner.Shutdown();
if (_runner.IsRunning)
{
Debug.Log("[BasicSpawner] Shutting down existing runner before recreation.");
await _runner.Shutdown();
}
Debug.Log("[BasicSpawner] Destroying existing runner component.");
Destroy(_runner);
_runner = null;
await Task.Yield();
}
finally
{
_isInternalShutdown = false;
}
Debug.Log("[BasicSpawner] Destroying existing runner component.");
Destroy(_runner);
_runner = null;
// Đợi 1 frame để đảm bảo component đã bị hủy thực sự
await Task.Yield();
}
Debug.Log("[BasicSpawner] Creating new NetworkRunner component.");
@@ -169,32 +177,42 @@ namespace Hallucinate.UI
public async Task<bool> StartClient(string sessionName, string password = null)
{
OnJoinStartedEvent?.Invoke();
await EnsureRunnerExists();
if (_isStarting) return false;
_isStarting = true;
var sceneManager = gameObject.GetComponent<NetworkSceneManagerDefault>();
if (sceneManager == null) sceneManager = gameObject.AddComponent<NetworkSceneManagerDefault>();
var result = await _runner.StartGame(new StartGameArgs()
try
{
GameMode = GameMode.Client,
SessionName = sessionName,
SceneManager = sceneManager
});
OnJoinStartedEvent?.Invoke();
await EnsureRunnerExists();
var sceneManager = gameObject.GetComponent<NetworkSceneManagerDefault>();
if (sceneManager == null) sceneManager = gameObject.AddComponent<NetworkSceneManagerDefault>();
if (result.Ok)
{
return true;
var result = await _runner.StartGame(new StartGameArgs()
{
GameMode = GameMode.Client,
SessionName = sessionName,
SceneManager = sceneManager
});
if (result.Ok)
{
return true;
}
else
{
Debug.LogError($"[BasicSpawner] Fusion StartClient Failed: {result.ShutdownReason}");
OnJoinFailedEvent?.Invoke();
return false;
}
}
else
finally
{
Debug.LogError($"Fusion StartClient Failed: {result.ShutdownReason}");
OnJoinFailedEvent?.Invoke();
return false;
_isStarting = false;
}
}
private Dictionary<PlayerRef, NetworkObject> _spawnedCharacters = new Dictionary<PlayerRef, NetworkObject>();
public void OnPlayerJoined(NetworkRunner runner, PlayerRef player)
@@ -267,6 +285,13 @@ namespace Hallucinate.UI
Debug.LogWarning($"[Fusion] Shutdown occurred. Reason: {shutdownReason}");
OnShutdownEvent?.Invoke(shutdownReason.ToString());
// Nếu shutdown là do hệ thống chủ động hủy để tạo runner mới, KHÔNG quay về Menu
if (_isInternalShutdown)
{
Debug.Log("[BasicSpawner] Internal shutdown detected, skipping Menu routing.");
return;
}
if (UIManager.Instance != null)
{
UIManager.Instance.OnBackToMenu();