1
- using UnityEngine ;
1
+ using System . Text ;
2
+ using UnityEngine ;
2
3
using UnityEngine . UI ;
3
4
4
5
namespace Zigurous . Debug
@@ -16,30 +17,83 @@ public class FPSDisplay : MonoBehaviour
16
17
public Text displayText ;
17
18
18
19
/// <summary>
19
- /// How often per second the framerate display updates .
20
+ /// The text format of the framerate display.
20
21
/// </summary>
21
- [ Tooltip ( "How often per second the framerate display updates." ) ]
22
+ public string displayFormat { get ; protected set ; }
23
+
24
+ /// <summary>
25
+ /// The amount of seconds between display updates.
26
+ /// </summary>
27
+ [ Tooltip ( "The amount of seconds between display updates." ) ]
22
28
public float refreshRate = 1.0f ;
23
29
30
+ /// <summary>
31
+ /// The number of decimal digits to display.
32
+ /// </summary>
33
+ [ Tooltip ( "The number of decimal digits to display." ) ]
34
+ [ SerializeField ]
35
+ private int _decimals = 0 ;
36
+
37
+ /// <summary>
38
+ /// The number of decimal digits to display.
39
+ /// </summary>
40
+ public int decimals
41
+ {
42
+ get => _decimals ;
43
+ set
44
+ {
45
+ _decimals = value ;
46
+ SetDisplayFormat ( value ) ;
47
+ }
48
+ }
49
+
24
50
/// <summary>
25
51
/// The time of the next framerate update.
26
52
/// </summary>
27
53
protected float _nextUpdate ;
28
54
55
+ private void OnValidate ( )
56
+ {
57
+ SetDisplayFormat ( this . decimals ) ;
58
+ }
59
+
60
+ private void Awake ( )
61
+ {
62
+ SetDisplayFormat ( this . decimals ) ;
63
+ }
64
+
29
65
private void Update ( )
30
66
{
31
67
if ( Time . unscaledTime > _nextUpdate )
32
68
{
33
- int fps = ( int ) ( 1.0f / Time . unscaledDeltaTime ) ;
34
- UpdateDisplay ( fps ) ;
35
69
_nextUpdate = Time . unscaledTime + this . refreshRate ;
70
+
71
+ float fps = 1.0f / Time . unscaledDeltaTime ;
72
+ UpdateDisplay ( fps ) ;
36
73
}
37
74
}
38
75
39
- protected virtual void UpdateDisplay ( int fps )
76
+ private void SetDisplayFormat ( int decimals )
77
+ {
78
+ int length = 1 + decimals ;
79
+ if ( decimals > 0 ) length ++ ;
80
+
81
+ StringBuilder stringBuilder = new StringBuilder ( length ) ;
82
+ stringBuilder . Append ( '0' ) ;
83
+
84
+ if ( decimals > 0 )
85
+ {
86
+ stringBuilder . Append ( '.' ) ;
87
+ stringBuilder . Insert ( 2 , "0" , decimals ) ;
88
+ }
89
+
90
+ this . displayFormat = stringBuilder . ToString ( ) ;
91
+ }
92
+
93
+ protected virtual void UpdateDisplay ( float fps )
40
94
{
41
95
if ( this . displayText != null ) {
42
- this . displayText . text = fps . ToString ( ) ;
96
+ this . displayText . text = fps . ToString ( this . displayFormat ) ;
43
97
}
44
98
}
45
99
0 commit comments