- Home /
Viewport ratio appears inconsistent in 2D game
I've been searching for a solution on this everywhere I can, so hopefully this isn't a "duh" thing that was in the FAQ.
The problem is that the ViewPort's handling of W over H are never consistent with the true/actual aspect ratio of my game assets. I am using the 2D workflow since this is a 2D game.
The reason I'm saying that it's inconsistent is that my "background" image is 640 by 480 which is a ratio of H = 0.75 * W (4:3), yet in order to "trick" the viewport into displaying the correct way, I need to tell it to use W = 0.59 and H = 1.0 which is definitely not correct.
Nothing I've tried has been able to fix the issue. Furthermore, the viewport gets all messed up when actually running the game (becomes more like W=0.5 H=1.0), but that appears to be a separate issue.
Answer by Wisteso · Nov 17, 2013 at 12:35 AM
I seemed to have solved the problem. After looking at some other posts, it seemed that ViewPort W and H are multiplied by the screen width and screen height, which would definitely explain the issues for me.
The problem is that the Inspector has no way to specify any alternative modes. Why the 2D workflow would not offer an alternative "absolute ratio" mode is beyond me, because the default mode does not work at all for "board game" type games. The fix required the following code in my start() handler...
this.camera.pixelRect = new Rect(0, 0, (int)(Screen.height / 0.75), Screen.height);
I use 0.75 because I want a 4:3 ratio, but you would adjust this to whatever you need your ratio to be.
And then you'd want to just completely ignore what the ViewPort W and H get adjusted to as a consequence of the above code since board games don't care about resolution-dependent-ratios. (it changed mine 0.59 to 0.5904335 since 0.59 was my approximation/workaround)
The reason Unity doesn't have a "absolute ratio" mode, is basically because stretching is generally bad. It's not very friendly to other devices and will make your game look really bad :)
Sure, I get that, but stretching would be optional. Some games wont look bad with stretching, and some games are fine with black bars in the unused space.
True, however how many games do you know which acctually need this feature? It's a very little amount and Unity should rather spend their time fixing important bugs and adding useful features, rather than adding a "absolute ratio" mode you can implement yourself...
You should accept your answer btw ;)
I think a lot of the new 2D games that we'll be seeing will desire this feature. Any game with a fixed size viewport will want it in order to work correctly on any size screen. Anyway, thanks for the comments and the re$$anonymous$$der!