lyh916

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

参考链接:

https://www.cnblogs.com/zhaolaosan/p/15259240.html

https://blog.csdn.net/linxinfa/article/details/113103369?spm=1001.2014.3001.5501

https://blog.csdn.net/hyx18775752051/article/details/107672114?spm=1001.2101.3001.6650.5&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-5.no_search_link&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-5.no_search_link

https://blog.csdn.net/Vitens/article/details/111053028?spm=1001.2101.3001.6650.7&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-7.no_search_link&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-7.no_search_link

 

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     }

 

3.全屏背景图适配

原则:

a.背景图要铺满整个屏幕,不要留空,不然效果不好

16:9

21:9

b.图片要按比例拉伸

 

 1 using UnityEngine;
 2 using UnityEngine.UI;
 3 
 4 public class BGScaler : MonoBehaviour
 5 {
 6     public RectTransform canvasRectTra;
 7     private RawImage rawImage;
 8     private RectTransform rectTra;
 9 
10     void Start()
11     {
12         rawImage = GetComponent<RawImage>();
13         rectTra = GetComponent<RectTransform>();
14 
15         Scale();
16     }
17 
18 #if UNITY_EDITOR
19     void Update()
20     {
21         Scale();
22     }
23 #endif
24 
25     public void Scale()
26     {
27         float canvasWidth = canvasRectTra.rect.width;
28         float canvasHeight = canvasRectTra.rect.height;
29         float canvasRate = canvasWidth / canvasHeight;
30 
31         float bgWidth = rawImage.texture.width;
32         float bgHeight = rawImage.texture.height;
33         float bgRate = bgWidth / bgHeight;
34 
35         if (bgRate > canvasRate) //为了使图片铺满整个屏幕,需要拉伸图片的高度,同时要保持图片等比例拉伸
36         {
37             rectTra.sizeDelta = new Vector2(canvasHeight * bgRate, canvasHeight);
38         }
39         else
40         {
41             rectTra.sizeDelta = new Vector2(canvasWidth, canvasWidth / bgRate);
42         }
43     }   
44 }

 

原理:

 

效果:

posted on 2021-10-24 19:24  艰苦奋斗中  阅读(333)  评论(0编辑  收藏  举报