Skip to content

feat: bad network conditions warning [MTT-3242] #632

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
May 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
a12826a
Adding text warning to UI when UTP RTT is over a threshold
LPLafontaineB Apr 27, 2022
64995d4
adding pulsing to warning
LPLafontaineB Apr 28, 2022
d30bf8c
adding comment
LPLafontaineB Apr 28, 2022
4d863e5
Merge branch 'develop' into feature/bad-network-condition-warning
LPLafontaineB Apr 28, 2022
1aabf73
Added coloring of stats text
LPLafontaineB Apr 28, 2022
d62897d
Merge branch 'develop' into feature/bad-network-condition-warning
LPLafontaineB May 5, 2022
82c75d5
reduced increase in fontsize for bad network conditions warning
LPLafontaineB May 10, 2022
1bc8aaf
Merge branch 'develop' into feature/bad-network-condition-warning
LPLafontaineB May 11, 2022
186b9cd
made debug overlay canvas scale with screen resolution
LPLafontaineB May 12, 2022
3a68934
removing font size increase for bad conditions warning text
LPLafontaineB May 12, 2022
6b51417
Apply suggestions from code review
LPLafontaineB May 12, 2022
2311d07
formatting
LPLafontaineB May 12, 2022
4613300
Merge branch 'feature/bad-network-condition-warning' of https://githu…
LPLafontaineB May 12, 2022
00918b4
updating changelog
LPLafontaineB May 13, 2022
86fc33f
resetting scale after parenting UIText to canvas
LPLafontaineB May 17, 2022
51a707d
Merge branch 'develop' into feature/bad-network-condition-warning
LPLafontaineB May 17, 2022
6b5f057
moving the scaling fix to NetworkOverlay
LPLafontaineB May 17, 2022
e3857f2
Using TMP for NetworkLatencyWarning and increasing font size
LPLafontaineB May 17, 2022
1288950
extracted duplicated methods to NetworkOverlay
LPLafontaineB May 17, 2022
1713157
increased width of Vetical Layout Group in Debug Overlay Canvas
LPLafontaineB May 17, 2022
e4b6a2d
further increased width of vertical layout group
LPLafontaineB May 19, 2022
449c9c7
made font smaller
LPLafontaineB May 19, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions Assets/BossRoom/Prefabs/Debug Overlay Canvas.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ RectTransform:
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 0, y: 0, z: 0}
m_ConstrainProportionsScale: 0
m_Children:
- {fileID: 2084502874807427418}
m_Father: {fileID: 0}
Expand Down Expand Up @@ -72,10 +73,10 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 0cd44c1031e13a943bb63640046fad76, type: 3}
m_Name:
m_EditorClassIdentifier:
m_UiScaleMode: 0
m_UiScaleMode: 1
m_ReferencePixelsPerUnit: 100
m_ScaleFactor: 1
m_ReferenceResolution: {x: 800, y: 600}
m_ReferenceResolution: {x: 1920, y: 1080}
m_ScreenMatchMode: 0
m_MatchWidthOrHeight: 0
m_PhysicalUnit: 3
Expand Down Expand Up @@ -127,14 +128,15 @@ RectTransform:
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: 5075973321638144936}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
m_AnchoredPosition: {x: 40, y: 40}
m_SizeDelta: {x: 240, y: 400}
m_SizeDelta: {x: 400, y: 400}
m_Pivot: {x: 0, y: 0}
--- !u!114 &3464894946384537378
MonoBehaviour:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using Unity.Netcode;
Expand All @@ -10,7 +11,7 @@ namespace Unity.Multiplayer.Samples.BossRoom.Editor
{
public class NetworkLatencyWarning : MonoBehaviour
{
Text m_LatencyText;
TextMeshProUGUI m_LatencyText;
bool m_LatencyTextCreated;

Color m_TextColor = Color.red;
Expand Down Expand Up @@ -66,19 +67,7 @@ void CreateLatencyText()
Assert.IsNotNull(NetworkOverlay.Instance,
"No NetworkOverlay object part of scene. Add NetworkOverlay prefab to bootstrap scene!");

var statUI = new GameObject("UI Latency Warning Text");

m_LatencyText = statUI.AddComponent<Text>();
m_LatencyText.font = Font.CreateDynamicFontFromOSFont("Arial", 24);
m_LatencyText.horizontalOverflow = HorizontalWrapMode.Overflow;
m_LatencyText.alignment = TextAnchor.MiddleLeft;
m_LatencyText.raycastTarget = false;
m_LatencyText.resizeTextForBestFit = true;

m_LatencyText.text = "Network Latency Enabled";

var statUIRectTransform = statUI.GetComponent<RectTransform>();
NetworkOverlay.Instance.AddToUI(statUIRectTransform);
NetworkOverlay.Instance.AddTextToUI("UI Latency Warning Text", "Network Latency Enabled", out m_LatencyText);
}
}
}
17 changes: 17 additions & 0 deletions Assets/BossRoom/Scripts/Infrastructure/Editor/NetworkOverlay.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using TMPro;
using UnityEngine;
using UnityEngine.UI;

Expand All @@ -18,6 +19,21 @@ void Awake()
DontDestroyOnLoad(this);
}

public void AddTextToUI(string gameObjectName, string defaultText, out TextMeshProUGUI textComponent)
{
var rootGO = new GameObject(gameObjectName);
textComponent = rootGO.AddComponent<TextMeshProUGUI>();
textComponent.fontSize = 28;
textComponent.text = defaultText;
textComponent.horizontalAlignment = HorizontalAlignmentOptions.Left;
textComponent.verticalAlignment = VerticalAlignmentOptions.Middle;
textComponent.raycastTarget = false;
textComponent.autoSizeTextContainer = true;

var rectTransform = rootGO.GetComponent<RectTransform>();
AddToUI(rectTransform);
}

public void AddToUI(RectTransform displayTransform)
{
if (m_VerticalLayoutTransform == null)
Expand All @@ -28,6 +44,7 @@ public void AddToUI(RectTransform displayTransform)
displayTransform.sizeDelta = new Vector2(100f, 24f);
displayTransform.SetParent(m_VerticalLayoutTransform);
displayTransform.SetAsFirstSibling();
displayTransform.localScale = Vector3.one;
}

void CreateDebugCanvas()
Expand Down
47 changes: 30 additions & 17 deletions Assets/BossRoom/Scripts/Infrastructure/NetworkStats.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,17 @@ public ExponentialMovingAverageCalculator(float average)
const float k_PingIntervalSeconds = 0.1f;
const float k_MaxWindowSize = k_MaxWindowSizeSeconds / k_PingIntervalSeconds;

// Some games are less sensitive to latency than others. For fast-paced games, latency above 100ms becomes a challenge for players while for others 500ms is fine. It's up to you to establish those thresholds.
const float k_StrugglingNetworkConditionsRTTThreshold = 130;
const float k_BadNetworkConditionsRTTThreshold = 200;

ExponentialMovingAverageCalculator m_BossRoomRTT = new ExponentialMovingAverageCalculator(0);
ExponentialMovingAverageCalculator m_UtpRTT = new ExponentialMovingAverageCalculator(0);

float m_LastPingTime;
TextMeshProUGUI m_TextStat;
TextMeshProUGUI m_TextHostType;
TextMeshProUGUI m_TextBadNetworkConditions;

// When receiving pong client RPCs, we need to know when the initiating ping sent it so we can calculate its individual RTT
int m_CurrentRTTPingId;
Expand Down Expand Up @@ -86,23 +91,9 @@ void CreateNetworkStatsText()
"No NetworkOverlay object part of scene. Add NetworkOverlay prefab to bootstrap scene!");

string hostType = IsHost ? "Host" : IsClient ? "Client" : "Unknown";
InitializeTextLine($"Type: {hostType}", out m_TextHostType);
InitializeTextLine("No Stat", out m_TextStat);
}

void InitializeTextLine(string defaultText, out TextMeshProUGUI textComponent)
{
var rootGO = new GameObject("UI Stat Text");
textComponent = rootGO.AddComponent<TextMeshProUGUI>();
textComponent.fontSize = 24;
textComponent.text = defaultText;
textComponent.horizontalAlignment = HorizontalAlignmentOptions.Left;
textComponent.verticalAlignment = VerticalAlignmentOptions.Middle;
textComponent.raycastTarget = false;
textComponent.autoSizeTextContainer = true;

var rectTransform = rootGO.GetComponent<RectTransform>();
Editor.NetworkOverlay.Instance.AddToUI(rectTransform);
Editor.NetworkOverlay.Instance.AddTextToUI("UI Host Type Text", $"Type: {hostType}", out m_TextHostType);
Editor.NetworkOverlay.Instance.AddTextToUI("UI Stat Text", "No Stat", out m_TextStat);
Editor.NetworkOverlay.Instance.AddTextToUI("UI Bad Conditions Text", "", out m_TextBadNetworkConditions);
}

void FixedUpdate()
Expand All @@ -124,6 +115,28 @@ void FixedUpdate()
if (m_TextStat != null)
{
m_TextToDisplay = $"RTT: {(m_BossRoomRTT.Average * 1000).ToString("0")} ms;\nUTP RTT {m_UtpRTT.Average.ToString("0")} ms";
if (m_UtpRTT.Average > k_BadNetworkConditionsRTTThreshold)
{
m_TextStat.color = Color.red;
}
else if (m_UtpRTT.Average > k_StrugglingNetworkConditionsRTTThreshold)
{
m_TextStat.color = Color.yellow;
}
else
{
m_TextStat.color = Color.white;
}
}

if (m_TextBadNetworkConditions != null)
{
// Right now, we only base this warning on UTP's RTT metric, but in the future we could watch for packet loss as well, or other metrics.
// This could be a simple icon instead of doing heavy string manipulations.
m_TextBadNetworkConditions.text = m_UtpRTT.Average > k_BadNetworkConditionsRTTThreshold ? "Bad Network Conditions Detected!" : "";
var color = Color.red;
color.a = Mathf.PingPong(Time.time, 1f);
m_TextBadNetworkConditions.color = color;
}
}
else
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
## [Unreleased] - yyyy-mm-dd

### Added
feat: bad network conditions warning [MTT-3242] (#632)
feat: adding RNSM (Runtime Network Stats Monitor) to boss room [MTT-3267] (#621)
Added basic automated testing and CI (#484) (#487) (#639)
feat: connection feedback + IP connection window [MTT-2315] [MTT-3234] (#613)
Expand Down