DebuggerComponent.FpsCounter.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //------------------------------------------------------------
  2. // Game Framework v3.x
  3. // Copyright © 2013-2017 Jiang Yin. All rights reserved.
  4. // Homepage: http://gameframework.cn/
  5. // Feedback: mailto:jiangyin@gameframework.cn
  6. //------------------------------------------------------------
  7. using System;
  8. namespace UnityGameFramework.Runtime
  9. {
  10. public partial class DebuggerComponent
  11. {
  12. private sealed class FpsCounter
  13. {
  14. private float m_UpdateInterval;
  15. private float m_CurrentFps;
  16. private int m_Frames;
  17. private float m_Accumulator;
  18. private float m_TimeLeft;
  19. public FpsCounter(float updateInterval)
  20. {
  21. if (updateInterval <= 0f)
  22. {
  23. throw new ArgumentOutOfRangeException("Update interval is invalid.");
  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. throw new ArgumentOutOfRangeException("Update interval is invalid.");
  39. }
  40. m_UpdateInterval = value;
  41. Reset();
  42. }
  43. }
  44. public float CurrentFps
  45. {
  46. get
  47. {
  48. return m_CurrentFps;
  49. }
  50. }
  51. public void Update(float elapseSeconds, float realElapseSeconds)
  52. {
  53. m_Frames++;
  54. m_Accumulator += realElapseSeconds;
  55. m_TimeLeft -= realElapseSeconds;
  56. if (m_TimeLeft <= 0f)
  57. {
  58. m_CurrentFps = m_Accumulator > 0f ? m_Frames / m_Accumulator : 0f;
  59. m_Frames = 0;
  60. m_Accumulator = 0f;
  61. m_TimeLeft += m_UpdateInterval;
  62. }
  63. }
  64. private void Reset()
  65. {
  66. m_CurrentFps = 0f;
  67. m_Frames = 0;
  68. m_Accumulator = 0f;
  69. m_TimeLeft = 0f;
  70. }
  71. }
  72. }
  73. }