fix(NetworkDiscoveryHUD): More robust OnValidate / Reset

Handler would not be added if component ref not null.
This commit is contained in:
MrGadget 2024-10-22 02:37:31 -04:00
parent fdc9369de4
commit e6509cec34
3 changed files with 38 additions and 9 deletions

View File

@ -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<byte> data = writer.ToArraySegment();
//Debug.Log($"Discovery: Sending BroadcastDiscoveryRequest {request}");
clientUdpClient.SendAsync(data.Array, data.Count, endPoint);
}
catch (Exception)

View File

@ -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<NetworkDiscovery>();
// Add default event handler if not already present
if (!Enumerable.Range(0, networkDiscovery.OnServerFound.GetPersistentEventCount())
.Any(i => networkDiscovery.OnServerFound.GetPersistentMethodName(i) == "OnDiscoveredServer"))
{
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;
}

View File

@ -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