#Handling multiple resolutions in 2D

1 messages · Page 1 of 1 (latest)

leaden talon
#

Hey, while building this prototype i noticed i have a problem with resolutions.

First image is how i kinda of want it to look

Second image is it at around 16:9

and third is the game when the window is around 9:16

So, any suggestions to deal with this?
Should i allow moving the camera?
Should i force a certain resolution? GOD NO
Should i use a render texture? That absolutely kills the interactions that i want to build
Should i force an aspect ratio? How?

mossy briar
#

A lot of games that require a specific aspect ratio like this will either force a resolution or use letterboxing

leaden talon
#

Any resources i can use to understand how to implement that?

My latest letterboxing attempt did cut the camera but the ui would extend over the letterbox.

And forcing certain resolution is kind of an unknown area to me.

I did look into some google results but nothing looked actually solid (is there a way to set that into the build profile?)

mossy briar
#

And like one line of code

bold plover
#

you want the camera to have a different aspect ratio than the actual screen

#

(i guess you can just render too much and then cover up some areas)

mossy briar
#

you can also use the camera viewport settings if you wish

leaden talon
#

Ended up using the camera viewport, locking it to certain aspect ratio and then in my main canvas, setting it to camera

#
using UnityEngine;

[ExecuteInEditMode, RequireComponent(typeof(Camera))]
public class CameraScreenResolution : MonoBehaviour
{
    void Start()
    {
        SetAspect();
    }



#if UNITY_EDITOR
    void Update()
    {
        SetAspect();
    }
#endif


    void SetAspect()
    {
        float targetAspect = 1f; // 1:1
        float windowAspect = (float)Screen.width / Screen.height;
        float scaleHeight = windowAspect / targetAspect;

        Camera cam = GetComponent<Camera>();

        if (scaleHeight < 1.0f)
        {
            // Letterbox (top/bottom bars)
            Rect rect = cam.rect;
            rect.width = 1f;
            rect.height = scaleHeight;
            rect.x = 0;
            rect.y = (1f - scaleHeight) / 2f;
            cam.rect = rect;
        }
        else
        {
            // Pillarbox (left/right bars)
            float scaleWidth = 1f / scaleHeight;

            Rect rect = cam.rect;
            rect.width = scaleWidth;
            rect.height = 1f;
            rect.x = (1f - scaleWidth) / 2f;
            rect.y = 0;
            cam.rect = rect;
        }
    }
} 

here is the code if someone is interested