Skip to content

Commit 07dc76e

Browse files
committed
fix to prevent IndexOutOfRangeException
This prevents the IndexOutOfRangeException exception when Unity returns NaN for the refresh rate. It also displays the refresh rate with up to 3 decimal places so that rates like 29.97, 59.94, and 144.003 will show up correctly and not truncated.
1 parent 1cdf316 commit 07dc76e

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

Runtime/Advanced/G_AdvancedData.cs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ public class G_AdvancedData : MonoBehaviour, IMovable, IModifiableState
8787
"Hz"
8888
};
8989

90+
#if UNITY_2022_2_OR_NEWER
91+
private SortedList<RefreshRate, string> m_refreshRates = new SortedList<RefreshRate, string>();
92+
#endif
93+
9094
#endregion
9195

9296
#region Methods -> Unity Callbacks
@@ -96,6 +100,20 @@ private void Awake()
96100
Init();
97101
}
98102

103+
private string RefreshRateToString( Resolution resolution )
104+
{
105+
#if UNITY_2022_2_OR_NEWER
106+
if( !m_refreshRates.TryGetValue( resolution.refreshRateRatio, out string refreshRate ) )
107+
{
108+
refreshRate = resolution.refreshRateRatio.value.ToString( "0.###" );
109+
m_refreshRates.Add( resolution.refreshRateRatio, refreshRate );
110+
}
111+
return refreshRate;
112+
#else
113+
return resolution.refreshRate.ToStringNonAlloc();
114+
#endif
115+
}
116+
99117
private void Update()
100118
{
101119
m_deltaTime += Time.unscaledDeltaTime;
@@ -107,13 +125,7 @@ private void Update()
107125

108126
m_sb.Append( m_windowStrings[ 0 ] ).Append( Screen.width.ToStringNonAlloc() )
109127
.Append( m_windowStrings[ 1 ] ).Append( Screen.height.ToStringNonAlloc() )
110-
.Append( m_windowStrings[ 2 ] ).Append(
111-
#if UNITY_2022_2_OR_NEWER
112-
((int)Screen.currentResolution.refreshRateRatio.value).ToStringNonAlloc()
113-
#else
114-
Screen.currentResolution.refreshRate.ToStringNonAlloc()
115-
#endif
116-
)
128+
.Append( m_windowStrings[ 2 ] ).Append( RefreshRateToString( Screen.currentResolution ) )
117129
.Append( m_windowStrings[ 3 ] )
118130
.Append( m_windowStrings[ 4 ] ).Append( ((int) Screen.dpi).ToStringNonAlloc() )
119131
.Append( m_windowStrings[ 5 ] );
@@ -333,11 +345,7 @@ private void Init()
333345
+ "x"
334346
+ res.height
335347
+ "@"
336-
#if UNITY_2022_2_OR_NEWER
337-
+ ((int)Screen.currentResolution.refreshRateRatio.value).ToStringNonAlloc()
338-
#else
339-
+ res.refreshRate
340-
#endif
348+
+ RefreshRateToString( res )
341349
+ "Hz";
342350

343351
m_operatingSystemText.text

Runtime/Util/G_Intstring.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public static void Dispose()
9898
/// </returns>
9999
public static string ToStringNonAlloc( this int value )
100100
{
101-
if( value < 0 && -value <= m_negativeBuffer.Length )
101+
if( value < 0 && value >= -m_negativeBuffer.Length )
102102
{
103103
return m_negativeBuffer[ -value - 1 ];
104104
}

0 commit comments

Comments
 (0)