Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by goatkaycom · Mar 22, 2019 at 05:25 AM · screen sizes

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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 !

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by LookDev · Oct 27, 2021 at 06:34 PM

Check out these two scripts -

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

https://gist.github.com/fadookie/256947788c364400abe1

Or just check out this video -

https://youtu.be/Oq99C-Jaei8

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

104 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

variables not resetting when reloading scene 1 Answer

2d game for different screen resolutions 1 Answer

Chrome OS detection 0 Answers

Change play previw editor window size to match 1280x800 (android) 1 Answer

game runs faster on bigger screen size? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges