2
\$\begingroup\$
  • (Desktop) I want to make it so that when the user re-sizes the game window, the game is stretched to fill the window.

  • (Similarly, for Android) I want it to be stretched to fit the screen as well.

The game's real resolution is supposed to be 720x1280 (16:9 equivalent). In the image on the left you can see the actual resolution.

I can see that unity automatically re-scales it everytime the game window is re-sized, but it kind of keeps the same aspect ratio. This results in either black bars or part of the game being off-screen. How can I scale my game based on the current window aspect ratio ? I know the graphics will get distorted.

1

Isn't there a way to stretch the game without keeping the same aspect ratio, within the unity interface ? If not, how could I do that within a script ?

In xna or libgdx, I would simply add a scale matrix on every spriteBatch.Begin() method call. That scale would be based on the (current window width, current window height) and the ("real" game width, "real" game height).

For example, since my game is 720x1280, if you had your window re-sized at 500x500, I would scale the graphics by 500/720 on X axis and 500/1280 on Y axis. And then I would have to scale the inputs by 1/scale matrix. I need to know how this works on unity. Perhaps, setting Camera.main.aspect to the current aspect ratio of the current window (I could calculate that everytime the window is resized), would solve the problem (?), but is there another option ?

I assumed that supported aspect ratios would do just what I needed. But when I re-size my game window to 720x1080 (4:3 equivalent) down from 720x1280 (16:9 equivalent) it just adds black bars. When I want the game to stretch, in all cases.

2

\$\endgroup\$
3
  • \$\begingroup\$ Hey, you are using the same background as I am :) \$\endgroup\$
    – AgentFire
    Commented Jul 12, 2015 at 12:34
  • 1
    \$\begingroup\$ @AgentFire, Sorry dude, I don't mean to be rude, but your comment doesn't really help the community, so who cares if he is using the same background as you are? Does that answer his question? Comments like that should be reserved in the forums. It is not welcome on Q&A answer site like this. Remember most people reading this site are busy working people, they want answers/explanations, not your antics. \$\endgroup\$
    – Neon Warge
    Commented Apr 14, 2016 at 0:09
  • 1
    \$\begingroup\$ @NeonWarge okay I forgive you. <3 \$\endgroup\$
    – AgentFire
    Commented Apr 14, 2016 at 7:50

1 Answer 1

2
\$\begingroup\$

The camera will normally render with an aspect ratio equal to Screen.width / Screen.height but this can be overruled.

This script will have an effect similar to taking 16:9 image from the camera then stretching it to fill the screen. This will of course cause some distortion.

using UnityEngine;

public class CameraAspect : MonoBehaviour
{
    public float width = 16f;
    public float height = 9f;

    void Awake ()
    {
        Camera.main.aspect = width / height;
    }
}
\$\endgroup\$
3
  • \$\begingroup\$ I don't think you understood my question. What I want to do is set the aspect ratio accordingly so that the game stretches to fit the screen. So camera.main.aspect would need to be set every time the game window is re-sized according to the game's window. Also, camera does not render according to screen width / screen height, because if it did, then part of the game would not be off-screen on my android phone. The actual resolution is 9:16 and it is windowed, so screen size doesn't matter (it does on mobile). Please read my question in detail, I explained everything very clearly. \$\endgroup\$
    – dimitris93
    Commented Mar 23, 2015 at 18:24
  • 1
    \$\begingroup\$ Did you try the sample code to see the effect that it has? \$\endgroup\$ Commented Mar 23, 2015 at 18:30
  • 1
    \$\begingroup\$ You are actually correct. My game has 9:16 ratio (portrait for mobile). So, when I tried with 16:9 values, it didn't work, and I started making false assumptions. I didn't think it would be this easy. I am used in different kind of programming when it comes to games, unity is new to me. Thanks a lot. \$\endgroup\$
    – dimitris93
    Commented Mar 23, 2015 at 18:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .