当前位置:网站首页>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_是刘海屏的高度,需要自己设置更换。
使用如下:
拖动对应的对象,选好适配方式即可
边栏推荐
- vscode笔记
- Advantages of overseas servers
- Tencent Internal Technology: Evolution of Server Architecture of "The Legend of Xuanyuan"
- 单片机期末复习大题
- DevOps process demo (practical record)
- Alibaba Cloud Video on Demand
- Wireshark packet capture and common filtering methods
- Chengyun Technology was invited to attend the 2022 Alibaba Cloud Partner Conference and won the "Gathering Strength and Going Far" Award
- LeetCode中常用语言的一些基本方法记录
- The method of using ROS1 bag under ROS2
猜你喜欢
随机推荐
docker部署完mysql无法连接
VRRP overview and experiment
通过反射获取Class对象的四种方式
LeetCode刷题记录(2)
reduce()方法的学习和整理
LaTeX笔记
[ingress]-ingress使用tcp端口暴露服务
格式化代码缩进的小技巧
Browser Storage WebStorage
sql server duplicate values are counted after
vscode笔记
Introduction to Network Layer Protocols
DevOps process demo (practical record)
scikit-image图像处理笔记
Next-Generation Parsing Technology - Cloud Parsing
Come, come, let you understand how Cocos Creator reads and writes JSON files
Chengyun Technology was invited to attend the 2022 Alibaba Cloud Partner Conference and won the "Gathering Strength and Going Far" Award
带你深入了解Cookie
selenium learning
[Problem has been resolved]-Virtual machine error contains a file system with errors check forced









