|
|
|
@ -8,9 +8,6 @@ using UnityEngine.SceneManagement;
|
|
|
|
|
using TMPro;
|
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using Fusion.Photon.Realtime;
|
|
|
|
|
using static Unity.Collections.Unicode;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class FusionLauncher : MonoBehaviour, INetworkRunnerCallbacks
|
|
|
|
|
{
|
|
|
|
@ -66,7 +63,6 @@ public class FusionLauncher : MonoBehaviour, INetworkRunnerCallbacks
|
|
|
|
|
|
|
|
|
|
IEnumerator AutoRefreshLobbyList()
|
|
|
|
|
{
|
|
|
|
|
yield return new WaitForSeconds(2f);
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
RefreshLobbyList();
|
|
|
|
@ -116,20 +112,31 @@ public class FusionLauncher : MonoBehaviour, INetworkRunnerCallbacks
|
|
|
|
|
// —————— RESET / CREATE FRESH RUNNER ——————
|
|
|
|
|
private async Task EnsureFreshRunner()
|
|
|
|
|
{
|
|
|
|
|
// … your existing shutdown/destroy logic …
|
|
|
|
|
// Reset flags
|
|
|
|
|
playerCount = 0;
|
|
|
|
|
gameplayLoaded = false;
|
|
|
|
|
connectedToServer = false;
|
|
|
|
|
|
|
|
|
|
// If an old runner exists & is running, shut it down & destroy its GO
|
|
|
|
|
if (runner != null && runner.IsRunning)
|
|
|
|
|
{
|
|
|
|
|
await runner.Shutdown();
|
|
|
|
|
Destroy(runner.gameObject);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Stop auto-refresh while we rebuild
|
|
|
|
|
if (refreshCoroutine != null)
|
|
|
|
|
{
|
|
|
|
|
StopCoroutine(refreshCoroutine);
|
|
|
|
|
refreshCoroutine = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Make brand-new runner GameObject
|
|
|
|
|
var go = new GameObject("NetworkRunnerGO");
|
|
|
|
|
DontDestroyOnLoad(go);
|
|
|
|
|
DontDestroyOnLoad(go); // keep it alive across scenes
|
|
|
|
|
|
|
|
|
|
// 1) Add the runner
|
|
|
|
|
runner = go.AddComponent<NetworkRunner>();
|
|
|
|
|
runner.ProvideInput = true;
|
|
|
|
|
|
|
|
|
|
// 2) Add *and* register your provider
|
|
|
|
|
var inputProv = go.AddComponent<FusionInputProvider>();
|
|
|
|
|
runner.AddCallbacks(inputProv);
|
|
|
|
|
|
|
|
|
|
// 3) If you still want FusionLauncher callbacks:
|
|
|
|
|
runner.AddCallbacks(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -139,29 +146,33 @@ public class FusionLauncher : MonoBehaviour, INetworkRunnerCallbacks
|
|
|
|
|
var name = lobbyNameInput.text.Trim();
|
|
|
|
|
if (string.IsNullOrEmpty(name))
|
|
|
|
|
{
|
|
|
|
|
Debug.LogWarning("[Host] Empty lobby name, not creating.");
|
|
|
|
|
Debug.LogWarning("Empty lobby name!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Tear down browse runner & spawn a fresh one
|
|
|
|
|
await EnsureFreshRunner();
|
|
|
|
|
|
|
|
|
|
var sceneRef = SceneRef.FromIndex(SceneManager.GetActiveScene().buildIndex);
|
|
|
|
|
var res = await runner.StartGame(new StartGameArgs()
|
|
|
|
|
{
|
|
|
|
|
GameMode = GameMode.Host,
|
|
|
|
|
SessionName = name,
|
|
|
|
|
PlayerCount = maxPlayers,
|
|
|
|
|
Scene = SceneRef.FromIndex(SceneManager.GetActiveScene().buildIndex),
|
|
|
|
|
Scene = sceneRef,
|
|
|
|
|
SceneManager = runner.gameObject.AddComponent<NetworkSceneManagerDefault>()
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (res.Ok)
|
|
|
|
|
{
|
|
|
|
|
Debug.Log($"✔ Hosted Lobby '{name}'");
|
|
|
|
|
lobbyUI.SetActive(false);
|
|
|
|
|
waitingText.text = "Waiting for player 2…";
|
|
|
|
|
waitingText.gameObject.SetActive(true);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError($"[Host] ✖ Host failed: {res.ShutdownReason}");
|
|
|
|
|
Debug.LogError($"✖ Host failed: {res.ShutdownReason}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -192,40 +203,14 @@ public class FusionLauncher : MonoBehaviour, INetworkRunnerCallbacks
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool isJoiningLobby = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[ContextMenu("RefreshLobbyList")]
|
|
|
|
|
// —————— REFRESH LOBBY LIST ——————
|
|
|
|
|
public void RefreshLobbyList()
|
|
|
|
|
{
|
|
|
|
|
// 1) ensure runner exists & is running
|
|
|
|
|
if (runner == null || !runner.IsRunning)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// 2) don’t spam JoinLobby if one is in flight
|
|
|
|
|
if (isJoiningLobby)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// 3) only once we’re cloud-ready
|
|
|
|
|
if (!runner.IsCloudReady)
|
|
|
|
|
{
|
|
|
|
|
Debug.Log("[Client] skipping Refresh: not yet cloud-ready");
|
|
|
|
|
return;
|
|
|
|
|
if (!connectedToServer) return;
|
|
|
|
|
//sessionListUIHandler.ClearList();
|
|
|
|
|
runner.JoinSessionLobby(SessionLobby.ClientServer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 4) now we can safely join the lobby
|
|
|
|
|
isJoiningLobby = true;
|
|
|
|
|
runner.JoinSessionLobby(SessionLobby.ClientServer)
|
|
|
|
|
.ContinueWith(_ => isJoiningLobby = false);
|
|
|
|
|
} // —————— REFRESH LOBBY LIST ——————
|
|
|
|
|
//public void RefreshLobbyList()
|
|
|
|
|
//{
|
|
|
|
|
// if (!connectedToServer) return;
|
|
|
|
|
// ////sessionListUIHandler.ClearList();
|
|
|
|
|
// runner.JoinSessionLobby(SessionLobby.ClientServer);
|
|
|
|
|
// //runner.JoinSessionLobby(SessionLobby.Shared);
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
// —————— FUSION CALLBACKS ——————
|
|
|
|
|
public void OnConnectedToServer(NetworkRunner r)
|
|
|
|
|
{
|
|
|
|
|