- Home /
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: 4. 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.
Answer by Natrad · Mar 25, 2019 at 02:37 AM
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: https://youtu.be/2uYkom__5zo (By Vivu Standard).
Hope it can help you!
Answer by goatkaycom · Mar 25, 2019 at 05:07 AM
Dear @Natrad ,
Clear & understood !!!
I tried and it worked for me, perfectly :)
Thanks for helping.
Answer by multipurposejack · Jan 14, 2021 at 04:48 AM
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 !
Answer by LookDev · Oct 27, 2021 at 06:34 PM
Check out these two scripts -
https://gist.github.com/fadookie/256947788c364400abe1
Or just check out this video -