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!
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
@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
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);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612052027im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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