当前位置:网站首页>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_是刘海屏的高度,需要自己设置更换。
使用如下:
拖动对应的对象,选好适配方式即可
边栏推荐
- Into the pre-service, thought they play so flowers
- Detailed explanation of ten solutions across domains (summary)
- Introduction to Network Layer Protocols
- cs231n学习记录
- Browser Storage for H5
- docker部署完mysql无法连接
- Advantages of overseas servers
- 教您简单几步实现工业树莓派正确安装RS232转USB驱动
- 多行文本省略
- 深入分析若依数据权限@datascope (注解+AOP+动态sql拼接) 【循序渐进,附分析过程】
猜你喜欢
随机推荐
Complete mysql offline installation in 5 minutes
[Problem has been resolved]-Virtual machine error contains a file system with errors check forced
DevOps - Understanding Learning
DevOps流程demo(实操记录)
前置++和后置++的区别
传输层协议
【FAQ】CCAPI兼容EOS相机列表(2022年8月 更新)
Growth: IT Operations Trends Report
Met with the browser page
Xiaodu Xiaodu is here!
Seven Ways to Center a Box Horizontally and Vertically
Billions of IT operations in the market, the product by strength to speak
LeetCode中常用语言的一些基本方法记录
disabledDate 日期选择器 datePicker
教您简单几步实现工业树莓派正确安装RS232转USB驱动
单片机期末复习大题
LeetCode刷题记录(2)
Cloud Computing Basics - Study Notes
Transformer interprets and predicts instance records in detail
Network Troubleshooting Basics - Study Notes