Last active
November 20, 2022 11:08
-
-
Save MerlinVR/2da80b29361588ddb556fd8d3f3f47b5 to your computer and use it in GitHub Desktop.
Basic global profiling scripts for Udon. Throw one of each script on a game object that has a TMP UI text somewhere in its children.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MIT License | |
// Copyright (c) 2021 Merlin | |
using UdonSharp; | |
using UnityEngine; | |
[DefaultExecutionOrder(1000000000)] | |
public class GlobalProfileHandler : UdonSharpBehaviour | |
{ | |
TMPro.TextMeshProUGUI timeText; | |
GlobalProfileKickoff kickoff; | |
private void Start() | |
{ | |
kickoff = GetComponent<GlobalProfileKickoff>(); | |
timeText = GetComponentInChildren<TMPro.TextMeshProUGUI>(); | |
} | |
int currentFrame = -1; | |
float elapsedTime = 0f; | |
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; | |
timeText.text = $"Update time:\n{elapsedTime:F4}ms"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MIT License | |
// Copyright (c) 2021 Merlin | |
using UdonSharp; | |
using UnityEngine; | |
[DefaultExecutionOrder(-1000000000)] | |
public class GlobalProfileKickoff : UdonSharpBehaviour | |
{ | |
[System.NonSerialized] | |
public System.Diagnostics.Stopwatch stopwatch; | |
private void Start() | |
{ | |
stopwatch = new System.Diagnostics.Stopwatch(); | |
} | |
private void FixedUpdate() | |
{ | |
stopwatch.Restart(); | |
} | |
private void Update() | |
{ | |
stopwatch.Restart(); | |
} | |
private void LateUpdate() | |
{ | |
stopwatch.Restart(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's an update with PostLateUpdate support as well: