当前位置:网站首页>UI刘海屏适配方式
UI刘海屏适配方式
2022-08-05 05:25:00 【木棉-小健】
刘海屏适配
项目上线了总免不了适配问题,
注:该方案支持热更适配,哪怕是上线项目也可以及时开启适配及调整适配程度。
原理:
通过获取设备型号及计算屏幕分辨率,在根据是否刘海屏调整侧边按钮的 Anchored Position 偏移量,从而避过刘海遮挡。
```csharp
using System;
using System.Collections;
using System.Collections.Generic;
using Sirenix.OdinInspector;
#if UNITY_EDITOR
using Sirenix.Utilities.Editor;
using UnityEditor;
#endif
using UnityEngine;
[ExecuteAlways]
public class UI_NotchAdapter : MonoBehaviour
{
public enum AdapterType
{
ePosY_Minus_AdaptY,
eOffsetMaxY_Minus_AdaptY,
eSizeDeltaY_Set_AdaptY,
eSizeDeltaY_Minus_AdaptY,
}
[System.Serializable]
public class UI_NA_Data
{
[LabelText("需要适配的UI对象")]
public RectTransform mRectTransform_;
[LabelText("适配方式")]
public AdapterType mAdapterType_;
}
[ListDrawerSettings(ShowIndexLabels = true, Expanded = true), LabelText("屏幕适配数据"), ShowInInspector]
public List<UI_NA_Data> mAdaptDatas_ = new List<UI_NA_Data>();
private void Awake()
{
}
private void OnDestroy()
{
}
private float mPreviewNotchHeight_ = 60;
void DoAdapterLogic(bool preview = false)
{
if (mAdaptDatas_ != null)
{
for (var i = 0; i < mAdaptDatas_.Count; ++i)
{
var data = mAdaptDatas_[i];
if (data != null)
{
GameManager gm = null;
if (!preview)
gm = GameManager.Instance;
switch (data.mAdapterType_)
{
case AdapterType.ePosY_Minus_AdaptY:
{
if (data.mRectTransform_ != null)
{
var mPos = data.mRectTransform_.transform.localPosition;
data.mRectTransform_.transform.localPosition = new Vector3(mPos.x, mPos.y - gm.MNotchAdaptY_, mPos.z);
}
}
break;
case AdapterType.eSizeDeltaY_Minus_AdaptY:
{
if (data.mRectTransform_ != null)
{
var rSize = data.mRectTransform_.sizeDelta;
data.mRectTransform_.sizeDelta = new Vector2(rSize.x, rSize.y - gm.MNotchAdaptY_);
}
}
break;
case AdapterType.eSizeDeltaY_Set_AdaptY:
{
if (data.mRectTransform_ != null)
{
var rSize = data.mRectTransform_.sizeDelta;
data.mRectTransform_.sizeDelta = new Vector2(rSize.x, gm.MNotchAdaptY_);
}
}
break;
case AdapterType.eOffsetMaxY_Minus_AdaptY:
{
if (data.mRectTransform_ != null)
{
var rectTop = data.mRectTransform_.offsetMax.y;
var rectRight = data.mRectTransform_.offsetMax.x;
data.mRectTransform_.offsetMax = new Vector2(rectRight, rectTop - gm.MNotchAdaptY_);
}
}
break;
}
}
}
}
}
// Start is called before the first frame update
void Start()
{
if (Application.isPlaying)
{
DoAdapterLogic();
}
}
}
将该代码挂到需要调整的适配的ui上,需要注意的是gm.MNotchAdaptY_是刘海屏的高度,需要自己设置更换。
使用如下:
拖动对应的对象,选好适配方式即可
边栏推荐
- 指针常量与常量指针 巧记
- What is Alibaba Cloud Express Beauty Station?
- ROS2下使用ROS1 bag的方法
- Browser Storage WebStorage
- Mina disconnects and reconnects
- sql server duplicate values are counted after
- Billions of IT operations in the market, the product by strength to speak
- NACOS配置中心设置配置文件
- Tencent Internal Technology: Evolution of Server Architecture of "The Legend of Xuanyuan"
- Chengyun Technology was invited to attend the 2022 Alibaba Cloud Partner Conference and won the "Gathering Strength and Going Far" Award
猜你喜欢
随机推荐
初识网页与浏览器
人人AI(吴恩达系列)
网络层协议介绍
docker部署完mysql无法连接
What are some things that you only know when you do operation and maintenance?
Matplotlib绘图笔记
深入分析若依数据权限@datascope (注解+AOP+动态sql拼接) 【循序渐进,附分析过程】
【FAQ】CCAPI兼容EOS相机列表(2022年8月 更新)
请问下通过flink sql读取hologres 的两张表的 binlog,然后如何进行join?
Alibaba Cloud Video on Demand
【考研结束第一天,过于空虚,想对自己进行总结一下】
[issue resolved] - jenkins pipeline checkout timeout
从“双卡双待“到”双通“,vivo率先推动DSDA架构落地
Teach you simple steps to achieve industrial raspberries pie properly installed RS232 USB drive
Programmers should understand I/O this way
Collision, character controller, Cloth components (cloth), joints in the Unity physics engine
Difference between link and @improt
Network Troubleshooting Basics - Study Notes
5分钟完成mysql离线安装
Tencent Cloud Message Queue CMQ