Files
Main/Assets/Plugins/References/FuncellBase/Screen/ScreenSystem.cs

101 lines
3.0 KiB
C#
Raw Normal View History

2025-01-25 04:38:09 +08:00
using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
namespace Thousandto.Core.Base
{
/// <summary>
/// 屏幕系统
/// </summary>
public class ScreenSystem
{
//定义的标准的宽高16:9
public const int STAND_WIDTH = 1136;
public const int STAND_HEIGHT = 640;
//宽高比例
private static int scaleWidth = 0;
private static int scaleHeight = 0;
//是否初始化
private static bool _isInitialized = false;
//是否是安卓
private static bool _isAndroid = false;
//初始化
public static void Initialize( bool isAndroid)
{
//设置屏幕不可以休眠
Screen.sleepTimeout = SleepTimeout.NeverSleep;
//记录判断是不是android
_isAndroid = isAndroid;
//初始化修改屏幕的大小
if (!_isInitialized)
{
SetMaxDesignContent();
}
_isInitialized = true;
}
//设置屏幕的设计内容的比率
//android下面的修改
public static void SetDesignContentScale()
{
if (!_isAndroid) return;
if (scaleWidth == 0 && scaleHeight == 0)
{
int width = Screen.currentResolution.width;
int height = Screen.currentResolution.height;
int designWidth = STAND_WIDTH;
int designHeight = STAND_HEIGHT;
float s1 = (float)designWidth / (float)designHeight;
float s2 = (float)width / (float)height;
if (s1 < s2)
{
designWidth = (int)Mathf.FloorToInt(designHeight * s2);
}
else if (s1 > s2)
{
designHeight = (int)Mathf.FloorToInt(designWidth / s2);
}
float contentScale = (float)designWidth / (float)width;
if (contentScale < 1.0f)
{
scaleWidth = designWidth;
scaleHeight = designHeight;
}
}
if (scaleWidth > 0 && scaleHeight > 0)
{
if (scaleWidth % 2 == 0)
{
scaleWidth += 1;
}
else
{
scaleWidth -= 1;
}
Screen.SetResolution(scaleWidth, scaleHeight, true);
}
}
//设置屏幕的设计内容的比率
//android下面的修改
public static void SetMaxDesignContent()
{
if (!_isAndroid) return;
var resolutions = Screen.resolutions;
if (resolutions != null && resolutions.Length > 0)
{
//设置为显示器支持的最大分辨率
var size = resolutions[resolutions.Length - 1];
Screen.SetResolution(size.width, size.height, true);
}
}
}
}