FPS.cs 606 B

123456789101112131415161718192021222324252627
  1. using UnityEngine;
  2. public class FPS : MonoBehaviour
  3. {
  4. float deltaTime = 0.0f;
  5. void Update()
  6. {
  7. deltaTime += (Time.deltaTime - deltaTime) * 0.1f;
  8. }
  9. void OnGUI()
  10. {
  11. int w = Screen.width, h = Screen.height;
  12. GUIStyle style = new GUIStyle();
  13. Rect rect = new Rect(0, 0, w, h * 2 / 100);
  14. style.alignment = TextAnchor.UpperLeft;
  15. style.fontSize = h * 2 / 100;
  16. style.normal.textColor = new Color (1, 1, 1, 1.0f);
  17. float msec = deltaTime * 1000.0f;
  18. float fps = 1.0f / deltaTime;
  19. string text = string.Format("{0:0.0} ms ({1:0.} fps)", msec, fps);
  20. GUI.Label(rect, text, style);
  21. }
  22. }