1

My game is on 2D landscape format and I wanted to scale my game screen size based on mobile device screen. I tried different code but to no avail. This is the first script I tried

[ExecuteInEditMode]
[RequireComponent(typeof(Camera))]

public class MatchWidth : MonoBehaviour 
{
    public float sceneWidth = 25;

    Camera _camera;
    void Start() 
    {
        _camera = GetComponent<Camera>();
    }

    void Update() 
    {
        float unitsPerPixel = sceneWidth / Screen.width;

        float desiredHalfHeight = 0.5f * unitsPerPixel * Screen.height;

        camera.orthographicSize = desiredHalfHeight;
    }
}

and the other script I tried

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ChangeScreenSizeBasedonDevice : MonoBehaviour
{
    // Use this for initialization
    public float screenHeight = 1920f;
    public float screenWidth = 1080f;
    public float targetAspect = 9f / 16f;
    public float orthographicSize;
    private Camera mainCamera;


    void Start()
    {

        mainCamera = Camera.main;
        orthographicSize = mainCamera.orthographicSize;

        float orthoWidth = orthographicSize / screenHeight * screenWidth;
        orthoWidth = orthoWidth / (targetAspect / mainCamera.aspect);
        Camera.main.orthographicSize = (orthoWidth / Screen.width * Screen.height);
    }
}

The first one has a problem with its height it has a space on top and bottom and the second one zoom in too much. Can someone point where did i go wrong or who has a better code. I placed the both script on Main Camera

UPDATE I tried also Saif says in this link https://gamedev.stackexchange.com/questions/79546/how-do-you-handle-aspect-ratio-differences-with-unity-2d but the result is still the same as script 1. here is the pic:

enter image description here

What do I need to remove the space or margin at top or bottom

UPDATE 2

Solve the margin issue using this

void Start()
{
    float screenWidth = GameManager.Instance.getScreenWidth();
    float screenHeight = GameManager.Instance.getScreenHeight();

    if (gameObject.name == "Cube")
    {
        transform.localScale = new Vector3(screenWidth / 4, screenHeight, -1);
        transform.position = new Vector3(transform.position.x, 0, transform.position.z);
    }
}

attached to gameobject to fit screen

6
  • If you are using a single mobile device, then get the resolution of it and add it to Unity by clicking on "Game" View -> Click on "Free Aspect" -> Click on "+" and add the width and height of the mobile screen. Now any UI elements you set here will be reflected as the same on your mobile screen. Commented Sep 16, 2019 at 15:02
  • Solution 2: If you are using canvas UI, then click on canvas object and change the Canvas Scaler; UI scale mode from "Constant Pixel Size" to "Scale with screen size" Commented Sep 16, 2019 at 15:03
  • @Saif I'll publish the game so i expect that it will be use by different mobile device. I have no problem with the UI scaling. just the background and prefab stuff. Commented Sep 16, 2019 at 15:07
  • Check this link out, it may answer your question. gamedev.stackexchange.com/questions/79546/… Commented Sep 16, 2019 at 15:10
  • Another solution: youtube.com/watch?v=2uYkom__5zo&feature=youtu.be Commented Sep 16, 2019 at 15:13

1 Answer 1

2

Your Camera's projection probably is Orthographic. I'll give you an easy solution to this.

using UnityEngine;

public class ScreenManager : MonoBehaviour
{
    static public ScreenManager SM { get; set; }

    private void Awake()
    {
        SM = this;
    }

    public float getScreenHeight()
    {
        return Camera.main.orthographicSize * 2.0f;

    }
    public float getScreenWidth()
    {
        return getScreenHeight() * Screen.width / Screen.height;
    }

}

You can call these two functions anywhere after you put this script to a gameobject.

Example

For example;

Lets say you want a gameobject to be half size of the screen height and place on the middle-left of the screen. (And lets set its width to 4/screen width size)

public GameObject AnObject;



 void Start()
    {
      AnObject.transform.localScale = new Vector2(ScreenManager.SM.getScreenWidth()/4,
 ScreenManager.SM.getScreenHeight() / 2);

     AnObject.transform.position = new vector2(-ScreenManager.SM.getScreenWidth()/2,0);
    }

Note: If you want scaling to work perfectly, you have to set image pixel per unit in import settings correctly. For example, if an image is 1024x1024, you want to set your pixel per unit for that image to 1024.

Example2

Sign up to request clarification or add additional context in comments.

4 Comments

Your statement about pixels per unit being 1024 for the 1024x1024 image is not helpful. The image would only consume 1x1 world unity if the ppu is set to 1024. Pixels per unit should be set accordingly for every image so that the size of the image correlates to the the world units it represents. The pixels per unit should be the same for all of your sprites (or multiples of 2, otherwise), not set according to each sprite's pixel count. Otherwise you have wonky resolution images in different sizes and bleeding pixels.
You gotta set the pixel per unity exactly the same. As you said, the image should consume 1x1 world unit for my solution to work. I'm not sure if setting pixel per unit effect the resolution or causes bleeding pixels. I've been doing this solution for a long time, i did not had any issues with the quality of my games. But again i might be wrong.
Pixels per unit means exactly what it says. "Pixels/unit" If you have a "Castle background" image at 1024x1024, and a "Mario" image at 64x64, then the castle should be 16 times bigger than mario. If you mapped the Castle to 1024 ppu, and mario to 64 ppu, then they would be the same size and you'll have WAY mroe resolution on the castle than you would Mario. You are misunderstanding what Pixels per unit does.
Hi @FaTaLL can you please suggest for perspective view.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.