BITFALL/Assets/BITKit/Unity/Scripts/Components/ShowProfiler.cs

55 lines
1.8 KiB
C#
Raw Normal View History

2023-11-21 18:05:18 +08:00
using System;
2023-08-12 01:43:24 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace BITKit
{
2023-08-27 02:58:19 +08:00
public class ShowProfiler : MonoBehaviour
2023-08-12 01:43:24 +08:00
{
[Header(Constant.Header.Settings)]
[SubclassSelector, SerializeReference] public References pingAddress;
2023-08-23 01:59:40 +08:00
[SerializeField] private IntervalUpdate fpsInterval;
2023-08-12 01:43:24 +08:00
[Header(Constant.Header.Output)]
2023-08-27 02:58:19 +08:00
[SerializeReference,SubclassSelector] private IProvider fpsOutput;
[SerializeReference,SubclassSelector] private IProvider pingOutput;
[SerializeReference,SubclassSelector] private IProvider resolutionOutput;
[SerializeReference,SubclassSelector] private IProvider frameRateOutput;
2023-08-12 01:43:24 +08:00
private readonly DeltaTimer timer = new();
private Ping ping;
[Header(Constant.Header.InternalVariables)]
private int frameRate;
2023-08-23 01:59:40 +08:00
2023-08-12 01:43:24 +08:00
private void Update()
{
timer.Update();
frameRate = timer;
2023-08-23 01:59:40 +08:00
resolutionOutput?.Set(Screen.currentResolution.ToString());
if (fpsInterval.AllowUpdate)
fpsOutput.Set((string)timer);
2023-11-21 18:05:18 +08:00
if (pingOutput is not null)
2023-08-12 01:43:24 +08:00
{
2023-11-21 18:05:18 +08:00
switch (ping)
2023-08-12 01:43:24 +08:00
{
2023-11-21 18:05:18 +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-08-12 01:43:24 +08:00
}
}
2023-08-23 01:59:40 +08:00
frameRateOutput?.Set(Application.targetFrameRate is -1 or 0
? "Unlimited"
: Application.targetFrameRate.ToString());
2023-08-12 01:43:24 +08:00
}
2023-11-21 18:05:18 +08:00
private void OnDestroy()
{
ping?.DestroyPing();
}
2023-08-12 01:43:24 +08:00
}
}