Unity 框架

FPS.cs 986B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /****************************************************
  2. 文件:FPS.cs
  3. 作者:陶长春
  4. 邮箱:376248129@qq.com
  5. 日期:2025年3月12日 6:03:14
  6. UnityVersion: 2021.3.13f1
  7. 功能:FPS工具类
  8. *****************************************************/
  9. using TFramework;
  10. using System.Collections.Generic;
  11. using UnityEngine;
  12. public class FPS
  13. {
  14. private float _lastTime = 0f;
  15. private int _fpsCount = 0;
  16. public TAction<float> onUpdateFPS;
  17. /// <summary>
  18. /// 当前帧率
  19. /// </summary>
  20. public float CurrentFPS { get; private set; }
  21. public FPS()
  22. {
  23. Main.Instance.onUpdate += Update;
  24. }
  25. private void Update()
  26. {
  27. float time = Time.realtimeSinceStartup - _lastTime;
  28. _fpsCount += 1;
  29. if (time > 1)
  30. {
  31. CurrentFPS = _fpsCount / time;
  32. _fpsCount = 0;
  33. _lastTime = Time.realtimeSinceStartup;
  34. onUpdateFPS?.Invoke(CurrentFPS);
  35. }
  36. }
  37. }