iphoneX适配做法

原理:就是右移相应的距离,话不多说,上源码,直接挂到你需要适配的界面节点上面
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UIAdapter : MonoBehaviour
{
public enum AlignType
{
Left = 1,
Right = 2,
}
// Use this for initialization
public int phoneTopMargin = 88;
public int phoneBottomMargin = 0;
public int botomMargin = 0;
RectTransform rectTransform;
Vector2 orignalPostion;
Vector2 orignalMin;
Vector2 orignalMax;
public AlignType alignType = AlignType.Left;
public ScreenOrientation lastOrientantion = ScreenOrientation.Unknown;
private bool m_AdapterFirstRuned = false;
Vector2 lastScreenSize;

bool m_initialized;

#if UNITY_IOS || UNITY_EDITOR
void OnEnable()
{
m_AdapterFirstRuned = false;
Init();
//Adapter();
}
void Adapter()
{
if (!rectTransform)
return;

    m_AdapterFirstRuned = true;
    if (!UIAdapterUtil.IsFixUI)
    {
        rectTransform.anchoredPosition = orignalPostion;
        rectTransform.offsetMin = orignalMin;
        rectTransform.offsetMax = orignalMax;
    }
    else
    {
        if (alignType == AlignType.Left)
        {
            if (lastOrientantion == ScreenOrientation.LandscapeLeft)
            {
                rectTransform.anchoredPosition = new Vector2(orignalPostion.x + phoneTopMargin, orignalPostion.y + botomMargin);
            }
            else if (lastOrientantion == ScreenOrientation.LandscapeRight)
            {
                rectTransform.anchoredPosition = new Vector2(orignalPostion.x + phoneBottomMargin, orignalPostion.y + botomMargin);
            }
        }
        else if (alignType == AlignType.Right)
        {
            if (lastOrientantion == ScreenOrientation.LandscapeLeft)
            {
                rectTransform.anchoredPosition = new Vector2(orignalPostion.x - phoneBottomMargin, orignalPostion.y + botomMargin);
            }
            else if (lastOrientantion == ScreenOrientation.LandscapeRight)
            {
                rectTransform.anchoredPosition = new Vector2(orignalPostion.x - phoneTopMargin, orignalPostion.y + botomMargin);
            }
        }
        else
        {
            if (lastOrientantion == ScreenOrientation.LandscapeLeft)
            {
                rectTransform.offsetMin = new Vector2(orignalMin.x + phoneTopMargin, orignalMin.y + botomMargin);
                rectTransform.offsetMax = new Vector2(orignalMax.x - phoneBottomMargin, orignalMax.y);
            }
            else if (lastOrientantion == ScreenOrientation.LandscapeRight)
            {
                rectTransform.offsetMin = new Vector2(orignalMin.x + phoneBottomMargin, orignalMin.y + botomMargin);
                rectTransform.offsetMax = new Vector2(orignalMax.x - phoneTopMargin, orignalMax.y);
            }
        }

    }
}
void Update()
{
    bool adapter = false;
    if (Screen.width != lastScreenSize.x || Screen.height != lastScreenSize.y)
    {
        lastScreenSize.x = Screen.width;
        lastScreenSize.y = Screen.height;
        adapter = true;
    }

    if (lastOrientantion != UIAdapterUtil.orientation)
    {
        lastOrientantion = UIAdapterUtil.orientation;
        adapter = true;
    }

    if (orignalPostion == rectTransform.anchoredPosition)
    {
        adapter = true;
    }

    if (adapter || m_AdapterFirstRuned == false)
    {
        Adapter();
    }
}
void Init()
{
    if (m_initialized)
        return;
    
    m_initialized = true;

    rectTransform = GetComponent<RectTransform>();
    if (rectTransform)
    {
        orignalPostion = rectTransform.anchoredPosition;
        orignalMin = rectTransform.offsetMin;
        orignalMax = rectTransform.offsetMax;
    }


}

#endif
// Update is called once per frame

}

//using LuaInterface;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UIAdapterUtil
{
//[NoToLua]
public static ScreenOrientation Orientation = ScreenOrientation.LandscapeRight;

//[NoToLua]
public static bool m_fixUI = false;

public static void CheckModelInfo(string deviceModelStr)
{
    if (!string.IsNullOrEmpty(deviceModelStr))
    {
        m_fixUI = false;
        var deviceModelSystem = SystemInfo.deviceModel;
        if (!string.IsNullOrEmpty(deviceModelSystem))
        {
            string[] deviceModelArr = deviceModelStr.Split('|');
            for (int i = 0; i < deviceModelArr.Length; i++)
            {
                string deviceModel = deviceModelArr[i];
                //Debugger.LogError("CheckModelInfo ===========11========== i = " + i + ", model = " + deviceModel + ", deviceModel = " + deviceModelSystem);
                if (deviceModelSystem.Contains(deviceModel))
                {
                    //Debugger.LogError("CheckModelInfo ===========22========== i = " + i + ", model = " + deviceModel + ", deviceModel = " + deviceModelSystem);
                    m_fixUI = true;
                    break;
                }
            }
        }
    }
}

public static bool IsFixUI
{
    get
    {
        return m_fixUI;
    }
}

public static ScreenOrientation orientation
{
    get
    {

#if UNITY_EDITOR
return Orientation;
#else
return Screen.orientation;
#endif
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值