Skip to content

Commit

Permalink
fix(NetworkDiscoveryHUD): More robust OnValidate / Reset (#3929)
Browse files Browse the repository at this point in the history
* fix(NetworkDiscoveryHUD): More robust OnValidate / Reset
Handler would not be added if component ref not null.

* Use nameof(OnDiscoveredServer)
  • Loading branch information
MrGadget1024 authored Nov 29, 2024
1 parent f36e585 commit 6c5403b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
3 changes: 3 additions & 0 deletions Assets/Mirror/Components/Discovery/NetworkDiscoveryBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ public void AdvertiseServer()
MulticastLoopback = false
};

//Debug.Log($"Discovery: Advertising Server {Dns.GetHostName()}");

// listen for client pings
_ = ServerListenAsync();
}
Expand Down Expand Up @@ -418,6 +420,7 @@ public void BroadcastDiscoveryRequest()

ArraySegment<byte> data = writer.ToArraySegment();

//Debug.Log($"Discovery: Sending BroadcastDiscoveryRequest {request}");
clientUdpClient.SendAsync(data.Array, data.Count, endPoint);
}
catch (Exception)
Expand Down
18 changes: 15 additions & 3 deletions Assets/Mirror/Components/Discovery/NetworkDiscoveryHUD.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace Mirror.Discovery
Expand All @@ -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<NetworkDiscovery>();

// Add default event handler if not already present
if (!Enumerable.Range(0, networkDiscovery.OnServerFound.GetPersistentEventCount())
.Any(i => networkDiscovery.OnServerFound.GetPersistentMethodName(i) == nameof(OnDiscoveredServer)))
{
networkDiscovery = GetComponent<NetworkDiscovery>();
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
Expand Down Expand Up @@ -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;
}
Expand Down
27 changes: 21 additions & 6 deletions Assets/Mirror/Examples/Discovery/Scenes/MirrorDiscovery.unity
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ RenderSettings:
--- !u!157 &3
LightmapSettings:
m_ObjectHideFlags: 0
serializedVersion: 11
serializedVersion: 12
m_GIWorkflowMode: 0
m_GISettings:
serializedVersion: 2
Expand Down Expand Up @@ -98,7 +98,7 @@ LightmapSettings:
m_TrainingDataDestination: TrainingData
m_LightProbeSampleCountMultiplier: 4
m_LightingDataAsset: {fileID: 0}
m_UseShadowmask: 1
m_LightingSettings: {fileID: 0}
--- !u!196 &4
NavMeshSettings:
serializedVersion: 2
Expand All @@ -118,6 +118,8 @@ NavMeshSettings:
manualTileSize: 0
tileSize: 256
accuratePlacement: 0
maxJobWorkers: 0
preserveTilesOutsideBounds: 0
debug:
m_Flags: 0
m_NavMeshData: {fileID: 0}
Expand Down Expand Up @@ -433,8 +435,6 @@ MonoBehaviour:
headlessStartMode: 1
editorAutoStart: 0
sendRate: 30
autoStartServerBuild: 0
autoConnectClientBuild: 0
offlineScene:
onlineScene:
offlineSceneLoadDelay: 0
Expand All @@ -444,8 +444,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: []
Expand Down Expand Up @@ -508,6 +507,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:
Expand Down Expand Up @@ -628,6 +642,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
Expand Down

0 comments on commit 6c5403b

Please sign in to comment.