Tanoda
FPSCounter.cs
Go to the documentation of this file.
1// MIT license - license_nvjob.txt
2// FPS counter with buffer v1.2 - https://github.com/nvjob/Unity-FPS-Counter
3// #NVJOB Nicholas Veselov (independent developer) - https://nvjob.pro, http://nvjob.dx.am
4// The script is written according to the calculation of the average value in mathematics.
5
6
7using UnityEngine;
8using System.Collections;
9using System.Collections.Generic;
10using UnityEngine.UI;
11
12
15
16
17public class FPSCounter : MonoBehaviour
18{
20
21
22 public Text counterText;
23 public Transform graph;
24 public int frameUpdate = 60;
25 public int highestPossibleFPS = 300;
26 public int maxLines = 60;
27 public float graphUpdate = 0.5f;
28 public Color graphColor = new Color(1, 1, 1, 0.5f);
29
30 //---------------------------------
31
32 float ofsetX;
33 int curCount, lineCount;
34
35 //---------------------------------
36
37 static Transform stTr;
38 static GameObject[] stLines;
39 static int stNumLines;
40
41
43
44
45 void Awake()
46 {
47 //---------------------------------
48
49 stTr = graph;
50 stNumLines = maxLines * 2;
51 stLines = new GameObject[stNumLines];
52
53 for (int i = 0; i < stNumLines; i++)
54 {
55 stLines[i] = new GameObject();
56 stLines[i].SetActive(false);
57 stLines[i].name = "Line_" + i;
58 stLines[i].transform.parent = stTr;
59 Image img = stLines[i].AddComponent<Image>();
60 img.color = graphColor;
61 }
62
63 StartCoroutine(DrawGraph());
64
65 //---------------------------------
66 }
67
68
70
71
72 void Update()
73 {
74 //---------------------------------
75
76 curCount = StFPS.Counter(frameUpdate, Time.deltaTime);
77 counterText.text = "FPS: " + curCount.ToString();
78
79 //---------------------------------
80 }
81
82
84
85
86 IEnumerator DrawGraph()
87 {
88 //---------------------------------
89
90 while (true)
91 {
92 yield return new WaitForSeconds(graphUpdate);
93
94 GameObject obj = GiveLine();
95 Image img = obj.GetComponent<Image>();
96 img.rectTransform.anchorMin = new Vector2(ofsetX, 0);
97 img.rectTransform.anchorMax = new Vector2(ofsetX + 0.01f, 1.0f / highestPossibleFPS * curCount);
98 img.rectTransform.offsetMax = img.rectTransform.offsetMin = new Vector2(0, 0);
99 obj.SetActive(true);
100
101 if (lineCount++ > maxLines)
102 {
103 foreach (Transform child in graph) child.gameObject.SetActive(false);
104 ofsetX = lineCount = 0;
105 }
106 else ofsetX += 0.02f;
107 }
108
109 //---------------------------------
110 }
111
112
114
115
116 static GameObject GiveLine()
117 {
118 //---------------------------------
119
120 for (int i = 0; i < stNumLines; i++) if (!stLines[i].activeSelf) return stLines[i];
121 return null;
122
123 //---------------------------------
124 }
125
126
128}
129
130
133
134
135public static class StFPS
136{
138
139
140 static List<float> fpsBuffer = new List<float>();
141 static float fpsB;
142 static int fps;
143
144
146
147
148 public static int Counter(int frameUpdate, float deltaTime)
149 {
150 //---------------------------------
151
152 int fpsBCount = fpsBuffer.Count;
153
154 if (fpsBCount <= frameUpdate) fpsBuffer.Add(1.0f / Time.deltaTime);
155 else
156 {
157 for (int f = 0; f < fpsBCount; f++) fpsB += fpsBuffer[f];
158 fpsBuffer = new List<float> { 1.0f / Time.deltaTime };
159 fpsB = fpsB / fpsBCount;
160 fps = Mathf.RoundToInt(fpsB);
161 fpsB = 0;
162 }
163
164 return fps;
165
166 //---------------------------------
167 }
168
169
171}
System.Drawing.Image Image
Definition: TestScript.cs:37
UnityEngine.Color Color
Definition: TestScript.cs:32
Transform graph
Definition: FPSCounter.cs:23
int maxLines
Definition: FPSCounter.cs:26
int frameUpdate
Definition: FPSCounter.cs:24
int highestPossibleFPS
Definition: FPSCounter.cs:25
float graphUpdate
Definition: FPSCounter.cs:27
Color graphColor
Definition: FPSCounter.cs:28
Text counterText
Definition: FPSCounter.cs:22