【问题标题】:Set Display resolution limit in unity统一设置显示分辨率限制
【发布时间】:2017-05-16 07:27:53
【问题描述】:

所以我几个月前发布了一款游戏。

我在家里添加的设备(galaxy note 2、galaxy tab pro、wiko)上进行了大量测试,游戏在这些设备上运行流畅。

但昨天,我在 LG G3 设备上运行我的游戏,出现了很多 FPS 下降。

我认为这是因为游戏以屏幕的原始显示分辨率 (2560 x 1440) 运行。

是否可以创建一个脚本,当它检测到高于 FullHD 的显示分辨率(例如 LG G3)时,它会以较低的分辨率显示游戏?

我认为它会阻止 FPS 下降。

【问题讨论】:

    标签: android optimization unity3d screen device


    【解决方案1】:

    在每台设备上调整相同的相机分辨率。

    如果您的游戏处于纵向模式,则使用 720*1280 分辨率,如果使用横向模式,则使用 960*640 ,您的游戏将在每台设备上完美运行。

    1. 将脚本附加到您的相机
    2. 更改值目标方面

    using UnityEngine;
    using System.Collections;
    
    public class CameraResolution : MonoBehaviour {
    
    void Start () {
        // set the desired aspect ratio (the values in this example are
        // hard-coded for 16:9, but you could make them into public
        // variables instead so you can set them at design time)
        float targetaspect = 720.0f / 1280.0f;
    
        // determine the game window's current aspect ratio
        float windowaspect = (float)Screen.width / (float)Screen.height;
    
        // current viewport height should be scaled by this amount
        float scaleheight = windowaspect / targetaspect;
    
        // obtain camera component so we can modify its viewport
        Camera camera = GetComponent<Camera> ();
    
        // if scaled height is less than current height, add letterbox
        if (scaleheight < 1.0f) {  
            Rect rect = camera.rect;
    
            rect.width = 1.0f;
            rect.height = scaleheight;
            rect.x = 0;
            rect.y = (1.0f - scaleheight) / 2.0f;
    
            camera.rect = rect;
        } else { // add pillarbox
            float scalewidth = 1.0f / scaleheight;
    
            Rect rect = camera.rect;
    
            rect.width = scalewidth;
            rect.height = 1.0f;
            rect.x = (1.0f - scalewidth) / 2.0f;
            rect.y = 0;
    
            camera.rect = rect;
         }
       }
     }
    

    【讨论】:

    • 你知道脚本不会改变分辨率吗?
    • 是的,但它适用于不同的分辨率。它是为 1280/720 编写的,但您可以看到它相应地缩放更高和更低分辨率的代码。
    • 嘿,感谢您的回答,但我不明白如果设备屏幕显示为 QHD 或 4k 屏幕,我如何将游戏的分辨率降低到全高清? (因为 unity 在屏幕显示分辨率下运行游戏,如果设备有 2k 或更高的显示,它非常耗电)。
    【解决方案2】:

    不是那么容易(结果质量很好)。

    基本上,您可以为此使用资产捆绑系统,并拥有双倍的 SD 和 HD 格式的图形。 Unity 支持它,它调用变体。请在此处找到有关资产包的更多信息: https://unity3d.com/learn/tutorials/topics/scripting/assetbundles-and-assetbundle-manager

    检测屏幕分辨率很容易。您可以使用Screen.widthScreen.height

    我知道Screen 类有一个方法SetResolution,这可能会在不使用 Asset Bundle 系统的情况下为您做一些事情。我从来没有单独使用过它。 以下是关于Screen 类的更多信息: https://docs.unity3d.com/ScriptReference/Screen.html

    具体的SetResolution方法: https://docs.unity3d.com/ScriptReference/Screen.SetResolution.html

    您也可以使用Camera.aspect 来获取屏幕的纵横比: https://docs.unity3d.com/ScriptReference/Camera-aspect.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多