17 [Tooltip(
"Reference to TextMesh component where the FPS should be displayed.")]
19 private TextMesh textMesh;
24 [Tooltip(
"Reference to uGUI text component where the FPS should be displayed.")]
26 private Text uGUIText;
31 [Tooltip(
"How many frames should we consider into our average calculation?")]
34 private int frameRange = 60;
36 private int averageFps;
38 private int[] fpsBuffer;
39 private int fpsBufferIndex;
41 private static readonly
string[] StringsFrom00To99 =
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" 65 UpdateTextDisplay(averageFps);
71 private void InitBuffer()
75 textMesh = GetComponent<TextMesh>();
80 uGUIText = GetComponent<Text>();
88 fpsBuffer =
new int[frameRange];
96 private void UpdateTextDisplay(
int fps)
98 string displayString = StringsFrom00To99[Mathf.Clamp(fps, 0, 99)];
100 if (textMesh != null)
102 textMesh.text = displayString;
105 if (uGUIText != null)
107 uGUIText.text = displayString;
114 private void UpdateFrameBuffer()
116 fpsBuffer[fpsBufferIndex++] = (int)(1f / Time.unscaledDeltaTime);
118 if (fpsBufferIndex >= frameRange)
127 private void CalculateFps()
131 for (
int i = 0; i < frameRange; i++)
133 int fps = fpsBuffer[i];
137 averageFps = sum / frameRange;