Android刘海屏的处理方式
画布处理
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public static class ScreenUtil
{
//设计头部安全高度
public const float DesignSafeHeadHeight = 0;
//设计
public const float DesignWidth = 828;
public const float DesignHeight = 1792;
public static float DesignWHRate = DesignWidth / DesignHeight;
//设备
public static float PhoneWidth = Screen.width;
public static float PhoneHeight = Screen.height;
public static float PhoneWHRate = PhoneWidth / PhoneHeight;
//画布
public static float CanvasWidth;
public static float CanvasHeight;
public const float ScaleBG = 1.02f;
/// <summary>
/// 画布适配 初始化画布时调用此函数
/// </summary>
public static void CanvasScalerFit(Canvas canvas, CanvasScaler canvasScaler)
{
canvasScaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
canvasScaler.referenceResolution = new Vector2(DesignWidth, DesignHeight);
canvasScaler.screenMatchMode = CanvasScaler.ScreenMatchMode.MatchWidthOrHeight;
if (PhoneWHRate > DesignWHRate)//超宽时:长度不变,宽度拉长
{
canvasScaler.matchWidthOrHeight = 1;
CanvasWidth = ScreenUtil.DesignHeight * ScreenUtil.PhoneWHRate;
CanvasHeight = ScreenUtil.DesignHeight;
}
else//超长时:宽度不变,长度拉长
{
canvasScaler.matchWidthOrHeight = 0;
CanvasWidth = ScreenUtil.DesignWidth;
CanvasHeight = ScreenUtil.DesignWidth / ScreenUtil.PhoneWHRate;
}
canvasScaler.referencePixelsPerUnit = 100;
}
/// <summary>
/// 运行中自动适配屏幕分辨率(分屏或折叠屏) 实时刷新调用
/// </summary>
public static bool IsResolutionChanged()
{
int width = (int)PhoneWidth;
int height = (int)PhoneHeight;
if (width == Screen.width && height == Screen.height)
return false;
else
{
PhoneWidth = Screen.width;
PhoneHeight = Screen.height;
PhoneWHRate = PhoneWidth / PhoneHeight;
return true;
}
}
/// <summary>
/// 窗体适配
/// </summary>
/// <param name="rectTransform"></param>
/// <param name="reverse"></param>
public static void WindowSafeAreaAdapt(RectTransform rectTransform, bool reverse = false)
{
float offsetCanvasHeight = WindowSafeAreaTopOffset();
rectTransform.anchorMax = Vector2.one;
rectTransform.anchorMin = rectTransform.offsetMin = Vector2.zero;
rectTransform.offsetMax = new Vector2(0, (reverse ? 1 : -1) * offsetCanvasHeight);
}
public enum ShowRectEnum
{
/// <summary>
/// 大又全
/// </summary>
BigAndTotall,
/// <summary>
/// 大又满
/// </summary>
BigAndFull
}
/// 更改特殊底图的处理
public static void ShowRect(ShowRectEnum showRectEnum, RectTransform rectTransform)
{
Vector2 rawSize;
float rectWHRate;
float scale = 1;
switch (showRectEnum)
{
case ShowRectEnum.BigAndFull:
rawSize = rectTransform.sizeDelta;
if ((rawSize.x + rawSize.y) <= 0)
{
rawSize = rectTransform.sizeDelta = new Vector2(Screen.width, Screen.height);
}
rectWHRate = rawSize.x / rawSize.y;
if (rectWHRate >= ScreenUtil.PhoneWHRate)
{
scale = ScreenUtil.CanvasHeight / rawSize.y;
}
else
{
scale = ScreenUtil.CanvasWidth / rawSize.x;
}
rectTransform.localScale = new Vector3(scale * ScaleBG, scale * ScaleBG, 1);
break;
case ShowRectEnum.BigAndTotall:
rawSize = rectTransform.sizeDelta;
if ((rawSize.x + rawSize.y) <= 0)
{
rawSize = rectTransform.sizeDelta = new Vector2(Screen.width, Screen.height);
}
rectWHRate = rawSize.x / rawSize.y;
if (rectWHRate >= ScreenUtil.PhoneWHRate)
{
scale = ScreenUtil.CanvasWidth / rawSize.x;
}
else
{
scale = ScreenUtil.CanvasHeight / rawSize.y;
}
rectTransform.localScale = new Vector3(scale, scale, 1);
break;
}
}
/// <summary>
/// 窗体安全区顶部偏移
/// </summary>
/// <returns></returns>
public static float WindowSafeAreaTopOffset()
{
float safeHeight = Screen.safeArea.height;
float safeHeadHeight = PhoneHeight - safeHeight;
float offset = safeHeadHeight - DesignSafeHeadHeight;
if (offset < 0)
offset = 0;
float offsetPhoneHeightRate = offset / PhoneHeight;
float offsetCanvasHeight = offsetPhoneHeightRate * CanvasHeight;
return offsetCanvasHeight;
}
}
脚本是处理画布根据处理画布的大小,将更改ui中的适配;
背景图处理
using UnityEngine;
using UnityEngine.UI;
public class ScreenPanelBG : MonoBehaviour
{
public enum PanelBgType
{
AddTopBg,
Stretching,
}
[SerializeField] private RectTransform m_PanelBg;
[SerializeField] private RectTransform m_PanelTopBg;
[SerializeField] private PanelBgType m_PanelBgType;
private void Start()
{
ScreenRefresh();
}
public void ScreenRefresh()
{
if (ScreenUtil.WindowSafeAreaTopOffset() <= 0)
return;
switch (m_PanelBgType)
{
case PanelBgType.AddTopBg:
if (m_PanelTopBg)
AddTopBg();
else
TemplateModel.Instance.CloneFromTemplate("CommonAddTopBg", m_PanelBg, (cloneGameObject) =>
{
m_PanelTopBg = cloneGameObject.transform as RectTransform;
AddTopBg();
});
break;
case PanelBgType.Stretching:
ScreenUtil.WindowSafeAreaAdapt(m_PanelBg, true);
break;
default:
break;
}
}
private void AddTopBg()
{
m_PanelTopBg.pivot = Vector2.zero;
m_PanelTopBg.anchorMax = Vector2.one;
m_PanelTopBg.anchorMin = new Vector2(0, 1);
m_PanelTopBg.anchoredPosition3D = Vector3.zero;
m_PanelTopBg.sizeDelta = new Vector2(0, ScreenUtil.WindowSafeAreaTopOffset());
}
}
进行画布处理后,全屏幕显示刘海屏显示不全,故需要在每个界面中添加此脚本;进行背景图拉伸或添加刘海屏图片显示;
疑惑点
1:unity中的 Screen.safeArea对于安卓系统和苹果系统来说是不是存在不一样的设置;(两者获取的结果中心点有差别);
2:有的手机不仅仅有刘海屏还有下面弯曲部分,如果需要做处理的话是需要如何处理的;
如有授业解惑者,希望能不吝赐教。期待大佬的教导解惑。