- FPSCounter.cs
- Utility /
- Standard Assets /
- Assets /
- project /
1 using System;
2 using UnityEngine;
3 using UnityEngine.UI;
4
5 namespace UnityStandardAssets.Utility
6 {
7 [RequireComponent(typeof (Text))]
8 public class FPSCounter : MonoBehaviour
9 {
10 const float fpsMeasurePeriod = 0.5f;
11 private int m_FpsAccumulator = 0;
12 private float m_FpsNextPeriod = 0;
13 private int m_CurrentFps;
14 const string display = "{0} FPS";
15 private Text m_Text;
16
17
18 private void Start()
19 {
20 m_FpsNextPeriod = Time.realtimeSinceStartup + fpsMeasurePeriod;
21 m_Text = GetComponent<Text>();
22 }
23
24
25 private void Update()
26 {
27 // measure average frames per second
28 m_FpsAccumulator++;
29 if (Time.realtimeSinceStartup > m_FpsNextPeriod)
30 {
31 m_CurrentFps = (int) (m_FpsAccumulator/fpsMeasurePeriod);
32 m_FpsAccumulator = 0;
33 m_FpsNextPeriod += fpsMeasurePeriod;
34 m_Text.text = string.Format(display, m_CurrentFps);
35 }
36 }
37 }
38 }
2 using UnityEngine;
3 using UnityEngine.UI;
4
5 namespace UnityStandardAssets.Utility
6 {
7 [RequireComponent(typeof (Text))]
8 public class FPSCounter : MonoBehaviour
9 {
10 const float fpsMeasurePeriod = 0.5f;
11 private int m_FpsAccumulator = 0;
12 private float m_FpsNextPeriod = 0;
13 private int m_CurrentFps;
14 const string display = "{0} FPS";
15 private Text m_Text;
16
17
18 private void Start()
19 {
20 m_FpsNextPeriod = Time.realtimeSinceStartup + fpsMeasurePeriod;
21 m_Text = GetComponent<Text>();
22 }
23
24
25 private void Update()
26 {
27 // measure average frames per second
28 m_FpsAccumulator++;
29 if (Time.realtimeSinceStartup > m_FpsNextPeriod)
30 {
31 m_CurrentFps = (int) (m_FpsAccumulator/fpsMeasurePeriod);
32 m_FpsAccumulator = 0;
33 m_FpsNextPeriod += fpsMeasurePeriod;
34 m_Text.text = string.Format(display, m_CurrentFps);
35 }
36 }
37 }
38 }