ngui 适配iphone x

本文介绍了一个用于Unity游戏开发中实现UI自适应不同屏幕尺寸,特别是iPhone X等带有安全区域设备的解决方案。通过使用FixedUIRect脚本,可以确保UI元素在不同设备上正确显示,避免被底部安全区域遮挡。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using UnityEngine;

using System.Collections;

[RequireComponent(typeof(UIPanel))]
public class FixedUIRect : MonoBehaviour
{
public enum SAVE_AREA
{
FULL_SCREEN,
SAVE_AREA_WITH_BOTTOM,
SAVE_AREA,
}

public SAVE_AREA type = SAVE_AREA.FULL_SCREEN;
public float IPHONE_X_SAVE_RASIO = 0.965f;
private UIPanel panel;

void OnEnable ()
{
FixClipRegion ();
}

void Start ()
{
FixClipRegion ();
}

private bool IsIphoneX()
{
return true;
// return NativeManager.inst.IsIPHONEX ();
}
private void FixClipRegion ()
{
panel = GetComponent<UIPanel> ();

UIRoot rt = panel.root;

if (panel != null && IsIphoneX() && rt != null) {

panel.clipping = UIDrawCall.Clipping.ConstrainButDontClip;

if (type == SAVE_AREA.SAVE_AREA_WITH_BOTTOM)
{
panel.baseClipRegion = new Vector4 (0, 0, panel.root.manualHeight * (757 / 375f), panel.root.manualHeight);
}
else if (type == SAVE_AREA.SAVE_AREA)
{
panel.baseClipRegion = new Vector4(0, 0, panel.root.manualHeight * (785 / 375f), panel.root.manualHeight);
float offsetY = (1 - IPHONE_X_SAVE_RASIO) * panel.root.manualHeight / 2;
gameObject.transform.localPosition = new Vector3(0,offsetY);

float scale = IPHONE_X_SAVE_RASIO;
gameObject.transform.localScale = scale * Vector3.one;

}
else
{
panel.baseClipRegion = new Vector4 (0, 0, panel.root.manualHeight * (812 / 375f), panel.root.manualHeight);
}
}
}
}

转载于:https://www.cnblogs.com/rexzhao/p/8358431.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值