参考链接:
https://www.cnblogs.com/zhaolaosan/p/15259240.html
0.
在这篇文章的基础上进行适配:
https://www.cnblogs.com/lyh916/p/11831213.html
1.异形屏适配
通过PackageManager搜索并安装Device Simulator(需要unity版本2019.3以上)
通过菜单栏Window/General/Device Simulator打开
Game视图:
iPhoneX:
解决思路:
调整界面根节点的anchor,使其处于安全区域内
1 using UnityEngine; 2 3 [DisallowMultipleComponent] 4 [RequireComponent(typeof(RectTransform))] 5 public class SafeAreaRect : MonoBehaviour 6 { 7 private RectTransform rectTransform; 8 private ScreenOrientation lastScreenOrientation; 9 10 private void Awake() 11 { 12 rectTransform = GetComponent<RectTransform>(); 13 lastScreenOrientation = Screen.orientation; 14 ChangeRect(); 15 } 16 17 void Update() 18 { 19 if (lastScreenOrientation != Screen.orientation) 20 { 21 lastScreenOrientation = Screen.orientation; 22 ChangeRect(); 23 } 24 //print(Screen.safeArea); 25 //print(Screen.orientation); 26 //print(Application.isMobilePlatform); 27 //print(Screen.width + "_" + Screen.height); 28 } 29 30 private void ChangeRect() 31 { 32 ScreenOrientation screenOrientation = Screen.orientation; 33 Rect safeArea = Screen.safeArea; 34 if (screenOrientation == ScreenOrientation.Portrait || screenOrientation == ScreenOrientation.PortraitUpsideDown) //竖屏 35 { 36 rectTransform.anchorMin = new Vector2(safeArea.x / Screen.width, safeArea.y / Screen.height); 37 rectTransform.anchorMax = new Vector2((safeArea.x + safeArea.width) / Screen.width, (safeArea.y + safeArea.height) / Screen.height); 38 } 39 else //横屏 40 { 41 rectTransform.anchorMin = new Vector2(safeArea.x / Screen.width, safeArea.y / Screen.height); 42 rectTransform.anchorMax = new Vector2((safeArea.x + safeArea.width) / Screen.width, (safeArea.y + safeArea.height) / Screen.height); 43 } 44 } 45 }
2.屏幕旋转
可以这样设置:
对应的代码设置:
1 private void Awake() 2 { 3 Screen.autorotateToLandscapeLeft = true; 4 Screen.autorotateToLandscapeRight = true; 5 Screen.autorotateToPortrait = false; 6 Screen.autorotateToPortraitUpsideDown = false; 7 Screen.orientation = ScreenOrientation.AutoRotation; 8 }