1+ using UnityEngine ;
2+ using UnityEngine . UI ;
3+
4+ /// <summary>
5+ /// Script di debug per visualizzare informazioni sulla risoluzione e DPI
6+ /// Utile per diagnosticare problemi di scalatura su diversi PC
7+ /// </summary>
8+ public class ResolutionDebugUI : MonoBehaviour
9+ {
10+ [ Header ( "UI Elements" ) ]
11+ public Text debugText ;
12+ public Canvas debugCanvas ;
13+
14+ [ Header ( "Settings" ) ]
15+ public bool showDebugInfo = true ;
16+ public KeyCode toggleKey = KeyCode . F1 ;
17+
18+ private string debugInfo = "" ;
19+
20+ void Start ( )
21+ {
22+ if ( debugCanvas == null )
23+ {
24+ CreateDebugUI ( ) ;
25+ }
26+
27+ if ( showDebugInfo )
28+ {
29+ debugCanvas . enabled = true ;
30+ }
31+ }
32+
33+ void CreateDebugUI ( )
34+ {
35+ // Crea Canvas per debug
36+ GameObject canvasGO = new GameObject ( "Debug Canvas" ) ;
37+ debugCanvas = canvasGO . AddComponent < Canvas > ( ) ;
38+ debugCanvas . renderMode = RenderMode . ScreenSpaceOverlay ;
39+ debugCanvas . sortingOrder = 999 ;
40+
41+ CanvasScaler scaler = canvasGO . AddComponent < CanvasScaler > ( ) ;
42+ scaler . uiScaleMode = CanvasScaler . ScaleMode . ScaleWithScreenSize ;
43+ scaler . referenceResolution = new Vector2 ( 1920 , 1080 ) ;
44+
45+ canvasGO . AddComponent < GraphicRaycaster > ( ) ;
46+
47+ // Crea testo debug
48+ GameObject textGO = new GameObject ( "Debug Text" ) ;
49+ textGO . transform . SetParent ( debugCanvas . transform , false ) ;
50+
51+ debugText = textGO . AddComponent < Text > ( ) ;
52+ debugText . font = Resources . GetBuiltinResource < Font > ( "Arial.ttf" ) ;
53+ debugText . fontSize = 14 ;
54+ debugText . color = Color . white ;
55+
56+ // Posiziona il testo in alto a sinistra
57+ RectTransform rectTransform = debugText . GetComponent < RectTransform > ( ) ;
58+ rectTransform . anchorMin = new Vector2 ( 0 , 1 ) ;
59+ rectTransform . anchorMax = new Vector2 ( 0 , 1 ) ;
60+ rectTransform . pivot = new Vector2 ( 0 , 1 ) ;
61+ rectTransform . anchoredPosition = new Vector2 ( 10 , - 10 ) ;
62+ rectTransform . sizeDelta = new Vector2 ( 400 , 300 ) ;
63+
64+ // Aggiungi background
65+ GameObject bgGO = new GameObject ( "Background" ) ;
66+ bgGO . transform . SetParent ( debugText . transform , false ) ;
67+ bgGO . transform . SetAsFirstSibling ( ) ;
68+
69+ Image bg = bgGO . AddComponent < Image > ( ) ;
70+ bg . color = new Color ( 0 , 0 , 0 , 0.7f ) ;
71+
72+ RectTransform bgRect = bg . GetComponent < RectTransform > ( ) ;
73+ bgRect . anchorMin = Vector2 . zero ;
74+ bgRect . anchorMax = Vector2 . one ;
75+ bgRect . offsetMin = new Vector2 ( - 5 , - 5 ) ;
76+ bgRect . offsetMax = new Vector2 ( 5 , 5 ) ;
77+
78+ DontDestroyOnLoad ( canvasGO ) ;
79+ }
80+
81+ void Update ( )
82+ {
83+ if ( Input . GetKeyDown ( toggleKey ) )
84+ {
85+ showDebugInfo = ! showDebugInfo ;
86+ debugCanvas . enabled = showDebugInfo ;
87+ }
88+
89+ if ( showDebugInfo && debugText != null )
90+ {
91+ UpdateDebugInfo ( ) ;
92+ debugText . text = debugInfo ;
93+ }
94+ }
95+
96+ void UpdateDebugInfo ( )
97+ {
98+ debugInfo = "=== RESOLUTION DEBUG INFO ===\n " ;
99+ debugInfo += $ "Screen Resolution: { Screen . width } x{ Screen . height } \n ";
100+ debugInfo += $ "DPI: { Screen . dpi } \n ";
101+ debugInfo += $ "Fullscreen Mode: { Screen . fullScreenMode } \n ";
102+ debugInfo += $ "Native Resolution: { Display . main . systemWidth } x{ Display . main . systemHeight } \n ";
103+ debugInfo += $ "Refresh Rate: { Screen . currentResolution . refreshRate } Hz\n ";
104+
105+ // Info DPI Manager se disponibile
106+ if ( DPIManager . Instance != null )
107+ {
108+ debugInfo += "\n === DPI MANAGER ===\n " ;
109+ debugInfo += $ "Current DPI: { DPIManager . Instance . currentDPI } \n ";
110+ debugInfo += $ "DPI Scale: { DPIManager . Instance . dpiScale : F2} \n ";
111+ debugInfo += $ "Scale Factor: { DPIManager . Instance . GetScaleFactor ( ) : F2} \n ";
112+ }
113+
114+ // Info Resolution Manager se disponibile
115+ ResolutionManager resManager = FindObjectOfType < ResolutionManager > ( ) ;
116+ if ( resManager != null )
117+ {
118+ debugInfo += "\n === RESOLUTION MANAGER ===\n " ;
119+ debugInfo += $ "Current: { resManager . currentResolution } \n ";
120+ debugInfo += $ "Native: { resManager . nativeResolution } \n ";
121+ debugInfo += $ "Aspect Ratio: { resManager . aspectRatio : F2} \n ";
122+ }
123+
124+ // Info Canvas se disponibile
125+ Canvas [ ] canvases = FindObjectsOfType < Canvas > ( ) ;
126+ if ( canvases . Length > 0 )
127+ {
128+ debugInfo += "\n === CANVAS INFO ===\n " ;
129+ debugInfo += $ "Number of Canvases: { canvases . Length } \n ";
130+
131+ foreach ( Canvas canvas in canvases )
132+ {
133+ if ( canvas . name != "Debug Canvas" )
134+ {
135+ CanvasScaler scaler = canvas . GetComponent < CanvasScaler > ( ) ;
136+ if ( scaler != null )
137+ {
138+ debugInfo += $ "{ canvas . name } : { scaler . uiScaleMode } , Ref: { scaler . referenceResolution } \n ";
139+ }
140+ }
141+ }
142+ }
143+
144+ debugInfo += $ "\n === SYSTEM INFO ===\n ";
145+ debugInfo += $ "OS: { SystemInfo . operatingSystem } \n ";
146+ debugInfo += $ "GPU: { SystemInfo . graphicsDeviceName } \n ";
147+ debugInfo += $ "GPU Memory: { SystemInfo . graphicsMemorySize } MB\n ";
148+
149+ debugInfo += $ "\n === CONTROLS ===\n ";
150+ debugInfo += $ "Press { toggleKey } to toggle this debug info\n ";
151+ debugInfo += $ "Press Alt+Enter to toggle fullscreen\n ";
152+ }
153+ }
0 commit comments