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:
- Scaling the canvas to Fit Screen Size (There are many instruction by video about this point): I DONE by myself.
- Scale my background Image fit to any phone Screen size:
- 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.