ShowFpsOnGUI.cs 907 B

12345678910111213141516171819202122232425262728293031323334353637
  1. using UnityEngine;
  2. using System.Collections;
  3. public class ShowFpsOnGUI : MonoBehaviour {
  4. public float fpsMeasuringDelta = 2.0f;
  5. private float timePassed;
  6. private int m_FrameCount = 0;
  7. private float m_FPS = 0.0f;
  8. private void Start() {
  9. timePassed = 0.0f;
  10. }
  11. private void Update() {
  12. m_FrameCount = m_FrameCount + 1;
  13. timePassed = timePassed + Time.deltaTime;
  14. if (timePassed > fpsMeasuringDelta) {
  15. m_FPS = m_FrameCount / timePassed;
  16. timePassed = 0.0f;
  17. m_FrameCount = 0;
  18. }
  19. }
  20. private void OnGUI() {
  21. GUIStyle bb = new GUIStyle();
  22. bb.normal.background = null;
  23. bb.normal.textColor = new Color(1.0f, 0.5f, 0.0f);
  24. bb.fontSize = 30;
  25. //居中显示FPS
  26. GUI.Label(new Rect(Screen.width - 200, 2, 200, 200), "FPS: " + m_FPS.ToString("f2"), bb);
  27. }
  28. }