-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFPSCounter.cs
More file actions
86 lines (64 loc) · 1.87 KB
/
FPSCounter.cs
File metadata and controls
86 lines (64 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
public class FPSCounter : MonoBehaviour {
public const string PLAYERPREF_FPS_KEY = "dbg_fps";
// FPS Counter
public bool loadFPSSettings = true;
public bool fpsCounterEnabled = true;
public bool fpsDisplayEnabled = false;
public float fpsCounterTime = 0f;
public float fpsCounterTimeMax = 1f;
public float fpsCounterValue = 0f;
public Text fpsCounterText;
/**
* Variables For Low FPS Event
*/
public float minFPSValue = 60f;
public UnityEvent onFPSDropDown;
private void Start()
{
if (fpsCounterText != null)
{
fpsCounterText.text = "";
}
if (loadFPSSettings && PlayerPrefs.HasKey(PLAYERPREF_FPS_KEY))
{
fpsDisplayEnabled = (PlayerPrefs.GetInt(PLAYERPREF_FPS_KEY) == 1);
}
}
public void ToggleFPSDisplay()
{
PlayerPrefs.SetInt(PLAYERPREF_FPS_KEY, (!fpsDisplayEnabled ? 1 : 0));
fpsDisplayEnabled = !fpsDisplayEnabled;
if (fpsCounterText != null)
{
fpsCounterText.text = "";
}
}
// Update is called once per frame
void Update () {
if (fpsCounterEnabled)
{
fpsCounterTime += Time.deltaTime;
fpsCounterValue++;
if (fpsCounterTime >= fpsCounterTimeMax)
{
float fps = (Mathf.Floor(fpsCounterValue / fpsCounterTimeMax));
if (fpsCounterText != null && fpsDisplayEnabled)
{
fpsCounterText.text = fps.ToString();
}
// Checks Low FPS
if (minFPSValue > fps)
{
onFPSDropDown.Invoke();
}
fpsCounterTime -= fpsCounterTimeMax;
fpsCounterValue = 0f;
}
}
}
}