This commit is contained in:
2026-04-30 15:45:37 +07:00
parent c2b0e96570
commit 0542f5656f
4 changed files with 33 additions and 34 deletions

View File

@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
@@ -19,7 +19,7 @@ namespace Hallucinate.UI
public event Action OnJoinStartedEvent;
public event Action OnJoinFailedEvent;
[Header(""Prefabs"")]
[Header("Prefabs")]
[SerializeField] private NetworkPrefabRef _playerPrefab;
[SerializeField] private NetworkPrefabRef _playerDataManagerPrefab;
@@ -71,7 +71,7 @@ namespace Hallucinate.UI
var result = await _runner.JoinSessionLobby(SessionLobby.ClientServer);
if (!result.Ok)
{
Debug.LogWarning($""Join lobby result: {result.ShutdownReason}. This is often normal on first run if already connecting."");
Debug.LogWarning($"Join lobby result: {result.ShutdownReason}. This is often normal on first run if already connecting.");
}
}
@@ -82,7 +82,7 @@ namespace Hallucinate.UI
bool sceneExists = false;
for (int i = 0; i < UnityEngine.SceneManagement.SceneManager.sceneCountInBuildSettings; i++)
{
if (UnityEngine.SceneManagement.SceneUtility.GetScenePathByBuildIndex(i).Contains(""Main Scene""))
if (UnityEngine.SceneManagement.SceneUtility.GetScenePathByBuildIndex(i).Contains("Main Scene"))
{
sceneExists = true;
break;
@@ -91,7 +91,7 @@ namespace Hallucinate.UI
if (!sceneExists)
{
Debug.LogError(""CRITICAL: 'Main Scene' is NOT in Build Settings!"");
Debug.LogError("CRITICAL: 'Main Scene' is NOT in Build Settings!");
return false;
}
@@ -100,9 +100,9 @@ namespace Hallucinate.UI
var customProps = new Dictionary<string, SessionProperty>();
if (!string.IsNullOrEmpty(password))
{
customProps.Add(""pw"", password);
customProps.Add("pw", password);
}
customProps.Add(""rn"", displayName);
customProps.Add("rn", displayName);
var result = await _runner.StartGame(new StartGameArgs()
{
@@ -126,7 +126,7 @@ namespace Hallucinate.UI
}
else
{
Debug.LogError($""Fusion StartHost Failed: {result.ShutdownReason}."");
Debug.LogError($"Fusion StartHost Failed: {result.ShutdownReason}.");
OnJoinFailedEvent?.Invoke();
return false;
}
@@ -150,7 +150,7 @@ namespace Hallucinate.UI
}
else
{
Debug.LogError($""Fusion StartClient Failed: {result.ShutdownReason}"");
Debug.LogError($"Fusion StartClient Failed: {result.ShutdownReason}");
OnJoinFailedEvent?.Invoke();
return false;
}
@@ -180,10 +180,10 @@ namespace Hallucinate.UI
if (pdm != null)
{
string playerName = LocalPlayerProfile != null ? LocalPlayerProfile.Name : ""Player "" + player.PlayerId;
string playerName = LocalPlayerProfile != null ? LocalPlayerProfile.Name : "Player " + player.PlayerId;
// Thêm hậu tố (HOST) nếu là server để dễ phân biệt
if (_runner.IsServer) playerName += "" (HOST)"";
if (_runner.IsServer) playerName += " (HOST)";
_Role playerRole = _Role.Seeker;
@@ -197,7 +197,7 @@ namespace Hallucinate.UI
}
else
{
Debug.LogError(""[BasicSpawner] Could not find PlayerDataManager after retries. Data will not sync."");
Debug.LogError("[BasicSpawner] Could not find PlayerDataManager after retries. Data will not sync.");
}
}
@@ -205,7 +205,7 @@ namespace Hallucinate.UI
{
if (_runner != null && _runner.IsServer)
{
_runner.LoadScene(""Main Scene"");
_runner.LoadScene("Main Scene");
}
}
@@ -225,7 +225,7 @@ namespace Hallucinate.UI
public void OnShutdown(NetworkRunner runner, ShutdownReason shutdownReason)
{
Debug.LogWarning($""[Fusion] Shutdown occurred. Reason: {shutdownReason}"");
Debug.LogWarning($"[Fusion] Shutdown occurred. Reason: {shutdownReason}");
OnShutdownEvent?.Invoke(shutdownReason.ToString());
if (UIManager.Instance != null)
@@ -268,7 +268,7 @@ namespace Hallucinate.UI
public void OnSceneLoadDone(NetworkRunner runner)
{
string currentSceneName = UnityEngine.SceneManagement.SceneManager.GetActiveScene().name;
if (runner.IsServer && currentSceneName == ""Main Scene"")
if (runner.IsServer && currentSceneName == "Main Scene")
{
foreach (var player in runner.ActivePlayers)
{
@@ -277,12 +277,20 @@ namespace Hallucinate.UI
_spawnedCharacters.Add(player, networkPlayerObject);
}
}
if (currentSceneName == ""Main Scene"")
// Removed incorrect UI transition for Lobby/Menu scenes to allow LobbyController to manage its state.
// The original logic incorrectly called UIManager.OnBackToMenu() when entering the Lobby scene,
// causing the redirect to the Main Menu after creating a room.
// This block ensures that only the Main Scene triggers a specific UI transition (OnGameStarted).
// If other scenes like "Lobby" or "Menu" are loaded, no automatic transition is forced from here,
// letting scene-specific controllers (like LobbyController) manage their UI.
if (currentSceneName == "Main Scene")
{
UIManager.Instance?.OnGameStarted();
else if (currentSceneName == ""Lobby"" || currentSceneName == ""Menu"")
UIManager.Instance?.OnBackToMenu();
}
// Removed the problematic else-if block that would incorrectly call OnBackToMenu for "Lobby" or "Menu" scenes.
}
public void OnSceneLoadStart(NetworkRunner runner) { }
}
}

View File

@@ -360,17 +360,10 @@ namespace Hallucinate.UI
PlayerRef hostRef = PlayerRef.None;
PlayerRef guestRef = PlayerRef.None;
foreach (var p in runner.ActivePlayers)
{
if (runner.IsPlayerServer(p))
{
hostRef = p;
}
else
{
guestRef = p;
}
}
// Trong Host Mode, chủ phòng luôn là người có PlayerId = 1
var sortedPlayers = runner.ActivePlayers.OrderBy(p => p.PlayerId).ToList();
if (sortedPlayers.Count > 0) hostRef = sortedPlayers[0];
if (sortedPlayers.Count > 1) guestRef = sortedPlayers[1];
// Update Room Name for Guest
if (runner.SessionInfo != null && runner.SessionInfo.Properties.TryGetValue("rn", out var rnProp))