ArabDesert/Assets/ReimajoBoothAssets/Extras/PerfMeasurement/GlobalProfileHandler.cs

71 lines
2.3 KiB
C#

#define AVERAGE_OUTPUT
using UdonSharp;
using UnityEngine;
/// <summary>
/// Code from MerlinVR, edited by Reimajo.
/// Can be publicly found here: https://gist.github.com/MerlinVR/2da80b29361588ddb556fd8d3f3f47b5
/// For this script only:
/// MIT License
/// Copyright (c) 2021 Merlin
/// </summary>
namespace NOT_ReimajoBoothAssets //namespace was added here so that it doesn't conflict with your own classes
{
[DefaultExecutionOrder(1000000000)]
public class GlobalProfileHandler : UdonSharpBehaviour
{
public UnityEngine.UI.Text _timeText;
private GlobalProfileKickoff _kickoff;
private void Start()
{
_kickoff = GetComponent<GlobalProfileKickoff>();
}
private int _currentFrame = -1;
private float _elapsedTime = 0f;
#if AVERAGE_OUTPUT
private float _measuredTimeTotal = 0f;
private int _measuredTimeFrameCount = 0;
private const int MEASURE_FRAME_AMOUNT = 45;
#endif
private void FixedUpdate()
{
if (_currentFrame != Time.frameCount)
{
_elapsedTime = 0f;
_currentFrame = Time.frameCount;
}
if (_kickoff)
_elapsedTime += (float)_kickoff.stopwatch.Elapsed.TotalSeconds * 1000f;
}
private void Update()
{
if (_currentFrame != Time.frameCount) // FixedUpdate didn't run this frame, so reset the time
_elapsedTime = 0f;
_elapsedTime += (float)_kickoff.stopwatch.Elapsed.TotalSeconds * 1000f;
}
private void LateUpdate()
{
_elapsedTime += (float)_kickoff.stopwatch.Elapsed.TotalSeconds * 1000f;
#if AVERAGE_OUTPUT
if (_measuredTimeFrameCount >= MEASURE_FRAME_AMOUNT)
{
float averageTime = _measuredTimeTotal / _measuredTimeFrameCount;
_timeText.text = $"{averageTime:F4}ms";
_measuredTimeTotal = 0f;
_measuredTimeFrameCount = 0;
}
_measuredTimeTotal += _elapsedTime;
_measuredTimeFrameCount += 1;
#else
_timeText1.text = $"Update time:\n{_elapsedTime:F4}ms";
_timeText2.text = $"Update time:\n{_elapsedTime:F4}ms";
#endif
}
}
}