FPSComponent.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using UnityEngine;
  2. using System.Collections;
  3. namespace Engine.Component
  4. {
  5. /// <summary>统计帧频</summary>
  6. public class FPSComponent : BaseComponent
  7. {
  8. /// <summary>当前帧数</summary>
  9. private int nFrameCt = 60;
  10. /// <summary>统计时间</summary>
  11. private int nStartTime = 0;
  12. /// <summary>统计中的值</summary>
  13. private int nFrameIng = 0;
  14. /// <summary>渲染帧函数</summary>
  15. void Update()
  16. {
  17. if (CStaticMethod.SystemAfterTime () - nStartTime >= 1000)
  18. {
  19. nFrameCt = nFrameIng;
  20. nStartTime = CStaticMethod.SystemAfterTime ();
  21. nFrameIng = 0;
  22. }
  23. else
  24. {
  25. nFrameIng++;
  26. }
  27. }
  28. void OnGUI()
  29. {
  30. GUIStyle testStyle = new GUIStyle();
  31. testStyle.normal.background = null;
  32. if (nFrameCt >= 40)
  33. {
  34. testStyle.normal.textColor = Color.white;
  35. }
  36. else if (nFrameCt >= 30)
  37. {
  38. testStyle.normal.textColor = Color.green;
  39. }
  40. else {
  41. testStyle.normal.textColor = Color.red;
  42. }
  43. testStyle.fontSize = 30;
  44. GUI.Label(new Rect(10, 10, 200, 60), "frame:" + nFrameCt, testStyle);
  45. }
  46. }
  47. }