Set Display resolution limit in unity

10.3k Views Asked by At

So I released a game a few months ago.

I run a lot of test on devices I add at home (galaxy note 2, galaxy tab pro, wiko), and the game runs smoothly on these devices.

But last day, I run my game on an LG G3 device, and there are a lot of FPS drops.

I think it's because the game runs with the native display resolution of the screen (2560 x 1440).

Is it possible to create a script, that when it detects a display resolution upper than FullHD (like for the LG G3), it displays the game in a lower resolution?

I think it would stop the FPS drops.

2

There are 2 best solutions below

3
On BEST ANSWER

Adjust same Camera Resolution on every Device.

If your Game is in portrait mode then use 720*1280 resolution and if using landscape mode the use 960*640 , your game will run perfect on every device.

  1. Attach Script to your camera
  2. Change Values targetaspect

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;
     }
   }
 }
0
On

is not that easy (with a good quality result).

Basically, you can use asset bundle system for it and have double of your graphics in SD and HD formats. Unity supports it, it calls variants. Please find more information about Asset Bundles here: https://unity3d.com/learn/tutorials/topics/scripting/assetbundles-and-assetbundle-manager

Detection of screen resolution is easy. You can use Screen.width and Screen.height for it.

I know Screen class has a method SetResolution and this might do a thing for you without using an Asset Bundle system. I have never use it on my own. Here is more about Screen class: https://docs.unity3d.com/ScriptReference/Screen.html

and concrete SetResolution method: https://docs.unity3d.com/ScriptReference/Screen.SetResolution.html

You can use Camera.aspect to get an aspect ratio of the screen as well: https://docs.unity3d.com/ScriptReference/Camera-aspect.html