1
votes

I am working on a 2d project.I am using different sprites of different pixels inside my project.Each sprites are having different height and width not square sides. For example look at the sprite settings in the project.Similarly there are many sprites like this.This light green background is used as football court.It looks ok in ipad view(2732 x 2048 Portrait mode). Sprite settings

But if I change it to iphone6 view(750 x 1334 Potrait Mode).The sprites don't get resized.Still look big.The dark green part cannot be seen.I have added other sprites which also don't change their size.The image is given below.Sprite size not changed

I got a code of getting the screen size which is given below.

public static float GetScreenToWorldWidth
{
    get
    {
        Vector2 topRightCorner = new Vector2(1, 1);
        Vector2 edgeVector = Camera.main.ViewportToWorldPoint(topRightCorner);
        var width = edgeVector.x * 2;
        return width;

    }
}

If I have the screen width how to make use of it to adjust the sprites to various screen resolutions.For this resolution(2732 x 2048 Portrait mode ipad pro) I need not change the sprites size.But for all other screen resolutions change accordingly.

1
It's a matter of configuring your camera. Are you using orthographic projection? (note that for orthographic projection the parameter you can edit is "Size", which represents how many world units the camera can see vertically.)artcorpse
Yes projection- orthographic for main camera.zyonneo

1 Answers

0
votes

You can do something like this on the orthographic camera:

public class MatchCameraToFit : MonoBehaviour
{
    public int Height;
    public int Width;

    public void Awake()
    {
        MatchCamera();
    }

    private void OnValidate()
    {
        MatchCamera();
    }

    private void MatchCamera()
    {
        var cam = GetComponent<Camera>();
        if (cam == null) return;

        var position = cam.ViewportToWorldPoint(Vector3.zero);
        var up = cam.ViewportToWorldPoint(Vector3.up) - position;
        var right = cam.ViewportToWorldPoint(Vector3.right) - position;

        var matchSize = Mathf.Max(Height, Width * up.magnitude / right.magnitude);

        cam.orthographicSize = matchSize;
    }
}

This script will resize the camera such that an object of world size (Width, Height) will always be perfectly matched by the camera horizontally or vertically (depending on which direction doesn't fit).