AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
FpsDisplay.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 using UnityEngine;
5 using UnityEngine.UI;
6 
7 namespace HoloToolkit.Unity
8 {
12  public class FpsDisplay : MonoBehaviour
13  {
17  [Tooltip("Reference to TextMesh component where the FPS should be displayed.")]
18  [SerializeField]
19  private TextMesh textMesh;
20 
24  [Tooltip("Reference to uGUI text component where the FPS should be displayed.")]
25  [SerializeField]
26  private Text uGUIText;
27 
31  [Tooltip("How many frames should we consider into our average calculation?")]
32  [SerializeField]
33  [Range(1, 300)]
34  private int frameRange = 60;
35 
36  private int averageFps;
37 
38  private int[] fpsBuffer;
39  private int fpsBufferIndex;
40 
41  private static readonly string[] StringsFrom00To99 =
42  {
43  "00", "01", "02", "03", "04", "05", "06", "07", "08", "09",
44  "10", "11", "12", "13", "14", "15", "16", "17", "18", "19",
45  "20", "21", "22", "23", "24", "25", "26", "27", "28", "29",
46  "30", "31", "32", "33", "34", "35", "36", "37", "38", "39",
47  "40", "41", "42", "43", "44", "45", "46", "47", "48", "49",
48  "50", "51", "52", "53", "54", "55", "56", "57", "58", "59",
49  "60", "61", "62", "63", "64", "65", "66", "67", "68", "69",
50  "70", "71", "72", "73", "74", "75", "76", "77", "78", "79",
51  "80", "81", "82", "83", "84", "85", "86", "87", "88", "89",
52  "90", "91", "92", "93", "94", "95", "96", "97", "98", "99"
53  };
54 
55  private void Start()
56  {
57  InitBuffer();
58  }
59 
60  private void Update()
61  {
62  UpdateFrameBuffer();
63  CalculateFps();
64 
65  UpdateTextDisplay(averageFps);
66  }
67 
71  private void InitBuffer()
72  {
73  if (textMesh == null)
74  {
75  textMesh = GetComponent<TextMesh>();
76  }
77 
78  if (uGUIText == null)
79  {
80  uGUIText = GetComponent<Text>();
81  }
82 
83  if (frameRange <= 0)
84  {
85  frameRange = 1;
86  }
87 
88  fpsBuffer = new int[frameRange];
89  fpsBufferIndex = 0;
90  }
91 
96  private void UpdateTextDisplay(int fps)
97  {
98  string displayString = StringsFrom00To99[Mathf.Clamp(fps, 0, 99)];
99 
100  if (textMesh != null)
101  {
102  textMesh.text = displayString;
103  }
104 
105  if (uGUIText != null)
106  {
107  uGUIText.text = displayString;
108  }
109  }
110 
114  private void UpdateFrameBuffer()
115  {
116  fpsBuffer[fpsBufferIndex++] = (int)(1f / Time.unscaledDeltaTime);
117 
118  if (fpsBufferIndex >= frameRange)
119  {
120  fpsBufferIndex = 0;
121  }
122  }
123 
127  private void CalculateFps()
128  {
129  int sum = 0;
130 
131  for (int i = 0; i < frameRange; i++)
132  {
133  int fps = fpsBuffer[i];
134  sum += fps;
135  }
136 
137  averageFps = sum / frameRange;
138  }
139  }
140 }
Simple Behaviour which calculates the average frames per second over a number of frames and shows the...
Definition: FpsDisplay.cs:12