Added multiplayer joining capability to the desktop UI

This commit is contained in:
mxssw 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;

View File

@ -0,0 +1,262 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &1718131600983244308
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 2576899869751053199}
- component: {fileID: 3637504309793555790}
- component: {fileID: 7198265083142013856}
m_Layer: 0
m_Name: Text (TMP)
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &2576899869751053199
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1718131600983244308}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 6142704360089163846}
m_RootOrder: -1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 1, y: 1}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 0, y: 0}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!222 &3637504309793555790
CanvasRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1718131600983244308}
m_CullTransparentMesh: 1
--- !u!114 &7198265083142013856
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1718131600983244308}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: f4688fdb7df04437aeb418b961361dc5, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_RaycastTarget: 1
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
m_Maskable: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_text: Sample IP
m_isRightToLeft: 0
m_fontAsset: {fileID: 11400000, guid: 254d33525bc3919439f569ea33703c5b, type: 2}
m_sharedMaterial: {fileID: 4369893532151414794, guid: 254d33525bc3919439f569ea33703c5b,
type: 2}
m_fontSharedMaterials: []
m_fontMaterial: {fileID: 0}
m_fontMaterials: []
m_fontColor32:
serializedVersion: 2
rgba: 4294967295
m_fontColor: {r: 1, g: 1, b: 1, a: 1}
m_enableVertexGradient: 0
m_colorMode: 3
m_fontColorGradient:
topLeft: {r: 1, g: 1, b: 1, a: 1}
topRight: {r: 1, g: 1, b: 1, a: 1}
bottomLeft: {r: 1, g: 1, b: 1, a: 1}
bottomRight: {r: 1, g: 1, b: 1, a: 1}
m_fontColorGradientPreset: {fileID: 0}
m_spriteAsset: {fileID: 0}
m_tintAllSprites: 0
m_StyleSheet: {fileID: 0}
m_TextStyleHashCode: -1183493901
m_overrideHtmlColors: 0
m_faceColor:
serializedVersion: 2
rgba: 4294967295
m_fontSize: 20
m_fontSizeBase: 20
m_fontWeight: 400
m_enableAutoSizing: 0
m_fontSizeMin: 18
m_fontSizeMax: 72
m_fontStyle: 0
m_HorizontalAlignment: 2
m_VerticalAlignment: 512
m_textAlignment: 65535
m_characterSpacing: 0
m_wordSpacing: 0
m_lineSpacing: 0
m_lineSpacingMax: 0
m_paragraphSpacing: 0
m_charWidthMaxAdj: 0
m_TextWrappingMode: 1
m_wordWrappingRatios: 0.4
m_overflowMode: 0
m_linkedTextComponent: {fileID: 0}
parentLinkedComponent: {fileID: 0}
m_enableKerning: 0
m_ActiveFontFeatures: 6e72656b
m_enableExtraPadding: 0
checkPaddingRequired: 0
m_isRichText: 1
m_EmojiFallbackSupport: 1
m_parseCtrlCharacters: 1
m_isOrthographic: 1
m_isCullingEnabled: 0
m_horizontalMapping: 0
m_verticalMapping: 0
m_uvLineOffset: 0
m_geometrySortingOrder: 0
m_IsTextObjectScaleStatic: 0
m_VertexBufferAutoSizeReduction: 0
m_useMaxVisibleDescender: 1
m_pageToDisplay: 1
m_margin: {x: 0, y: 0, z: 0, w: 0}
m_isUsingLegacyAnimationComponent: 0
m_isVolumetricText: 0
m_hasFontAssetChanged: 0
m_baseMaterial: {fileID: 0}
m_maskOffset: {x: 0, y: 0, z: 0, w: 0}
--- !u!1 &8155264290485183093
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 6142704360089163846}
- component: {fileID: 6886962170738448281}
- component: {fileID: 636455074159682324}
- component: {fileID: 5691563554839744214}
m_Layer: 0
m_Name: IP Button
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &6142704360089163846
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8155264290485183093}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1.2, y: 1.2, z: 1.2}
m_ConstrainProportionsScale: 1
m_Children:
- {fileID: 2576899869751053199}
m_Father: {fileID: 0}
m_RootOrder: -1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 160, y: 30}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!222 &6886962170738448281
CanvasRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8155264290485183093}
m_CullTransparentMesh: 1
--- !u!114 &636455074159682324
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8155264290485183093}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_RaycastTarget: 1
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
m_Maskable: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_Sprite: {fileID: 10905, guid: 0000000000000000f000000000000000, type: 0}
m_Type: 1
m_PreserveAspect: 0
m_FillCenter: 1
m_FillMethod: 4
m_FillAmount: 1
m_FillClockwise: 1
m_FillOrigin: 0
m_UseSpriteMesh: 0
m_PixelsPerUnitMultiplier: 1
--- !u!114 &5691563554839744214
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8155264290485183093}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 4e29b1a8efbd4b44bb3f3716e73f07ff, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Navigation:
m_Mode: 3
m_WrapAround: 0
m_SelectOnUp: {fileID: 0}
m_SelectOnDown: {fileID: 0}
m_SelectOnLeft: {fileID: 0}
m_SelectOnRight: {fileID: 0}
m_Transition: 1
m_Colors:
m_NormalColor: {r: 0.21698111, g: 0.21698111, b: 0.21698111, a: 0.9019608}
m_HighlightedColor: {r: 0.21960784, g: 0.21960784, b: 0.21960784, a: 1}
m_PressedColor: {r: 0.21960784, g: 0.21960784, b: 0.21960784, a: 1}
m_SelectedColor: {r: 0.21960784, g: 0.21960784, b: 0.21960784, a: 1}
m_DisabledColor: {r: 0.21960784, g: 0.21960784, b: 0.21960784, a: 0.5019608}
m_ColorMultiplier: 1
m_FadeDuration: 0.1
m_SpriteState:
m_HighlightedSprite: {fileID: 0}
m_PressedSprite: {fileID: 0}
m_SelectedSprite: {fileID: 0}
m_DisabledSprite: {fileID: 0}
m_AnimationTriggers:
m_NormalTrigger: Normal
m_HighlightedTrigger: Highlighted
m_PressedTrigger: Pressed
m_SelectedTrigger: Selected
m_DisabledTrigger: Disabled
m_Interactable: 1
m_TargetGraphic: {fileID: 636455074159682324}
m_OnClick:
m_PersistentCalls:
m_Calls: []

View File

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 161eef8a6244baa46b0988be67d74a08
PrefabImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

File diff suppressed because it is too large Load Diff

Binary file not shown.

BIN
ProjectSettings/ProjectSettings.asset (Stored with Git LFS)

Binary file not shown.