-
Notifications
You must be signed in to change notification settings - Fork 168
/
NetcodePanelStats.cs
143 lines (123 loc) · 4.45 KB
/
NetcodePanelStats.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using UnityEngine;
using UnityEngine.UIElements;
using UnityEngine.UIElements.Experimental;
namespace Unity.NetCode
{
/// <summary>
/// Shows diagnostics of the performance, and Ping.
/// </summary>
public class NetcodePanelStats : MonoBehaviour
{
private Button m_BackButton;
private VisualElement m_BackIcon;
private VisualElement m_InfoPanel;
private VisualElement m_Main;
private bool m_PanelHidden;
private Label m_PlayersLabel;
private Label m_PingLabel;
private Label m_EntitiesLabel;
private Label m_GhostsLabel;
private Label m_FPSLabel;
private Label m_QualityLabel;
private Label m_SystemsLabel;
private Label m_KeyToggleLabel;
public static NetcodePanelStats Instance { private set; get; }
private float PanelHeight => m_InfoPanel.layout.height + 10f;
private void Awake()
{
#if !(DEVELOPMENT_BUILD || UNITY_EDITOR)
DestroyImmediate(gameObject);
#endif
if (Instance == null)
{
Instance = this;
}
else
{
DestroyImmediate(gameObject);
}
}
public void ToggleNetcodePanel()
{
if (m_PanelHidden)
ShowPanel();
else
HidePanel();
}
private void OnEnable()
{
var root = GetComponent<UIDocument>().rootVisualElement;
m_Main = root.Q<VisualElement>("info-panel");
m_InfoPanel = root.Q<VisualElement>("info-panel-body");
m_BackButton = root.Q<Button>("info-panel-back-button");
m_FPSLabel = root.Q<Label>("fps-value");
m_QualityLabel = root.Q<Label>("quality-value");
m_PlayersLabel = root.Q<Label>("players-value");
m_PingLabel = root.Q<Label>("ping-value");
m_EntitiesLabel = root.Q<Label>("entities-value");
m_GhostsLabel = root.Q<Label>("ghosts-value");
m_SystemsLabel = root.Q<Label>("systems-value");
m_BackIcon = root.Q<VisualElement>("back-icon");
m_BackButton.clicked += ToggleNetcodePanel;
m_InfoPanel.RegisterCallback<GeometryChangedEvent>(OnGeometryChanged);
Disable();
}
private void OnGeometryChanged(GeometryChangedEvent evt)
{
// Hide panel by default
HidePanel();
}
private void HidePanel()
{
m_InfoPanel.experimental.animation.Position(new Vector3(0, -PanelHeight), 500).Ease(Easing.OutCubic)
.OnCompleted(() => { m_PanelHidden = true; });
m_BackIcon.experimental.animation.Rotation(Quaternion.Euler(0, 0, 180f), 300).Ease(Easing.OutQuad);
}
private void ShowPanel()
{
m_InfoPanel.experimental.animation.Position(new Vector3(0, 0), 500).Ease(Easing.OutCubic)
.OnCompleted(() => { m_PanelHidden = false; });
m_BackIcon.experimental.animation.Rotation(Quaternion.Euler(0, 0, 0), 300).Ease(Easing.OutQuad);
}
public void Enable()
{
if (m_Main.style.display != DisplayStyle.Flex)
m_Main.style.display = DisplayStyle.Flex;
}
public void Disable()
{
if (m_Main.style.display != DisplayStyle.None)
m_Main.style.display = DisplayStyle.None;
}
public void SetNumberOfPlayers(int number)
{
m_PlayersLabel.text = number.ToString();
}
public void SetFPSLabel(float fps)
{
m_FPSLabel.text = $"{((int) fps).ToString()} FPS";
}
public void SetPingLabel(int estimatedRTT, int deviationRTT)
{
m_PingLabel.text = estimatedRTT < 1000
? $"{estimatedRTT}±{deviationRTT}ms"
: $"~{estimatedRTT + deviationRTT / 2:0}ms";
}
public void SetSystemsLabel(uint value)
{
m_SystemsLabel.text = value.ToString("n0");
}
public void SetEntitiesLabel(int value)
{
m_EntitiesLabel.text = value.ToString("n0");
}
public void SetGhostsLabel(GhostCount value)
{
m_GhostsLabel.text = value.GhostCountOnServer.ToString("n0");
}
public void UpdateQualityLabel(string quality)
{
m_QualityLabel.text = quality;
}
}
}