首先明確兩個概念:
Screen.currentResolution - 指的是設備分辨率,比如:電腦上指的就是電腦的屏幕分辨率,手機上指的就是手機的屏幕分辨率。
Screen.width,Screen.height - 指的是使用該值的窗口的寬高,比如:在Scene窗口使用指的就是Scene窗口的寬高,在Game窗口使用,即運行編輯器時使用,指的就是Game窗口的寬高,在自定義的Window使用指的就是自定義的Window的寬高。
定義如下窗口,用來測試上述結論:

代碼:
1 using UnityEngine; 2 using UnityEditor; 3 4 5 public class CustomWindow : EditorWindow 6 { 7 static CustomWindow mWin; 8 9 [MenuItem("自定義窗口/MyWin")] 10 static void ShowWin() 11 { 12 mWin = GetWindow<CustomWindow>(); 13 mWin.titleContent = new GUIContent("MyWin"); 14 mWin.minSize = new Vector2(400, 200); 15 } 16 17 void OnGUI() 18 { 19 if (GUILayout.Button("ShowInfo", GUILayout.Width(200), GUILayout.Height(50))) 20 { 21 Debug.Log("Screen.width = " + Screen.width + " Screen.height = " + Screen.height + " | currentResolution = " + Screen.currentResolution); 22 } 23 } 24 }
打印結果:

在游戲開發過程中,我們更多關注的是Game視圖下的屏幕寬高。假如,我們想在自定的Window中獲取Game視圖的屏幕寬高,該如何做呢?
下面是NGUI-3.9.4中提供的獲取Game視圖屏幕寬高的方法:
1 static int mSizeFrame = -1; 2 static System.Reflection.MethodInfo s_GetSizeOfMainGameView; 3 static Vector2 mGameSize = Vector2.one; 4 5 /// <summary> 6 /// Size of the game view cannot be retrieved from Screen.width and Screen.height when the game view is hidden. 7 /// </summary> 8 9 static public Vector2 screenSize 10 { 11 get 12 { 13 int frame = Time.frameCount; 14 15 if (mSizeFrame != frame || !Application.isPlaying) 16 { 17 mSizeFrame = frame; 18 19 if (s_GetSizeOfMainGameView == null) 20 { 21 System.Type type = System.Type.GetType("UnityEditor.GameView,UnityEditor"); 22 s_GetSizeOfMainGameView = type.GetMethod("GetSizeOfMainGameView", 23 System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static); 24 } 25 mGameSize = (Vector2)s_GetSizeOfMainGameView.Invoke(null, null); 26 } 27 return mGameSize; 28 } 29 }
對於移動設備,個人理解Screen.width,Screen.height和Screen.currentResolution值是一樣的(並未實際驗證)。為了安全起見,個人建議,編輯器下和移動設備上最好用Screen.width,Screen.height統一起來。NGUI也是這個思路:
1 #if UNITY_EDITOR 2 static int mSizeFrame = -1; 3 static System.Reflection.MethodInfo s_GetSizeOfMainGameView; 4 static Vector2 mGameSize = Vector2.one; 5 6 /// <summary> 7 /// Size of the game view cannot be retrieved from Screen.width and Screen.height when the game view is hidden. 8 /// </summary> 9 10 static public Vector2 screenSize 11 { 12 get 13 { 14 int frame = Time.frameCount; 15 16 if (mSizeFrame != frame || !Application.isPlaying) 17 { 18 mSizeFrame = frame; 19 20 if (s_GetSizeOfMainGameView == null) 21 { 22 System.Type type = System.Type.GetType("UnityEditor.GameView,UnityEditor"); 23 s_GetSizeOfMainGameView = type.GetMethod("GetSizeOfMainGameView", 24 System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static); 25 } 26 mGameSize = (Vector2)s_GetSizeOfMainGameView.Invoke(null, null); 27 } 28 return mGameSize; 29 } 30 } 31 #else 32 /// <summary> 33 /// Size of the game view cannot be retrieved from Screen.width and Screen.height when the game view is hidden. 34 /// </summary> 35 36 static public Vector2 screenSize { get { return new Vector2(Screen.width, Screen.height); } } 37 #endif
這里再提供一個折疊屏手機展開或者閉合后屏幕適配的一個思路:
Update中檢測Screen的size是否發生變化,如果發生變化就去刷新一下錨定設置。
代碼:
1 using UnityEngine; 2 3 public class CheckScreen : MonoBehaviour 4 { 5 int screenWidth, screenHeight; 6 void Start() 7 { 8 screenWidth = Screen.width; 9 screenHeight = Screen.height; 10 } 11 12 13 void Update() 14 { 15 if (screenWidth != Screen.width || screenHeight != Screen.height) 16 { 17 screenWidth = Screen.width; 18 screenHeight = Screen.height; 19 OnScreenChange(); 20 } 21 } 22 23 void OnScreenChange() 24 { 25 //發送消息-處理適配之 前 的一些操作 26 //UI適配 27 //3D適配 28 //其他... 29 //發送消息-處理適配之 后 的一些操作 30 } 31 }
