添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

Hello! Not sure if this should be here or in scripting.

I want to force the aspect ratio of the camera in the viewport .
I want to see the same limits editing and testing or playing.

This is a new 2D project by default with just a 1920x1080 sprite at 0, 0, 0:
http://i.imgur.com/3P2desm.png

And this is what I get if I increase the size. The camera can’t see all the sprite up and down:
http://i.imgur.com/soDIi8x.png

Sorry is this is pretty basic and simple.
I actually tried some scrips for camera and no ones works for me as I want.

Greetings!

Well, at the top left of your game view you can choose a resolution or aspect ratio to preview your game with.

Enforcing the resolution when you build out the game is a different task. I would recommend using an overlay canvas to limit the visible screen space on larger devices. The canvas will respect your chosen preview aspect ratio or resolution, so you can design around that. Adding black bars on the outside of it, or whatever you’re going for. The canvas has a Canvas Scalar component that manages how the canvas will scale with different aspect ratios and resolutions.

If you want to display a scene with a fixed size independent of resolution
Include this script into your camera.

using UnityEngine;
using System.Collections.Generic;
/// <summary>
/// Skrypt odpowiada za usatwienie rozdzielczosci kemerze
/// </summary>
public class CameraResolution : MonoBehaviour
    #region Pola
    private int ScreenSizeX = 0;
    private int ScreenSizeY = 0;
    #endregion
    #region metody
    #region rescale camera
    private void RescaleCamera()
        if (Screen.width == ScreenSizeX && Screen.height == ScreenSizeY) return;
        float targetaspect = 16.0f / 9.0f;
        float windowaspect = (float)Screen.width / (float)Screen.height;
        float scaleheight = windowaspect / targetaspect;
        Camera camera = GetComponent<Camera>();
        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;
        ScreenSizeX = Screen.width;
        ScreenSizeY = Screen.height;
    #endregion
    #endregion
    #region metody unity
    void OnPreCull()
        if (Application.isEditor) return;
        Rect wp = Camera.main.rect;
        Rect nr = new Rect(0, 0, 1, 1);
        Camera.main.rect = nr;
        GL.Clear(true, true, Color.black);
        Camera.main.rect = wp;
    // Use this for initialization
    void Start () {
        RescaleCamera();
    // Update is called once per frame
    void Update () {
        RescaleCamera();
    #endregion

float targetaspect = 16.0f / 9.0f; - This is the aspect to 1920x1080

if you have an image / scene height of 1080 is that regardless of the resolution to the 1080 you need to calculate it as follows:
1080 / (Pixel per unit) / 2. For the 1080 value 5.4 (if pixel per unit) has a value of 100.

Sorry for my bad english :slight_smile:

I faced a similar issue, and I got around it by setting the orthographic cam size:

using UnityEngine;
public class OrtoCamResizerByScreenSize : MonoBehaviour
    [SerializeField] private float multiplier = 2.82f;
    private void Awake()
        Camera.main.orthographicSize = Screen.height / Screen.width * multiplier;

Played around with a few screen sizes, and 2.82ish just worked fine for a couple of devices.

If you want to display a scene with a fixed size independent of resolution
Include this script into your camera.

Is there a way to make it not stick to the edges and have a set-able size? (I really need this)

If you want to display a scene with a fixed size independent of resolution
Include this script into your camera.

using UnityEngine;
using System.Collections.Generic;
/// <summary>
/// Skrypt odpowiada za usatwienie rozdzielczosci kemerze
/// </summary>
public class CameraResolution : MonoBehaviour
    #region Pola
    private int ScreenSizeX = 0;
    private int ScreenSizeY = 0;
    #endregion
    #region metody
    #region rescale camera
    private void RescaleCamera()
        if (Screen.width == ScreenSizeX && Screen.height == ScreenSizeY) return;
        float targetaspect = 16.0f / 9.0f;
        float windowaspect = (float)Screen.width / (float)Screen.height;
        float scaleheight = windowaspect / targetaspect;
        Camera camera = GetComponent<Camera>();
        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;
        ScreenSizeX = Screen.width;
        ScreenSizeY = Screen.height;
    #endregion
    #endregion
    #region metody unity
    void OnPreCull()
        if (Application.isEditor) return;
        Rect wp = Camera.main.rect;
        Rect nr = new Rect(0, 0, 1, 1);
        Camera.main.rect = nr;
        GL.Clear(true, true, Color.black);
        Camera.main.rect = wp;
    // Use this for initialization
    void Start () {
        RescaleCamera();
    // Update is called once per frame
    void Update () {
        RescaleCamera();
    #endregion

float targetaspect = 16.0f / 9.0f; - This is the aspect to 1920x1080

if you have an image / scene height of 1080 is that regardless of the resolution to the 1080 you need to calculate it as follows:
1080 / (Pixel per unit) / 2. For the 1080 value 5.4 (if pixel per unit) has a value of 100.

Sorry for my bad english :slight_smile:

Great script!

How would you fix the canvas in this case?

If you are asking how to keep all canvas UI items in your game resized to the same size as the screen camera, this is what worked for me for my 3D game where I use a screen space canvas for my hud, and the above script for creating black bars (Thanks quarag).

  • Use Canvas Screen Space - Camera (Drag your camera into render camera)
  • Set Canvas Scaler UI Scale Mode to Scale with Screen and set the appropriate resolution reference.
  • Set Canvas Scaler Screen match mode = Match width or height
  • if your game is in portrait mode, you can use this script which will add black bars on top/bottom or left/right depending on aspect ratio

    float targetaspect = 16.0f / 9.0f;
    float windowaspect = (float)Screen.height / (float)Screen.width;
    float scalefactor;
    if (windowaspect >= targetaspect)
    scalefactor = windowaspect / targetaspect;
    Camera.main.orthographicSize = Camera.main.orthographicSize * scalefactor;
    heartbreakkid1:

    if your game is in portrait mode, you can use this script which will add black bars to either top or bottom

    Even when you are necro-posting to six-year-old threads, the same rules still apply:

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://discussions.unity.com/t/481379

    You may edit your post above. When you do, I shall delete this notice.

    I was dealing with this problem and struggling but I just fixed it way simpler than I thought I’d be able to. Basically I had my canvas on scale with screen size to a 1440x2560 resolution (9x16) and nothing was scaling properly but then I changed the match width or height to expand and then it worked perfectly. My app doesn’t have anything on the edges so I just added a black image component to the canvas itself and it just scales how I want it behind my UI