You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Unity中设置Gameboy分辨率并适配窗口/全屏显示

在Unity中实现Gameboy 160×144分辨率的全屏缩放适配

以下是具体实现步骤,确保游戏保持原始像素比例、全屏时自动添加黑边且像素清晰:

1. 设置项目基础分辨率

  • 打开Edit > Project Settings > Player
  • 在Resolution and Presentation面板:
    • 将Default Resolution设为160×144
    • 先将Fullscreen Mode设为Windowed,后续再调整全屏模式

2. 配置相机强制保持宽高比

  • 选中主相机,在Inspector面板调整Camera组件:
    • 设Projection为Orthographic(适配Gameboy 2D显示逻辑)
    • 计算正交大小:Orthographic Size = 144 / 2 = 72(正交相机大小为从中心到顶部的像素数)
    • 勾选Aspect Ratio,选择Custom并输入160和144,相机将自动在宽高比不匹配区域生成黑边

3. 启用像素完美渲染(保持像素清晰度)

  • 给主相机添加Pixel Perfect Camera组件(2D项目默认支持):
    • 设置Reference Resolution为160×144
    • 设Asset Pixels Per Unit为1(确保游戏内像素与屏幕像素1:1整数倍缩放)
    • 勾选Upscale Render Texture,避免缩放时出现模糊锯齿

4. 配置全屏模式规则

  • 回到Player Settings > Resolution and Presentation:
    • 将Fullscreen Mode设为Exclusive Fullscreen或Borderless Window(按需选择)
    • 设Scaling Mode为Scale With Screen Size,Screen Match Mode选Match Width Or Height,Match值设为0.5(或根据测试调整,确保整体按原始比例缩放)

5. 动态适配代码(可选)

如果需要运行时自动适配屏幕变化,挂载以下脚本到持久化对象:

using UnityEngine;

public class GBResolutionAdapter : MonoBehaviour
{
    private readonly Vector2Int _targetRes = new(160, 144);

    void Start()
    {
        Screen.SetResolution(_targetRes.x, _targetRes.y, Screen.fullScreen);
        Camera.main.aspect = (float)_targetRes.x / _targetRes.y;
    }

    void OnEnable() => Screen.onResolutionChanged += RefreshAspectRatio;
    void OnDisable() => Screen.onResolutionChanged -= RefreshAspectRatio;

    void RefreshAspectRatio(int width, int height)
    {
        Camera.main.aspect = (float)_targetRes.x / _targetRes.y;
    }
}

注意事项

  • 测试时切换全屏/窗口模式,验证黑边位置、像素是否无拉伸模糊
  • 若使用UGUI,给Canvas添加Canvas Scaler组件:设UI Scale Mode为Scale With Screen Size,Reference Resolution为160×144,确保UI同步适配

内容的提问来源于stack exchange,提问作者FinnM

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 11:50:29