**Foreword:** When making the UI interface of the game, different mobile phones have different notch screens. To prevent the UI from being blocked by the punch screen or notch screen, UI adaptation is required.
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// 注意
/// Screen.safeArea 使用屏幕空间坐标系,虽然它是一个Rect,但它的左下角才是(0,0)原点
/// </summary>
public class ScreenUtils
{
/// <summary>
/// 按画布尺寸返回画布上方安全区域外空间高度的缩放值
/// </summary>
/// <param name="canvasScaler">画布的缩放,为null则没有缩放</param>
/// <returns></returns>
public static float GetCanvasOutsideTop(CanvasScaler canvasScaler)
{
var result = Screen.height - Screen.safeArea.yMax;
if (canvasScaler != null)
{
result = result * canvasScaler.referenceResolution.y / Screen.height;
}
return result;
}
/// <summary>
/// 按画布尺寸返回画布下方安全区域外空间的高度的缩放值
/// </summary>
/// <param name="canvasScaler">画布的缩放,为null则没有缩放</param>
/// <returns></returns>
public static float GetCanvasOutsideBottom(CanvasScaler canvasScaler)
{
var result = Screen.safeArea.yMin;
if (canvasScaler != null)
{
result = result * canvasScaler.referenceResolution.y / Screen.height;
}
return result;
}
}
***Note:UseDevice Simulator toolkit to check the game performance of each model of mobile phone Adapt
installation steps:
- Open Unity's PackageManager page and click as shown in Figure 1 below
- Check the logo in Figure 2 to return to the PackangManager page to install the toolkit.