How can I show the fps as text on a canvas in my unity game?
Two separate things, FPS can be calculated by doing “amount of frames / time elapsed”, I suggest you have some time between the checks otherwise you will end calculating FPS each frame, and this isn’t necessary, and now that we have the fps we need to display it, create a reference for your text on the canvas and change their value
public class Test23 : MonoBehaviour
{
[SerializeField]
private Text myTextOnCanvas; // TMP if you're using text mesh pro
[SerializeField]
private float updateRate = .5f;
private float timeElapsed;
private int frameCounter;
private void Update()
{
timeElapsed += Time.deltaTime;
frameCounter++;
if (timeElapsed < updateRate)
return;
var currentFPS = frameCounter / timeElapsed;
myTextOnCanvas.text = $"{currentFPS:F2}";
frameCounter = 0;
timeElapsed -= updateRate;
}
}
1 Like
@Nefisto There is written that the namespace Text could not be found.
It works now I forgot to put using UnityEngine.UI
3 Likes