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 /
  • Help Room /
avatar image
0
Question by DroidifyDevs · Dec 10, 2015 at 02:07 AM · canvasgameobjectsscreen sizes

How to make game work on ALL screen sizes?

Hello!

I have been developing a simple 2D game, and worked so hard at it. Was pretty proud until I realized that it wouldn't scale the game to smaller screen sizes. However, the canvas elements (text) move to the edges of any screen size. This is hard to explain, please look at the pictures here: http://imgur.com/a/bqi14

As you can see, on the large screen size, all the objects and UI elements show, but when I make a smaller screen, the object sizes remain the same, meaning that much of the game isn't visible. Also, notice how the "score" stays on the left, but the top-right "hamburger" button doesn't?

So question: How to make the game scale to any screen size? THANKS!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by itsharshdeep · Dec 10, 2015 at 04:48 AM

I think in you screenshots the canvas is working fine, But the rest of the things are not working correctly like the Rocket, BG, fuel size. As per my observation these object should scale according to the screen resolution .. I generally used the approach like make all the Sprite Renderer (2D elements) to the child of one major parent. and then scale the parent according to Screen height & width. ( the above approach is not for Canvas)

Note :- Kindly check the output in any resolution expect in 'Free Aspect'.

Attached script should be on the parent of all the BG or the elements which you want to scale

 using UnityEngine;
 using System.Collections;
 
 public class ScaleForDevice : MonoBehaviour {
 
     public float resoulutionFactor ;
     float _height = Screen.height;
     float _weight = Screen.width;
     float scaleFactorW;
     float scaleFactorH;
     void Awake () {
 
                 float _height = Screen.height;
                 float _weight = Screen.width;
                 float scaleFactorW = _weight / 1024; //1136
                 float scaleFactorH = _height / 768;
         resoulutionFactor = scaleFactorW / scaleFactorH;
 
 #if UNITY_IOS
 
     if(iPhone.generation == iPhoneGeneration.iPhone4 || iPhone.generation == iPhoneGeneration.iPhone4S) 
         {
         if (scaleFactorW > scaleFactorH) {                        
             this.transform.localScale = new Vector3 (scaleFactorW / scaleFactorH, 1, 1);
         }
     }
 #endif
         #if UNITY_ANDROID  || UNITY_EDITOR
 
         if (scaleFactorW > scaleFactorH) {                        
         this.transform.localScale = new Vector3 (scaleFactorW / scaleFactorH, 1, scaleFactorW / scaleFactorH);
         }
 #endif
 }
 }
 

The above code may vary sometime, we might have to handle it some time. https://www.dropbox.com/s/4xmqcx5dll0nf20/UnityAnswer_scaleForDevice.gif?dl=0

Comment
Add comment · Show 1 · 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 itsharshdeep · Dec 11, 2015 at 04:37 AM 0
Share

@putin2001: Sorry to say but next time kindly reply on the appropriate answer ins$$anonymous$$d of the posting a new answer. And I have one query :- You have placed this script on your canvas or the sprite renderer? If ( yes ) then canvas has inbuilt canvas scaler script so no need to write your own.

For making the player in camera bonds. $$anonymous$$indly check this tutorial. You can do the whole if you need.

And for making the objects to instantiate in screen then you can take reference from the following code:-

 public void RepositioningOfCoins()
     {
         
         float x = Random.Range (2f, 6f);
         float X = x* ScaleForDevice.resoulutionFactor;
         float Y = Random.Range ($$anonymous$$Y, maxY);
         coins [currentIndex].gameObject.GetComponent<SpriteRenderer>().enabled=true;
         coins [currentIndex].transform.position= new Vector2(coins[index].transform.position.x+X,Y);
         index = currentIndex;
         currentIndex++;
         if (currentIndex == coins.Length)
         {
             currentIndex=0;
             index=coins.Length-1;
         }
     }

But I'm not sure whether it will work or not so kindly check

avatar image
0

Answer by DroidifyDevs · Dec 10, 2015 at 03:51 PM

@itsharshdeep Thanks so much! The script worked to make the canvas elements fit, and also the objects get smaller as the screen gets smaller.

However, my scripts that move the player (the rocket) still make it move off-screen. Also, the instantiation script (to spawn objects that fall from above)makes the objects go off-screen. How to make the sizes in scripts screen-based? For example, here is my player controller script:

 using UnityEngine;
 using System.Collections;
 
 [System.Serializable]
 public class Done_Boundary 
 {
     public float xMin, xMax, zMin, zMax;
 }
 
 public class Done_PlayerController : MonoBehaviour
 {
     public float speed;
     public float tilt;
     public Done_Boundary boundary;
 
     
     
     void Update ()
 
         {
             transform.Translate(Input.acceleration.x / 3, 0, 0);
         }
     void Start ()
 
     {
         GetComponent<Rigidbody>().velocity = transform.right * speed;
     }
 
 void FixedUpdate ()
     {
         float moveHorizontal = Input.GetAxis ("Horizontal");
         float moveVertical = Input.GetAxis ("Vertical");
 
         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
         GetComponent<Rigidbody>().velocity = movement * speed;
         
         GetComponent<Rigidbody>().position = new Vector3
         (
             Mathf.Clamp (GetComponent<Rigidbody>().position.x, boundary.xMin, boundary.xMax), 
             0.0f, 
             Mathf.Clamp (GetComponent<Rigidbody>().position.z, boundary.zMin, boundary.zMax)
         );
         
         GetComponent<Rigidbody>().rotation = Quaternion.Euler (0.0f, 0.0f, GetComponent<Rigidbody>().velocity.x * -tilt);
     }
 }
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

33 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

Related Questions

Fade out an object cause every other object to fade out 1 Answer

Keep UI object disabled unless it's inside canvas 0 Answers

OnPointerEnter blocked by something 2 Answers

Does using multiple World Space Canvases affect performance? 0 Answers

Playing a .mp4 video file after Scene change without Unity Pro? 0 Answers


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