From e6509cec34e8a017b30784d6955c917af1f67e3e Mon Sep 17 00:00:00 2001 From: MrGadget <9826063+MrGadget1024@users.noreply.github.com> Date: Tue, 22 Oct 2024 02:37:31 -0400 Subject: [PATCH] fix(NetworkDiscoveryHUD): More robust OnValidate / Reset Handler would not be added if component ref not null. --- .../Discovery/NetworkDiscoveryBase.cs | 3 +++ .../Discovery/NetworkDiscoveryHUD.cs | 18 ++++++++++--- .../Discovery/Scenes/MirrorDiscovery.unity | 26 ++++++++++++++----- 3 files changed, 38 insertions(+), 9 deletions(-) diff --git a/Assets/Mirror/Components/Discovery/NetworkDiscoveryBase.cs b/Assets/Mirror/Components/Discovery/NetworkDiscoveryBase.cs index 441734402..677bad336 100644 --- a/Assets/Mirror/Components/Discovery/NetworkDiscoveryBase.cs +++ b/Assets/Mirror/Components/Discovery/NetworkDiscoveryBase.cs @@ -170,6 +170,8 @@ public void AdvertiseServer() MulticastLoopback = false }; + //Debug.Log($"Discovery: Advertising Server {Dns.GetHostName()}"); + // listen for client pings _ = ServerListenAsync(); } @@ -418,6 +420,7 @@ public void BroadcastDiscoveryRequest() ArraySegment data = writer.ToArraySegment(); + //Debug.Log($"Discovery: Sending BroadcastDiscoveryRequest {request}"); clientUdpClient.SendAsync(data.Array, data.Count, endPoint); } catch (Exception) diff --git a/Assets/Mirror/Components/Discovery/NetworkDiscoveryHUD.cs b/Assets/Mirror/Components/Discovery/NetworkDiscoveryHUD.cs index e43c3d772..94aad86cc 100644 --- a/Assets/Mirror/Components/Discovery/NetworkDiscoveryHUD.cs +++ b/Assets/Mirror/Components/Discovery/NetworkDiscoveryHUD.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Linq; using UnityEngine; namespace Mirror.Discovery @@ -17,11 +18,20 @@ public class NetworkDiscoveryHUD : MonoBehaviour #if UNITY_EDITOR void OnValidate() { - if (networkDiscovery == null) + if (Application.isPlaying) return; + Reset(); + } + + void Reset() + { + networkDiscovery = GetComponent(); + + // Add default event handler if not already present + if (!Enumerable.Range(0, networkDiscovery.OnServerFound.GetPersistentEventCount()) + .Any(i => networkDiscovery.OnServerFound.GetPersistentMethodName(i) == "OnDiscoveredServer")) { - networkDiscovery = GetComponent(); UnityEditor.Events.UnityEventTools.AddPersistentListener(networkDiscovery.OnServerFound, OnDiscoveredServer); - UnityEditor.Undo.RecordObjects(new Object[] { this, networkDiscovery }, "Set NetworkDiscovery"); + UnityEditor.Undo.RecordObjects(new UnityEngine.Object[] { this, networkDiscovery }, "Set NetworkDiscovery"); } } #endif @@ -125,6 +135,8 @@ void Connect(ServerResponse info) public void OnDiscoveredServer(ServerResponse info) { + Debug.Log($"Discovered Server: {info.serverId} | {info.EndPoint} | {info.uri}"); + // Note that you can check the versioning to decide if you can connect to the server or not using this method discoveredServers[info.serverId] = info; } diff --git a/Assets/Mirror/Examples/Discovery/Scenes/MirrorDiscovery.unity b/Assets/Mirror/Examples/Discovery/Scenes/MirrorDiscovery.unity index 605a3855d..8a4c05a6d 100644 --- a/Assets/Mirror/Examples/Discovery/Scenes/MirrorDiscovery.unity +++ b/Assets/Mirror/Examples/Discovery/Scenes/MirrorDiscovery.unity @@ -43,7 +43,7 @@ RenderSettings: --- !u!157 &3 LightmapSettings: m_ObjectHideFlags: 0 - serializedVersion: 11 + serializedVersion: 12 m_GIWorkflowMode: 0 m_GISettings: serializedVersion: 2 @@ -98,7 +98,6 @@ LightmapSettings: m_TrainingDataDestination: TrainingData m_LightProbeSampleCountMultiplier: 4 m_LightingDataAsset: {fileID: 0} - m_UseShadowmask: 1 --- !u!196 &4 NavMeshSettings: serializedVersion: 2 @@ -118,6 +117,8 @@ NavMeshSettings: manualTileSize: 0 tileSize: 256 accuratePlacement: 0 + maxJobWorkers: 0 + preserveTilesOutsideBounds: 0 debug: m_Flags: 0 m_NavMeshData: {fileID: 0} @@ -433,8 +434,6 @@ MonoBehaviour: headlessStartMode: 1 editorAutoStart: 0 sendRate: 30 - autoStartServerBuild: 0 - autoConnectClientBuild: 0 offlineScene: onlineScene: offlineSceneLoadDelay: 0 @@ -444,8 +443,7 @@ MonoBehaviour: disconnectInactiveConnections: 0 disconnectInactiveTimeout: 60 authenticator: {fileID: 0} - playerPrefab: {fileID: 9081919128954505657, guid: ecd52c53a6ef7496693343d3e32dace1, - type: 3} + playerPrefab: {fileID: 9081919128954505657, guid: ecd52c53a6ef7496693343d3e32dace1, type: 3} autoCreatePlayer: 1 playerSpawnMethod: 0 spawnPrefabs: [] @@ -508,6 +506,21 @@ MonoBehaviour: serverBroadcastListenPort: 47777 ActiveDiscoveryInterval: 3 transport: {fileID: 1556883244} + OnServerFound: + m_PersistentCalls: + m_Calls: + - m_Target: {fileID: 1556883246} + m_TargetAssemblyTypeName: Mirror.Discovery.NetworkDiscoveryHUD, Mirror.Components + m_MethodName: OnDiscoveredServer + m_Mode: 0 + m_Arguments: + m_ObjectArgument: {fileID: 0} + m_ObjectArgumentAssemblyTypeName: + m_IntArgument: 0 + m_FloatArgument: 0 + m_StringArgument: + m_BoolArgument: 0 + m_CallState: 2 secretHandshake: 1558261479176021378 --- !u!1 &1611696151 GameObject: @@ -628,6 +641,7 @@ Light: m_UseColorTemperature: 0 m_BoundingSphereOverride: {x: 0, y: 0, z: 0, w: 0} m_UseBoundingSphereOverride: 0 + m_UseViewFrustumForShadowCasterCull: 1 m_ShadowRadius: 0 m_ShadowAngle: 0 --- !u!4 &1730851148