Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Letterboxing and Pillarboxing

Discussion in 'Editor & General Support' started by Jessy, Sep 8, 2007.

  1. Jessy


    Jun 7, 2007
    I am pretty sure this needs to go in the Wish List section, but I figured I would ask if the functionality is hidden somewhere that I haven't found yet. My search of the forum did not turn up any answers.

    I want to be in control of my game's aspect ratio. For many games, being able to use the full area of the screen is more important than maintaining aspect ratio. Unity is great at handling that sort of thing.

    However, I am a big fan of the Unity camera functions that allow you to turn screen coordinates into game coordinates and vice versa. Different screen aspect ratios lead to distorted appearances when using these functions. It would nice to be able to constrain the aspect ratio in order to alleviate this problem, as well as for cinematic effect. Can this be done?
  2. Eric5h5


    Volunteer Moderator Moderator

    Jul 19, 2006
    It's not necessary to do that in order to deal with the "distortion" problem. You just have to check Screen.width and .height for the aspect ratio and multiply the width of any gui stuff appropriately. (See some of the Start code here for an example.)

    However, if you want to do widescreen stuff on non-widescreen monitors for cinematic effect, or maybe pillarboxing on widescreen monitors for a multiplayer game so nobody has an unfair advantage or something, it's just a matter of setting up the normalized view port rect for the camera. (And make sure you check the aspect first, of course, in case the user's already running widescreen.) A rect of (0, .1, 0, .9) would make a 4:3 screen letterboxed, for example.

    I've also found that you should have two cameras if you're doing this, the one on top being the main camera with letterboxing or whatever, with clear flags set to don't clear, and then another normal one underneath that renders nothing except a black background color. The reason for this is that some graphics cards apparently display garbage outside the bounds of the main camera without the second camera (even if the main camera clear flags are set to a solid color).

    One thing to be careful about when messing with the normalized view port rect is that some screen functions can return "wrong" values since the rect isn't (0,0,1,1) anymore, like ScreenPortToRay. For example, I have a game that forces letterboxing, and I had to use something like

    Code (csharp):
    1. var sRay = camera.main.ScreenPointToRay(Input.mousePosition);
    2. if (isLetterboxed) {sRay.direction.y *= .8;}
    in order to compensate when doing raycasting from the mouse pointer.