using Fusion; using Fusion.Sockets; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; public class FusionLauncher : MonoBehaviour, INetworkRunnerCallbacks { [Header("Network Runner")] public NetworkRunner runner; [Header("Lobby UI")] public GameObject lobbyUI; public Transform lobbyListContainer; public GameObject lobbyButtonPrefab; private void Start() { if (runner == null) { Debug.LogError("Runner is not assigned in the Inspector!"); return; } runner.ProvideInput = true; runner.AddCallbacks(this); } public async void CreateLobby() { string sessionName = "Lobby_" + Random.Range(1000, 9999); var result = await runner.StartGame(new StartGameArgs() { GameMode = GameMode.Host, SessionName = sessionName, PlayerCount = 2, Scene = SceneRef.FromIndex(SceneManager.GetActiveScene().buildIndex), SceneManager = gameObject.AddComponent() }); if (result.Ok) { Debug.Log($"✔ Created Lobby: {sessionName}"); lobbyUI.SetActive(false); } else { Debug.LogError($"✖ Failed to create lobby: {result.ShutdownReason}"); } } public async void JoinLobby(string lobbyName) { var result = await runner.StartGame(new StartGameArgs() { GameMode = GameMode.Client, SessionName = lobbyName, Scene = SceneRef.FromIndex(SceneManager.GetActiveScene().buildIndex), SceneManager = gameObject.AddComponent() }); if (result.Ok) { Debug.Log($"✔ Joined Lobby: {lobbyName}"); lobbyUI.SetActive(false); } else { Debug.LogError($"✖ Failed to join lobby: {result.ShutdownReason}"); } } public void RefreshLobbyList() { runner.JoinSessionLobby(SessionLobby.ClientServer); // This triggers OnSessionListUpdated } public void OnSessionListUpdated(NetworkRunner runner, List sessionList) { foreach (Transform child in lobbyListContainer) { Destroy(child.gameObject); } foreach (var session in sessionList) { if (session.IsOpen && session.IsVisible && session.PlayerCount < session.MaxPlayers) { GameObject button = Instantiate(lobbyButtonPrefab, lobbyListContainer); button.GetComponentInChildren().text = session.Name; button.GetComponent