原理:就是右移相应的距离,话不多说,上源码,直接挂到你需要适配的界面节点上面
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
}
}
}