Skip to content

Commit

Permalink
fix: ping tool example updates and fixes (#3106)
Browse files Browse the repository at this point in the history
* fix

Fixed issue with negative ping time calculations.
Fixed issue with potential exception on exiting.
Fixed issue with serverhostclient identifier text not displaying in the render view.

* update

Includes some general updates to the project.
Updated to ExtendedNetworkManager that combines the NetworkManager, NetworkManagerHelper, and DisconnectNotify into a single component.
Added PingToolHelper label that provides on-screen directions as to which key to press to toggle the ping tool.
  • Loading branch information
NoelStephensUnity authored Oct 18, 2024
1 parent c2ab328 commit 5378d4d
Show file tree
Hide file tree
Showing 11 changed files with 333 additions and 202 deletions.
4 changes: 2 additions & 2 deletions Examples/PingTool/Assets/DefaultNetworkPrefabs.asset
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ MonoBehaviour:
IsDefault: 1
List:
- Override: 0
Prefab: {fileID: 2522762726852386808, guid: 380c984d34fc8664c8f53fc1d8733a25, type: 3}
Prefab: {fileID: 8921789205124766477, guid: 89b57e576a8d47643b2dbd45b1f8cab1, type: 3}
SourcePrefabToOverride: {fileID: 0}
SourceHashToOverride: 0
OverridingTargetPrefab: {fileID: 0}
- Override: 0
Prefab: {fileID: 8921789205124766477, guid: 89b57e576a8d47643b2dbd45b1f8cab1, type: 3}
Prefab: {fileID: 4874009706086500699, guid: 3f5e8ae5613c82a4f9f859c11a4f2e09, type: 3}
SourcePrefabToOverride: {fileID: 0}
SourceHashToOverride: 0
OverridingTargetPrefab: {fileID: 0}
Expand Down
42 changes: 36 additions & 6 deletions Examples/PingTool/Assets/PingTool/PingTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public class PingTool : NetworkBehaviour
public bool StartWithNetStateMonitorHidden = true;
public KeyCode NetStatsMonitorToggle = KeyCode.Tab;
#endif
public Text PingRateValue;
public Text PingToolHelp;
public bool EnableConsoleLogging = true;

public UnityEvent<ulong, ClientRtt> LogMessage;
Expand Down Expand Up @@ -65,7 +67,7 @@ public void Reset()
private Dictionary<ulong, float> m_NextClientPingTime = new Dictionary<ulong, float>();
private Dictionary<ulong, Coroutine> m_ClientUpdateRoutines = new Dictionary<ulong, Coroutine>();
private Slider m_Slider;
private Text m_PingRateValue;

private Canvas m_PingUICanvas;
private bool m_PingRateUpdated;
private float m_LastUpdatedTime;
Expand Down Expand Up @@ -93,7 +95,6 @@ public ClientRtt GetClientRtt(ulong clientID)
private void Awake()
{
m_Slider = GetComponentInChildren<Slider>();
m_PingRateValue = GetComponentInChildren<Text>();
m_PingUICanvas = GetComponentInChildren<Canvas>();
m_PingUICanvas.gameObject.SetActive(false);
#if MULTIPLAYER_TOOLS
Expand All @@ -102,6 +103,11 @@ private void Awake()
m_NetStatsMonitor.Visible = false;
}
#endif

if (PingToolHelp)
{
PingToolHelp.text = PingToolHelp.text.Replace("<>", $"<{NetStatsMonitorToggle}>");
}
}

protected override void OnNetworkPreSpawn(ref NetworkManager networkManager)
Expand Down Expand Up @@ -156,11 +162,19 @@ public override void OnNetworkSpawn()
InitializeAllClients();
NetworkManager.OnConnectionEvent += OnConnectionEvent;
}
UpdatePingRateValue(m_PingRate.Value);

m_PingRateValue.text = $"{m_PingRate.Value}";
base.OnNetworkSpawn();
}

private void UpdatePingRateValue(int value)
{
if (PingRateValue)
{
PingRateValue.text = $"{m_PingRate.Value}";
}
}

protected override void OnOwnershipChanged(ulong previous, ulong current)
{
if (current == NetworkManager.LocalClientId)
Expand All @@ -182,7 +196,7 @@ protected override void OnOwnershipChanged(ulong previous, ulong current)
private void OnPingRateChanged(int previous, int current)
{
InitializePingClientTime(OwnerClientId);
m_PingRateValue.text = $"{current}";
UpdatePingRateValue(current);
}

public override void OnNetworkDespawn()
Expand Down Expand Up @@ -290,7 +304,7 @@ private void OnPingRateChanged()
if (m_Slider.value != PingRate)
{
PingRate = (int)m_Slider.value;
m_PingRateValue.text = $"{PingRate}";
UpdatePingRateValue(PingRate);
// Flag the last time this value updated for the OwnerMonitorPingRateChange coroutine
m_LastUpdatedTime = Time.realtimeSinceStartup;
m_PingRateUpdated = true;
Expand Down Expand Up @@ -377,6 +391,10 @@ private void Update()
if (m_NetStatsMonitor && Input.GetKeyDown(NetStatsMonitorToggle))
{
m_NetStatsMonitor.Visible = !m_NetStatsMonitor.Visible;
if (PingToolHelp)
{
PingToolHelp.gameObject.SetActive(!m_NetStatsMonitor.Visible);
}
}
}
}
Expand Down Expand Up @@ -422,12 +440,24 @@ private void InitializeClient(ulong clientId, bool hasEntry)
[Rpc(SendTo.SpecifiedInParams)]
private void PingRpc(float timeSent, RpcParams rpcParams)
{
// Exit early if despawning or shutdown
if (!IsSpawned || NetworkManager == null || !NetworkManager.IsListening || NetworkManager.ShutdownInProgress)
{
return;
}

PongRpc(Mathf.Abs(NetworkManager.ServerTime.TimeAsFloat - timeSent), RpcTarget.Single(rpcParams.Receive.SenderClientId, RpcTargetUse.Temp));
}

[Rpc(SendTo.SpecifiedInParams)]
private void PongRpc(float timeDelta, RpcParams rpcParams)
{
// Exit early if despawning or shutdown
if (!IsSpawned || NetworkManager == null || !NetworkManager.IsListening || NetworkManager.ShutdownInProgress)
{
return;
}

UpdateClientRTT(rpcParams.Receive.SenderClientId, timeDelta);
}

Expand All @@ -448,7 +478,7 @@ private void UpdateClientRTT(ulong clientId, float timeDelta)
}

var pingEntry = m_ClientPingQueue[clientId];
var ping = timeDelta - (latencyToTimeServer * 0.001f);
var ping = Mathf.Abs(timeDelta - (latencyToTimeServer * 0.001f));
pingEntry.PingTime += ping;
pingEntry.UTP_RTT += currentRTT;
pingEntry.ReceivedPongs++;
Expand Down
101 changes: 99 additions & 2 deletions Examples/PingTool/Assets/PingTool/PingTool.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ RectTransform:
m_LocalScale: {x: 0, y: 0, z: 0}
m_ConstrainProportionsScale: 0
m_Children:
- {fileID: 1251000097366145441}
- {fileID: 6277513074320060017}
- {fileID: 4052611471472719404}
- {fileID: 4428071759692496771}
Expand Down Expand Up @@ -543,7 +544,7 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
GlobalObjectIdHash: 1175888021
InScenePlacedSourceGlobalObjectIdHash: 0
InScenePlacedSourceGlobalObjectIdHash: 290216032
DeferredDespawnTick: 0
Ownership: 2
AlwaysReplicateAsRoot: 0
Expand All @@ -553,6 +554,8 @@ MonoBehaviour:
SpawnWithObservers: 1
DontDestroyWithOwner: 1
AutoObjectParentSync: 1
SyncOwnerTransformWhenParented: 1
AllowOwnerToParent: 0
--- !u!114 &2592079281344418938
MonoBehaviour:
m_ObjectHideFlags: 0
Expand All @@ -565,14 +568,108 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 1466a9d81cf83ff45bca7014cb93c010, type: 3}
m_Name:
m_EditorClassIdentifier:
ShowTopMostFoldoutHeaderGroup: 1
PingRate: 2
StartWithNetStateMonitorHidden: 1
NetStatsMonitorToggle: 9
PingRateValue: {fileID: 3165861447957584369}
PingToolHelp: {fileID: 7873216060197180850}
EnableConsoleLogging: 0
LogMessage:
m_PersistentCalls:
m_Calls: []
m_Calls:
- m_Target: {fileID: 0}
m_TargetAssemblyTypeName: NetworkManagerHelper, Assembly-CSharp
m_MethodName: LogPingToolMessage
m_Mode: 0
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 0
m_FloatArgument: 0
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
m_NetStatsMonitor: {fileID: 1263430958893551039}
--- !u!1 &6370153036591290436
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1251000097366145441}
- component: {fileID: 277549501682696586}
- component: {fileID: 7873216060197180850}
m_Layer: 5
m_Name: PingToolHelper
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &1251000097366145441
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6370153036591290436}
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: 8751044705753477979}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
m_AnchoredPosition: {x: 132.5, y: 25.600006}
m_SizeDelta: {x: 260, y: 24}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!222 &277549501682696586
CanvasRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6370153036591290436}
m_CullTransparentMesh: 1
--- !u!114 &7873216060197180850
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6370153036591290436}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 5f7201a12d95ffc409449d95f23cf332, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 1, g: 0.4606132, b: 0, 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_FontData:
m_Font: {fileID: 10102, guid: 0000000000000000e000000000000000, type: 0}
m_FontSize: 18
m_FontStyle: 1
m_BestFit: 0
m_MinSize: 0
m_MaxSize: 40
m_Alignment: 4
m_AlignByGeometry: 0
m_RichText: 1
m_HorizontalOverflow: 0
m_VerticalOverflow: 0
m_LineSpacing: 1
m_Text: Hit <TAB> To Show PingTool
--- !u!1 &7349017088681708274
GameObject:
m_ObjectHideFlags: 0
Expand Down
Loading

0 comments on commit 5378d4d

Please sign in to comment.