Skip to content

Commit

Permalink
display fps
Browse files Browse the repository at this point in the history
  • Loading branch information
BBpezsgo committed Jan 11, 2025
1 parent 7599db8 commit b1aad16
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
22 changes: 17 additions & 5 deletions Assets/Mono/Client/HUDManager.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Diagnostics.CodeAnalysis;
using UnityEngine;
using UnityEngine.UIElements;
Expand All @@ -6,20 +7,31 @@ public class HUDManager : MonoBehaviour
{
[SerializeField, NotNull] UIDocument? _ui = default;

[NotNull] Label? _label = default;
[NotNull] Label? _labelResources = default;
[NotNull] Label? _labelFps = default;

float _refreshAt = default;
float _maxDeltaTime = default;

void Start()
{
_label = _ui.rootVisualElement.Q<Label>("label-resources");
_labelResources = _ui.rootVisualElement.Q<Label>("label-resources");
_labelFps = _ui.rootVisualElement.Q<Label>("label-fps");
}

void Update()
{
float now = Time.time;
_maxDeltaTime = MathF.Max(_maxDeltaTime, Time.deltaTime);
if (now < _refreshAt) return;
_refreshAt = now + 2f;
if (!PlayerManager.TryGetLocalPlayer(out Player localPlayer)) return;
_label.text = localPlayer.Resources.ToString();
_refreshAt = now + 1f;

_labelFps.text = MathF.Abs(_maxDeltaTime) < 0.01f ? "-" : MathF.Round(1f / _maxDeltaTime).ToString();
_maxDeltaTime = 0f;

if (PlayerManager.TryGetLocalPlayer(out Player localPlayer))
{
_labelResources.text = localPlayer.Resources.ToString();
}
}
}
4 changes: 4 additions & 0 deletions Assets/UI/HUD.uxml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@
<ui:Label text="Resources:" style="margin-top: 0; margin-right: 8px; margin-bottom: 0; margin-left: 0; padding-top: 0; padding-right: 0; padding-bottom: 0; padding-left: 0;" />
<ui:Label text="0" name="label-resources" style="padding-top: 0; padding-right: 0; padding-bottom: 0; padding-left: 0; margin-top: 0; margin-right: 0; margin-bottom: 0; margin-left: 0;" />
</ui:VisualElement>
<ui:VisualElement style="flex-direction: row; background-color: rgb(48, 48, 48); border-left-color: rgb(0, 0, 0); border-right-color: rgb(0, 0, 0); border-top-color: rgb(38, 38, 38); border-bottom-color: rgb(0, 0, 0); margin-top: 4px; margin-right: 4px; margin-bottom: 4px; margin-left: 4px; padding-top: 4px; padding-right: 4px; padding-bottom: 4px; padding-left: 4px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px;">
<ui:Label text="FPS:" style="margin-top: 0; margin-right: 8px; margin-bottom: 0; margin-left: 0; padding-top: 0; padding-right: 0; padding-bottom: 0; padding-left: 0;" />
<ui:Label text="0" name="label-fps" style="padding-top: 0; padding-right: 0; padding-bottom: 0; padding-left: 0; margin-top: 0; margin-right: 0; margin-bottom: 0; margin-left: 0;" />
</ui:VisualElement>
</ui:VisualElement>
</ui:UXML>

0 comments on commit b1aad16

Please sign in to comment.