AkiraPixelWind/Assets/Scripts/Main/UI/UIMgr/ScreenAutoAdjust.cs

201 lines
6.6 KiB
C#
Raw Permalink Normal View History

2022-12-29 18:20:40 +08:00
using UnityEngine;
using UnityEngine.UI;
public class ScreenAdjustMode
{
public static int M_16_9 = 0;
public static int M_18_9 = 1;
public static int M_16_12 = 2;
}
public class ScreenAutoAdjust : MonoBehaviour
{
public static ScreenAutoAdjust Instance;
public int mode;
public Vector2 screenPointMax;
//public Camera m_uiCamera;
//public Canvas m_Canvas;
public CanvasScaler m_CanvasScaler;
public UIMgr m_uiMgr;
public static readonly int DevWidth = 2568; // max 2972
public static readonly int DevHeight = 1284; // max 1712
private void Awake()
{
Instance = this;
//m_Canvas = transform.GetComponent<Canvas>();
m_CanvasScaler = transform.GetComponent<CanvasScaler>();
if (transform.parent)
{
m_uiMgr = transform.parent.GetComponent<UIMgr>();
}
}
private void Start()
{
ScreenAutoAdapt();
}
// 屏幕自适应
public void ScreenAutoAdapt()
{
float screenRate = Screen.width / (float)Screen.height;
if (screenRate < 1.77f)
{
ScreenAutoAdapt1();
}
else
{
ScreenAutoAdapt2();
}
}
// 多出来的填充黑边的适应方式 4:3屏幕使用, 只要小于16:9 基本都是用这个 *:填充黑边的方式需要有摄像机
public void ScreenAutoAdapt1()
{
int devWidth = DevWidth;
int devHeight = DevHeight;
mode = ScreenAdjustMode.M_16_12;
//屏幕比 > 2:1的用 2:1, 小于2:1的用16: 9
float screenRate = (float)Screen.width / (float)Screen.height;
devWidth = Mathf.FloorToInt(DevHeight * 16 / 9f);
RectTransform rect = m_uiMgr.m_NodeUI.GetComponent<RectTransform>();
rect.sizeDelta = new Vector2(devWidth, devHeight);
RectTransform rect2 = m_uiMgr.m_NodeScreen.GetComponent<RectTransform>();
rect2.sizeDelta = new Vector2(devWidth, devHeight);
RectTransform rect3 = m_uiMgr.m_NodeLoading.GetComponent<RectTransform>();
rect3.sizeDelta = new Vector2(devWidth, devHeight);
m_uiMgr.m_RtfShelter.gameObject.SetActive(true);
m_uiMgr.m_RtfShelter.sizeDelta = new Vector2(0, devHeight);
//项目要使用的屏幕比
float devRate = (float)devWidth / (float)devHeight;
int useHeight = Screen.height;
int useWidth = Mathf.FloorToInt(devRate * useHeight);
if (screenRate < devRate)
{
useWidth = Screen.width;
useHeight = Mathf.FloorToInt((float)useWidth / devRate);
}
Debug.Log("Screen.width:" + Screen.width.ToString() + ", Screen.height:" + Screen.height.ToString());
//m_Canvas.renderMode = RenderMode.ScreenSpaceCamera;
UIMgr.Instance.IsScreenClip = true;
m_CanvasScaler.referenceResolution = new Vector2Int(devWidth, devHeight);
if (screenRate >= devRate)
m_CanvasScaler.matchWidthOrHeight = 1;
else
m_CanvasScaler.matchWidthOrHeight = 0;
float cameraRectWidthRate = 0f;
float cameraRectHeightRate = 0f;
Debug.Log("screenRate:" + screenRate.ToString() + ", devRate:" + devRate.ToString());
if (useWidth < devWidth)
{// 缩小
if (screenRate >= devRate)
{// 宽度缩小 高度不变
m_CanvasScaler.matchWidthOrHeight = 1;
cameraRectWidthRate = (float)devWidth / (float)Screen.width;
Debug.Log("cameraRectHeightRate1 cameraRectWidthRate:" + cameraRectWidthRate.ToString());
}
else
{// 高度缩小 宽不变
m_CanvasScaler.matchWidthOrHeight = 0;
cameraRectHeightRate = (float)useHeight / (float)Screen.height;
Debug.Log("cameraRectHeightRate2 cameraRectHeightRate:" + cameraRectHeightRate.ToString());
}
}
else
{// 放大
if (screenRate >= devRate)
{// 宽度缩小 高度不变
m_CanvasScaler.matchWidthOrHeight = 1;
cameraRectWidthRate = (float)useWidth / (float)Screen.width;
Debug.Log("cameraRectHeightRate3 cameraRectWidthRate:" + cameraRectWidthRate.ToString());
}
else
{// 高度缩小 宽不变
m_CanvasScaler.matchWidthOrHeight = 0;
cameraRectHeightRate = (float)useHeight / (float)Screen.height;
Debug.Log("cameraRectHeightRate4 cameraRectHeightRate:" + cameraRectHeightRate.ToString());
}
}
Debug.Log("Screen.width:" + Screen.width + ", Screen.height:" + Screen.height);
Debug.Log("useWidth:" + useWidth.ToString() + ", useHeight:" + useHeight.ToString());
}
// 改变NodeUI的大小适应方式 16:9 2:1的都用这个
public void ScreenAutoAdapt2()
{
m_uiMgr.m_RtfShelter.gameObject.SetActive(false);
int devWidth = DevWidth;
int devHeight = DevHeight;
mode = ScreenAdjustMode.M_18_9;
// 屏幕比 > 2:1的用 2:1, 小于2:1的用16:9
float screenRate = Screen.width / (float)Screen.height;
if (screenRate < 1.98f)
{
devWidth = DevHeight * 16 / 9;// 2283;//1920
mode = ScreenAdjustMode.M_16_9;
}
float devRate = devWidth / (float)devHeight;
//// 项目要使用的屏幕比
int useHeight = Screen.height;
int useWidth = Mathf.FloorToInt(devRate * useHeight);
if (screenRate < devRate)
{
useWidth = Screen.width;
useHeight = Mathf.FloorToInt(useWidth / devRate);
}
screenPointMax.x = Screen.width;
screenPointMax.y = Screen.height;
m_CanvasScaler.referenceResolution = new Vector2Int(devWidth, devHeight); // Vector2
if (screenRate >= devRate)
{
m_CanvasScaler.matchWidthOrHeight = 1;
}
else
{
m_CanvasScaler.matchWidthOrHeight = 0;
}
if (m_uiMgr)
{
RectTransform rect = m_uiMgr.m_NodeUI.GetComponent<RectTransform>();
rect.sizeDelta = new Vector2(devWidth, devHeight);
RectTransform rect2 = m_uiMgr.m_NodeScreen.GetComponent<RectTransform>();
rect2.sizeDelta = new Vector2(devWidth, devHeight);
RectTransform rect3 = m_uiMgr.m_NodeLoading.GetComponent<RectTransform>();
rect3.sizeDelta = new Vector2(devWidth, devHeight);
}
Debug.Log("Screen.width:" + Screen.width + ", Screen.height:" + Screen.height);
Debug.Log("useWidth:" + useWidth.ToString() + ", useHeight:" + useHeight.ToString());
}
}