AI补足注释(已经快看不懂了)
实现i18n 优化部分模块的逻辑以优化性能 修复物品展示框打开时按下ESC唤出暂停菜单但没有暂停的bug Signed-off-by: Eicy <im@crash.work>
This commit is contained in:
@@ -1,44 +1,54 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.Serialization;
|
||||
|
||||
namespace Camera
|
||||
{
|
||||
// 处理屏幕的纵横比,以确保游戏在不同分辨率下的正确显示
|
||||
public class ScreenAspect : MonoBehaviour
|
||||
{
|
||||
//目标比例,默认16:9
|
||||
public float TargetAspect = 16f / 9f;
|
||||
private UnityEngine.Camera _mainCamera;
|
||||
[FormerlySerializedAs("TargetAspect")]
|
||||
[Header("Target Aspect Ratio")]
|
||||
[Tooltip("目标纵横比,默认设置为16:9")]
|
||||
public float targetAspect = 16f / 9f; // 默认目标比例
|
||||
|
||||
private UnityEngine.Camera _mainCamera; // 主相机引用
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
// 获取主相机
|
||||
_mainCamera = UnityEngine.Camera.main;
|
||||
|
||||
// 计算当前窗口的纵横比
|
||||
var windowAspect = Screen.width / (float)Screen.height;
|
||||
|
||||
var scaleHeight = windowAspect / TargetAspect;
|
||||
// 计算缩放高度
|
||||
var scaleHeight = windowAspect / targetAspect;
|
||||
|
||||
// 根据缩放比例调整相机的视口
|
||||
if (scaleHeight < 1f)
|
||||
{
|
||||
var rect = _mainCamera.rect;
|
||||
|
||||
rect.width = 1f;
|
||||
rect.height = scaleHeight;
|
||||
rect.x = 0;
|
||||
rect.y = (1f - scaleHeight) / 2f;
|
||||
|
||||
_mainCamera.rect = rect;
|
||||
// 纵向拉伸的情况
|
||||
SetCameraRect(1f, scaleHeight, 0, (1f - scaleHeight) / 2f);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 横向拉伸的情况
|
||||
var scaleWidth = 1f / scaleHeight;
|
||||
|
||||
var rect = _mainCamera.rect;
|
||||
|
||||
rect.width = scaleWidth;
|
||||
rect.height = 1f;
|
||||
rect.x = (1f - scaleWidth) / 2f;
|
||||
rect.y = 0;
|
||||
|
||||
_mainCamera.rect = rect;
|
||||
SetCameraRect(scaleWidth, 1f, (1f - scaleWidth) / 2f, 0);
|
||||
}
|
||||
}
|
||||
|
||||
// 设置相机视口的矩形
|
||||
private void SetCameraRect(float width, float height, float x, float y)
|
||||
{
|
||||
var rect = _mainCamera.rect; // 获取相机的当前视口矩形
|
||||
|
||||
rect.width = width; // 设置视口宽度
|
||||
rect.height = height; // 设置视口高度
|
||||
rect.x = x; // 设置视口X位置
|
||||
rect.y = y; // 设置视口Y位置
|
||||
|
||||
_mainCamera.rect = rect; // 应用新的视口矩形
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user