Changing aspect ratios from script

Hi!

Situation:

I am learning Unity while trying to develop a prototype for a project in which there are cutscenes that are meant to mimic a vhs tape on a 4:3 CRT TV. I’m dealing with the first of these cutscenes, which is just a text animated to roll up inside a UI with a 4:3 aspect ratio. So it gives me the nice “screen” with two black bars at the sides, which is what I want. So far, so good.
But then I went and applied a shader* that works via adding it as a component to the main camera, in order to enhance the VHS feel, but when I do that, the artifacts cover the whole screen space, including the black bars at the side, as one would expect.
So I figured, why not set the aspect ratio to 4:3 and it looks perfect that way, black bars look solid and clean.But then I go back to gameplay and I need my 16:9 aspect ratio back.

_* The shader I am using is: https://assetstore.unity.com/packages/vfx/shaders/fullscreen-camera-effects/camera-filter-pack-18433_

My question(s):

  • Is there a way that I can switch between the 4:3 and 16:9 aspect ratios via script?
  • Or maybe there’s a way to apply the shader just to a canvas instead of the camera, leaving the black bars clean?

I have searched for this and could not find anything, so I would really appreciate any tips or information. Thank you in advance!!!

Update: it just ocurred to me that while I can render the UI animation via the screen space - camera mode, I can have another UI on top with the black bars rendering via the screen space - overlay mode. So, I think I’ve found a workaround :smile:

1 Like

I think you could achieve this by doing something like this:

if (Camera.main.aspect >= 1.7f)
    Debug.Log("16:9");
else if (Camera.main.aspect >= 1.5f)
    Debug.Log("3:2");
else
    Debug.Log("4:3");

However, I have never tried this so don’t sue me if it is totally incorrect lol

You can, indeed, access the aspect ratio this way though. Camera.main.aspect but once again, I’m not totally sure about this but I think it’s basically height divided by width that determines the ratio. So all you need to know is these two elements and from there you can devise a formula to determine the ratio. Plus, if you can do that, you can manipulate it as you wish and create your ratios as needed.

My original answer would have been sorting layer but you already figured that out :slight_smile:

1 Like

Thank you! I am using my “work around” and so far, so good. Will write back if I run into any problems.

1 Like