Game.Settings.ScreenHelper
Assembly: Assembly-CSharp (Cities: Skylines 2 main assembly)
Namespace: Game.Settings
Type: static class
Base: System.Object
Summary:
Utility helper for querying and normalizing screen/display settings. Provides access to the current resolution and display mode, builds and caches lists of available resolutions (both full and simplified sets), finds the closest available resolution to a requested sample, detects multiple displays, and maps between game DisplayMode and Unity FullScreenMode. Intended for use from the main (Unity) thread when interacting with Screen APIs.
Fields
-
private static System.Collections.Generic.List<UnityEngine.DisplayInfo> m_DisplayInfos
Used as a reusable buffer for Screen.GetDisplayLayout(...) to detect multiple displays. The list is cleared each time HasMultipleDisplay() is called to avoid allocations. -
private static ScreenResolution[] resolutions
Cached full list of available resolutions (one entry per entry in Screen.resolutions), sorted by width desc, height desc, refresh rate desc. Rebuilt by RebuildResolutions(). -
private static ScreenResolution[] simpleResolutions
Cached simplified resolution list built by grouping full resolutions by (width, height, rounded refresh rate) and selecting the entry with the best (smallest) refreshRateDelta per group. Used when a simplified UI is desired (HideAdditionalResolutionOption() compares lengths).
Properties
-
public static ScreenResolution currentResolution { get; }
Returns a ScreenResolution representing the current runtime resolution. If the application is not fullscreen (Screen.fullScreen == false), it constructs a ScreenResolution using Screen.width and Screen.height and uses Screen.currentResolution.refreshRateRatio for refresh rate. If fullscreen, it returns a ScreenResolution constructed from Screen.currentResolution. The returned ScreenResolution is not modified by this property. -
private static ScreenResolution[] resolutions { get; set; }
Private backing property for the full resolution cache. See RebuildResolutions() for how it is populated. -
private static ScreenResolution[] simpleResolutions { get; set; }
Private backing property for the simplified resolution cache. -
public static DisplayMode currentDisplayMode { get; }
Computed property that maps UnityEngine.Screen.fullScreenMode to the game's DisplayMode enum: - FullScreenMode.ExclusiveFullScreen => DisplayMode.Fullscreen
- FullScreenMode.FullScreenWindow => DisplayMode.FullscreenWindow
- FullScreenMode.Windowed => DisplayMode.Window
- default => DisplayMode.Window
Constructors
static ScreenHelper()
Static constructor. Initializes the m_DisplayInfos list and calls RebuildResolutions() to populate resolution caches on first access.
Methods
-
public static ScreenResolution[] GetAvailableResolutions(bool all)
Returns either the simplified resolution list (simpleResolutions) when all == false, or the full list (resolutions) when all == true. Use this to populate resolution dropdowns depending on whether you want to show every refresh-rate variant or only a single entry per width/height/rounded-refresh-rate. -
public static void RebuildResolutions()
Rebuilds both resolutions and simpleResolutions: - resolutions: created from Screen.resolutions, each converted to ScreenResolution, then ordered by width desc, height desc, refreshRate.value desc.
- simpleResolutions: groups resolutions by (width, height, rounded refreshRate) and selects the entry in each group that has the smallest refreshRateDelta (an internal metric in ScreenResolution). This reduces entries so UI doesn't show multiple near-identical refresh-rate variants.
Call this when the available system resolutions may have changed or on initialization to ensure caches are up-to-date.
public static ScreenResolution GetClosestAvailable(ScreenResolution sample, bool all)
Given a sample ScreenResolution, finds and returns the closest available resolution from the selected list (simple or full). Selection logic:- Primary matching metric: L1 (Manhattan) distance on width and height: abs(width - sample.width) + abs(height - sample.height).
- If width and height are equal between candidates, picks the candidate with the closest refresh rate (by absolute difference in refreshRate.value).
-
If no available resolution has width/height > 0, returns the provided sample (after calling result.Sanitize()). The returned resolution is sanitized before being returned.
-
public static bool HideAdditionalResolutionOption()
Returns true when the simplified list is the same length as the full list (simpleResolutions.Length == resolutions.Length). Useful to decide whether to show a UI control for choosing “All refresh rates” vs “Simple list”. -
public static bool HasMultipleDisplay()
Clears m_DisplayInfos, calls Screen.GetDisplayLayout(m_DisplayInfos), and returns true if more than one display is present. Uses a reusable list to avoid allocations. -
public static UnityEngine.FullScreenMode GetFullscreenMode(DisplayMode displayMode)
Maps game DisplayMode to UnityEngine.FullScreenMode: - DisplayMode.Fullscreen => FullScreenMode.ExclusiveFullScreen
- DisplayMode.FullscreenWindow => FullScreenMode.FullScreenWindow
- DisplayMode.Window => FullScreenMode.Windowed
- default => FullScreenMode.Windowed
Usage Example
// Example: refresh caches, pick a closest resolution and apply it.
public void ApplyDesiredResolution()
{
// Ensure resolution caches are current (call on main thread)
ScreenHelper.RebuildResolutions();
// Example sample resolution (types provided by the game)
var sample = new ScreenResolution { width = 1920, height = 1080, refreshRate = new RefreshRate(60.0f) };
// Pick from simplified list (all = false) or full list (all = true)
var best = ScreenHelper.GetClosestAvailable(sample, all: false);
// Map to Unity FullScreenMode and apply (Screen.SetResolution / other game APIs)
var mode = ScreenHelper.GetFullscreenMode(DisplayMode.FullscreenWindow);
// Example using Unity's API (may differ depending on game wrappers):
Screen.SetResolution(best.width, best.height, mode, (int)Math.Round(best.refreshRate.value));
}
Notes and remarks: - This helper is designed to be used on the Unity main thread since it calls UnityEngine.Screen APIs. - ScreenResolution, RefreshRate, and DisplayMode are game-specific types used by Cities: Skylines 2. Inspect their definitions for details such as refreshRateDelta and Sanitize() behavior. - RebuildResolutions() does allocations when converting Screen.resolutions into ScreenResolution instances; call it sparingly (e.g., on settings open or when display changes) to avoid runtime GC spikes.