This commit is contained in:
2026-04-30 01:30:58 +07:00
parent a8d268c42b
commit 68dd336d00
3 changed files with 27 additions and 42 deletions

View File

@@ -38,19 +38,23 @@ namespace Hallucinate.UI
private async Task EnsureRunnerExists()
{
if (_runner != null)
if (_runner == null)
{
// Nếu runner cũ vẫn đang chạy hoặc lỗi, dọn dẹp nó
await _runner.Shutdown();
if (_runner != null && _runner.gameObject != null)
{
Destroy(_runner);
}
_runner = null;
_runner = GetComponent<NetworkRunner>();
}
if (_runner != null && _runner.IsRunning)
{
await _runner.Shutdown();
}
if (_runner == null)
{
_runner = gameObject.AddComponent<NetworkRunner>();
}
_runner = gameObject.AddComponent<NetworkRunner>();
_runner.ProvideInput = true;
_runner.RemoveCallbacks(this);
_runner.AddCallbacks(this);
}
@@ -186,12 +190,17 @@ namespace Hallucinate.UI
{
runner.Despawn(networkObject);
_spawnedCharacters.Remove(player);
if (runner.IsServer) runner.Shutdown();
// Chỉ Shutdown nếu người thoát chính là Server (Host)
if (runner.IsServer && player == runner.LocalPlayer)
{
runner.Shutdown();
}
}
}
public void OnShutdown(NetworkRunner runner, ShutdownReason shutdownReason)
{
Debug.LogWarning($"[Fusion] Shutdown occurred. Reason: {shutdownReason}");
OnShutdownEvent?.Invoke(shutdownReason.ToString());
}

View File

@@ -89,14 +89,14 @@ namespace Hallucinate.UI
});
}
// Đăng ký sự kiện từ Spawner (Sử dụng Instance)
// Đăng ký sự kiện từ Spawner
if (BasicSpawner.Instance != null)
{
RegisterSpawnerEvents();
_ = BasicSpawner.Instance.StartLobby();
}
else
{
// Nếu chưa có, thử tìm sau một chút
Invoke(nameof(RegisterSpawnerEvents), 0.1f);
}
}
@@ -107,6 +107,7 @@ namespace Hallucinate.UI
BasicSpawner.Instance.OnSessionListUpdatedEvent += UpdateRoomList;
BasicSpawner.Instance.OnJoinFailedEvent += () => { if(_joinPassError != null) _joinPassError.style.display = DisplayStyle.Flex; };
BasicSpawner.Instance.OnJoinStartedEvent += () => { /* Show loading if needed */ };
_ = BasicSpawner.Instance.StartLobby();
}
public void SetRoomTemplate(VisualTreeAsset template) => _roomItemTemplate = template;
@@ -122,7 +123,7 @@ 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();
_ = BasicSpawner.Instance?.StartLobby();
}
public void ShowCreate()
@@ -144,11 +145,7 @@ namespace Hallucinate.UI
private async void OnCreateRoomClicked()
{
var spawner = BasicSpawner.Instance;
if (spawner == null)
{
Debug.LogError("[LobbyController] BasicSpawner.Instance is missing!");
return;
}
if (spawner == null) return;
string id = _roomIDInput != null && !string.IsNullOrEmpty(_roomIDInput.value)
? _roomIDInput.value.Trim()
@@ -165,14 +162,7 @@ namespace Hallucinate.UI
: null;
bool success = await spawner.StartHost(id, pass);
if (success)
{
ShowLounge(name);
}
else
{
Debug.LogWarning("[LobbyController] Failed to create room. Please check AppID/Region.");
}
if (success) ShowLounge(name);
}
private void UpdateRoomList(List<SessionInfo> sessions)