0
点赞
收藏
分享

微信扫一扫

Unity,发布ios和Android的包,UGUI,异形屏适配问题。

Unity,发布ios和Android的包,UGUI,异形屏适配问题。

@TOC

<hr style=" border:solid; width:100px; height:1px;" color=#000000 size=1">

前言

unity发布移动端需要做ui的适配,我们用的是UGUI,暂且提供一种我们自己的ui适配解决方案,包含异形屏的。

<hr style=" border:solid; width:100px; height:1px;" color=#000000 size=1">

一、区分Android手机,Android平板,Iphone,Ipad

也查到了各种各样的区分方法。 我们是通过长宽比来区分的,下面直接上代码。

#if UNITY_ANDROID

        //通过屏幕比例判断是否刘海屏
        if ((float)Screen.width / Screen.height > 2)
        {
            isHaveLiuhai = true;
        }
        if ((float)Screen.width / Screen.height < 1.7f)
        {
        //是安卓平板
            isIphone = false;
        }
        else
        {
        //是安卓手机
            isIphone = true;

        }
#endif

#if UNITY_IOS
        
        //通过屏幕比例判断是否刘海屏
        if ((float)Screen.width / Screen.height > 2)
        {
            isHaveLiuhai = true;
        }
        if ((float)Screen.width / Screen.height < 1.5f)
        {
        //是Ipad
            isIphone = false;
        }
        else
        {
        //是Iphone
            isIphone = true;

        }
#endif

二、UGUI的适配

1.Canvas设置

这是canvas的相关设置

Unity,发布ios和Android的包,UGUI,异形屏适配问题。_#if

2.相应的ui缩进,来适应异形屏

设置ui缩进的代码如下(示例):

//设置节点的位置
    public void SetAdapta(Transform target, float x)
    {
#if UNITY_IPHONE
        if (isHaveLiuhai)
        {
            RectTransform rect = target.GetComponent<RectTransform>();
            Vector2 anPos = rect.anchoredPosition;
            anPos.x += x;
            rect.anchoredPosition = anPos;
        }
#endif
#if UNITY_ANDROID
        if (isHaveLiuhai)
        {
            RectTransform rect = target.GetComponent<RectTransform>();
            Vector2 anPos = rect.anchoredPosition;
            anPos.x += x;
            rect.anchoredPosition = anPos;
        }
#endif
    }

这是调用的代码

IPhoneAdapta.Instance.SetAdapta(scanButton.transform, -144);

表示右侧的ui向左缩进144,左侧ui向右缩进的话,把负值变为正值即可。


举报

相关推荐

Flutter 全面屏适配Android 和IOS

0 条评论