Home > OS >  Unity 2D Mobile Game scale to multiple screen sizes
Unity 2D Mobile Game scale to multiple screen sizes

Time:12-05

I am developing a 2D platformer game kind of Mario style with some differences. I'm not 100% how to proceed next.

  • Basically I have some platforms that the player can jump on. The level needs to always be the screen size, you need to see the whole map always because you are only fighting in that area and jumping on those platforms.
  • I've created 2 objects in the left and right side called Teleporters which if you enter the left one you'll come out on the right side and viceversa.

When I first installed the game on my Galaxy S10 I noticed that the game doens't fit the whole screen and the blue background shows in the left and right of the screen. Now I could use a longer background image, but that would mess up the game because some platforms are on the edge of the left and right side and it I enlarge the game they will no longer be on the edge.

I'm not sure what options do I have to workaround this.

CodePudding user response:

You basically have 2 viable options I can think of. The simpler and faster one is going into the Camera inspector window and changing the background to a solid black color. The second option is to make a script that changes the width of the background image to the width of the screen (or screen height if the screen is horizontal). You can do that with a simple script that checks for it at the Start() of the game. Not sure how you would place your portals though.

CodePudding user response:

Update:

GameObject Background = GameObject.Find("BG");
        var topRightCorner = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
        var worldSpaceWidth = topRightCorner.x * 2;
        var worldSpaceHeight = topRightCorner.y * 2;

        var spriteSize = Background.GetComponent<SpriteRenderer>().bounds.size;

        var scaleFactorX = worldSpaceWidth / spriteSize.x;
        var scaleFactorY = worldSpaceHeight / spriteSize.y;

        if (KeepAspectRatio)
        {
            if (scaleFactorX > scaleFactorY)
            {
                scaleFactorY = scaleFactorX;
            }
            else
            {
                scaleFactorX = scaleFactorY;
            }
        }

            Background.transform.localScale = new Vector3(scaleFactorX, scaleFactorY, 1);

Found this script that successfully scales the background to full size, now I must figure out the Teleporter issue.

  • Related