Added multiplayer joining capability to the desktop UI

This commit is contained in:
2025-09-30 18:22:46 +03:00
parent 803e87688a
commit 1580f40636
6 changed files with 1231 additions and 330 deletions

View File

@@ -4,6 +4,7 @@ using FishNet.Discovery;
using FishNet.Managing.Scened;
using FishNet.Object;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Net;
using TMPro;
@@ -13,6 +14,9 @@ using UnityEngine.UI;
public class NetworkMenuUI : MonoBehaviour
{
private ConcurrentQueue<System.Action> mainThreadQueue = new();
[Header("UI References")]
public Toggle VRToggle;
@@ -29,12 +33,14 @@ public class NetworkMenuUI : MonoBehaviour
public Camera uiCamera;
public AudioListener placeholderAudioListener;
private bool _useVR;
//public Image coverImage;
private readonly List<IPEndPoint> foundServers = new();
private void Start()
{
if (networkDiscovery == null)
networkDiscovery = FindObjectOfType<NetworkDiscovery>();
@@ -55,9 +61,23 @@ public class NetworkMenuUI : MonoBehaviour
joinMultiplayerButton.onClick.AddListener(OnJoinMultiplayer);
quitButton.onClick.AddListener(OnQuit);
networkDiscovery.ServerFoundCallback += OnServerFound;
networkDiscovery.ServerFoundCallback += (endPoint) =>
{
Debug.Log("Found a server");
// Only queue the endpoint for main-thread processing
mainThreadQueue.Enqueue(() =>
{
StartCoroutine(ProcessServerFound(endPoint));
});
};
}
private void Update()
{
while (mainThreadQueue.TryDequeue(out var action))
{
action.Invoke(); // safely runs on main thread
}
}
private void OnStartPlaying()
{
statusText.text = "Starting host...";
@@ -100,8 +120,11 @@ public class NetworkMenuUI : MonoBehaviour
private void OnJoinMultiplayer()
{
statusText.text = "Searching for servers...";
networkDiscovery.StopSearchingForServers();
ClearServerList();
//Debug.Log(foundServers.Count);
foundServers.Clear();
//Debug.Log(foundServers.Count);
networkDiscovery.StartSearchingForServers();
}
@@ -111,45 +134,57 @@ public class NetworkMenuUI : MonoBehaviour
Application.Quit();
}
private void OnServerFound(IPEndPoint endPoint)
private IEnumerator ProcessServerFound(IPEndPoint endPoint)
{
if (foundServers.Contains(endPoint)) return;
Debug.Log("server found");
//Debug.Log(endPoint.Address.ToString());
//Debug.Log(foundServers.Count);
if (foundServers.Contains(endPoint)) yield return null;
foundServers.Add(endPoint);
// Auto-join if started as host
if (InstanceFinder.IsServer)
{
//networkDiscovery.StopAdvertisingServer();
networkDiscovery.StopSearchingForServers();
Debug.Log($"Server found {endPoint}");
// Auto-join if started as host
if (InstanceFinder.IsServer)
{
Debug.Log("Server is local");
//networkDiscovery.StopAdvertisingServer();
networkDiscovery.StopSearchingForServers();
uiCamera.enabled = false;
Debug.Log("Disabled placeholder audio source");
placeholderAudioListener.enabled = false;
//coverImage.gameObject.SetActive(false);
InstanceFinder.ClientManager.StartConnection(endPoint.Address.ToString());
statusText.text = $"Joined server: {endPoint.Address}";
return;
}
// Display in UI for manual joining
GameObject item = Instantiate(serverListItemPrefab, serverListContainer);
TMP_Text label = item.GetComponentInChildren<TMP_Text>();
label.text = endPoint.Address.ToString();
Button btn = item.GetComponent<Button>();
btn.onClick.AddListener(() =>
{
networkDiscovery.StopSearchingForServers();
if (uiCamera != null) uiCamera.enabled = false;
if (placeholderAudioListener != null) {
uiCamera.enabled = false;
Debug.Log("Disabled placeholder audio source");
placeholderAudioListener.enabled = false;
placeholderAudioListener.enabled = false;
//coverImage.gameObject.SetActive(false);
InstanceFinder.ClientManager.StartConnection(endPoint.Address.ToString());
statusText.text = $"Joined server: {endPoint.Address}";
yield return null;
}
//coverImage.gameObject.SetActive(false);
InstanceFinder.ClientManager.StartConnection(endPoint.Address.ToString());
statusText.text = $"Joined server: {endPoint.Address}";
});
Debug.Log("Server is foregin");
GameObject item = Instantiate(serverListItemPrefab, serverListContainer);
TMP_Text label = item.GetComponentInChildren<TMP_Text>();
if (label != null)
label.text = endPoint.Address.ToString();
Button btn = item.GetComponent<Button>();
btn.onClick.RemoveAllListeners(); // clear any old listener
btn.onClick.AddListener(() =>
{
networkDiscovery.StopSearchingForServers();
if (uiCamera != null) uiCamera.enabled = false;
if (placeholderAudioListener != null)
{
Debug.Log("Disabled placeholder audio source");
placeholderAudioListener.enabled = false;
}
//coverImage.gameObject.SetActive(false);
InstanceFinder.ClientManager.StartConnection(endPoint.Address.ToString());
statusText.text = $"Joined server: {endPoint.Address}";
});
}
private Coroutine joinRoutine;