android - 统一设置显示分辨率限制

标签 android optimization unity-game-engine screen device

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

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

但最后一天,我在 LG G3 设备上运行我的游戏,发现 FPS 下降很多。

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

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

我认为它会阻止 FPS 下降。

最佳答案

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

如果您的游戏处于纵向模式,则使用 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;
     }
   }
 }

关于android - 统一设置显示分辨率限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41422601/

相关文章:

javascript - Phonegap 构建、HTML5 音频、Android

android - 如何在单个 Activity 中添加操作栏和工具栏?

python - scipy.optimize 非线性约束

optimization - 为什么CPU分支指令比较慢?

git - git 子模块上的云构建失败

安卓浏览器

java - libGDX新手困惑

python - 通过在 MATLAB/Python 中优化多个变量来减少两个图之间的差异?

java - Unity3D : APK file missing

unity-game-engine - if 语句表现不佳