DebuggerComponent.FpsCounter.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //------------------------------------------------------------
  2. // Game Framework
  3. // Copyright © 2013-2021 loyalsoft. All rights reserved.
  4. // Homepage: http://www.game7000.com/
  5. // Feedback: http://www.game7000.com/
  6. //------------------------------------------------------------
  7. namespace UnityGameFramework.Runtime
  8. {
  9. public sealed partial class DebuggerComponent : GameFrameworkComponent
  10. {
  11. private sealed class FpsCounter
  12. {
  13. private float m_UpdateInterval;
  14. private float m_CurrentFps;
  15. private int m_Frames;
  16. private float m_Accumulator;
  17. private float m_TimeLeft;
  18. public FpsCounter(float updateInterval)
  19. {
  20. if (updateInterval <= 0f)
  21. {
  22. Log.Error("Update interval is invalid.");
  23. return;
  24. }
  25. m_UpdateInterval = updateInterval;
  26. Reset();
  27. }
  28. public float UpdateInterval
  29. {
  30. get
  31. {
  32. return m_UpdateInterval;
  33. }
  34. set
  35. {
  36. if (value <= 0f)
  37. {
  38. Log.Error("Update interval is invalid.");
  39. return;
  40. }
  41. m_UpdateInterval = value;
  42. Reset();
  43. }
  44. }
  45. public float CurrentFps
  46. {
  47. get
  48. {
  49. return m_CurrentFps;
  50. }
  51. }
  52. public void Update(float elapseSeconds, float realElapseSeconds)
  53. {
  54. m_Frames++;
  55. m_Accumulator += realElapseSeconds;
  56. m_TimeLeft -= realElapseSeconds;
  57. if (m_TimeLeft <= 0f)
  58. {
  59. m_CurrentFps = m_Accumulator > 0f ? m_Frames / m_Accumulator : 0f;
  60. m_Frames = 0;
  61. m_Accumulator = 0f;
  62. m_TimeLeft += m_UpdateInterval;
  63. }
  64. }
  65. private void Reset()
  66. {
  67. m_CurrentFps = 0f;
  68. m_Frames = 0;
  69. m_Accumulator = 0f;
  70. m_TimeLeft = 0f;
  71. }
  72. }
  73. }
  74. }