zoukankan      html  css  js  c++  java
  • [Unity框架]UI框架01:屏幕适配

    参考链接:

    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     }
  • 相关阅读:
    测试工作中需要用到的一些linux命令
    软件的性能测试到底是测试哪些方面
    软件测试中的安全测试包括哪些方面
    git一不小心上传了大文件,怎么破?
    接口测试校验返回数据格式,JasonSchema使用详解
    selenium定位中需要鼠标悬浮才能显示的按钮
    jenknis参数化构建,windows和shell引用变量
    selenium元素定位之父子节点、胞节点
    selenium元素定位之多个元素中选择其中的一个
    Python实现注册和三次验证登录
  • 原文地址:https://www.cnblogs.com/lyh916/p/15452280.html
Copyright © 2011-2022 走看看