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();

View File

@@ -287,7 +287,13 @@ namespace Hallucinate.UI
if (_joinContainer != null) _joinContainer.style.display = DisplayStyle.Flex;
if (_createContainer != null) _createContainer.style.display = DisplayStyle.None;
if (_loungeContainer != null) _loungeContainer.style.display = DisplayStyle.None;
_ = BasicSpawner.Instance?.StartLobby();
// Chỉ bắt đầu Lobby nếu chưa có session nào đang chạy
var runner = BasicSpawner.Instance?.Runner;
if (runner == null || !runner.IsRunning)
{
_ = BasicSpawner.Instance?.StartLobby();
}
}
public void ShowCreate()
@@ -338,9 +344,6 @@ namespace Hallucinate.UI
if (success)
{
ShowLounge(name);
// Explicitly push the LobbyController to ensure it's the active UI screen.
// This helps prevent unintended navigation away from the lounge.
await uiManager.Push<LobbyController>();
}
}

View File

@@ -21,7 +21,7 @@ MonoBehaviour:
m_ScaleMode: 1
m_ReferenceSpritePixelsPerUnit: 100
m_PixelsPerUnit: 100
m_Scale: 1
m_Scale: 1.8199999
m_ReferenceDpi: 96
m_FallbackDpi: 96
m_ReferenceResolution: {x: 1200, y: 800}