Build 2D game for any Mobile phone screen size ?

Hi developers,
First of all, forgive me cause I’ve asked very old question.
I’m newbie in Unity 2D game, before ask this question, I read a lot of thread in Unity Answer + Watching instruction video in Youtube. However I cannot build my own game fit to few Mobile Phone Screen Size.
As I saw, I have follow 3 steps:

  1. Scaling the canvas to Fit Screen Size (There are many instruction by video about this point): I DONE by myself.
  2. Scale my background Image fit to any phone Screen size:
  3. Scale my sprites to fit any phone Screen Size: I can’t do this point with Game Object’s localscale Attribute. (After completing Step 2)

Right now I was stuck at Step 2:
Here is my Code:
private void fitBackgroundImg(GameObject bgImg) {

	// Get Device Screen Height & Width
    float screenH = Screen.height;
    float screenW = Screen.width;
    float screenAspect = screenW / screenH;
 
	// Scale Background 
    SpriteRenderer bgSR = bgImg.GetComponent<SpriteRenderer>();
    float bgImageH = bgSR.sprite.rect.height;
    float bgImageW = bgSR.sprite.rect.width;

    float scaleH = screenH / bgImageH;
    float scaleW = screenW / bgImageW;

    bgImg.transform.localScale = new Vector3(scaleW, scaleH, 1);
}

After implement that code, my Background Image was nearly fit it to My Phone screen Size, but NOT not completely fit.

Pls help me at this point.
Thank you very much.

Hi @goatkaycom
The key point are:

  • The graphic of your game displays to people via Camera View Port (that was defined by Camera’s size, aspect and kind of projection).
  • The default size of camera’s view port is different with your Device’s screen (and aspect also).

=> To fit your device screen size, you must do:

  • Change the camera’s aspect = device’s screen aspect.
  • Scale your background image size = camera’s size.

I found the simple & complete tutorial of this issue from youtube: [SOLVED] Full tutorial: Build 2D game fit to any device screen size, resolution in Unity 2018 - YouTube
(By Vivu Standard).

Hope it can help you!

Dear @Natrad ,

Clear & understood !!!

I tried and it worked for me, perfectly :slight_smile:

Thanks for helping.

Hi,

I’ have found that Vivu Standard has enabled the English Subtitles (CC) at this tutorial.

And I don’t know why they didn’t do it before ?

Anyway, that’s great !

Check out these two scripts -

https://gamedev.stackexchange.com/questions/79546/how-do-you-handle-aspect-ratio-differences-with-unity-2d

Or just check out this video -