BITKit/Src/Unity/Scripts/Components/ShowProfiler.cs

61 lines
2.0 KiB
C#
Raw Normal View History

2023-11-30 00:25:43 +08:00
using System;
2023-06-05 19:57:17 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace BITKit
{
2023-09-01 14:35:05 +08:00
public class ShowProfiler : MonoBehaviour
2023-06-05 19:57:17 +08:00
{
[Header(Constant.Header.Settings)]
[SubclassSelector, SerializeReference] public References pingAddress;
2023-08-23 01:59:26 +08:00
[SerializeField] private IntervalUpdate fpsInterval;
2023-06-05 19:57:17 +08:00
[Header(Constant.Header.Output)]
2023-09-01 14:35:05 +08:00
[SerializeReference,SubclassSelector] private IProvider fpsOutput;
[SerializeReference,SubclassSelector] private IProvider pingOutput;
[SerializeReference,SubclassSelector] private IProvider resolutionOutput;
[SerializeReference,SubclassSelector] private IProvider frameRateOutput;
2024-03-31 23:31:00 +08:00
[SerializeReference, SubclassSelector] private INetClient clientPing;
2023-08-11 23:57:37 +08:00
private readonly DeltaTimer timer = new();
private Ping ping;
2023-06-05 19:57:17 +08:00
[Header(Constant.Header.InternalVariables)]
2023-08-11 23:57:37 +08:00
private int frameRate;
2023-08-23 01:59:26 +08:00
2023-08-11 23:57:37 +08:00
private void Update()
2023-06-05 19:57:17 +08:00
{
timer.Update();
frameRate = timer;
2023-08-23 01:59:26 +08:00
resolutionOutput?.Set(Screen.currentResolution.ToString());
if (fpsInterval.AllowUpdate)
fpsOutput.Set((string)timer);
2024-03-31 23:31:00 +08:00
if (clientPing is not null)
{
pingOutput.Set(clientPing.Ping.ToString());
}
else if (pingOutput is not null)
2023-06-05 19:57:17 +08:00
{
2023-11-30 00:25:43 +08:00
switch (ping)
2023-06-05 19:57:17 +08:00
{
2023-11-30 00:25:43 +08:00
case null:
ping = new Ping(pingAddress);
break;
case var x when x.isDone:
pingOutput.Set(ping.time.ToString());
ping = new Ping(pingAddress);
break;
2023-06-05 19:57:17 +08:00
}
}
2023-08-23 01:59:26 +08:00
frameRateOutput?.Set(Application.targetFrameRate is -1 or 0
? "Unlimited"
: Application.targetFrameRate.ToString());
2023-06-05 19:57:17 +08:00
}
2023-11-30 00:25:43 +08:00
private void OnDestroy()
{
ping?.DestroyPing();
}
2023-06-05 19:57:17 +08:00
}
}