- Home /
How can I make my mobile game compatible for all screen sizes?
So I am working on this mobile game and I need to spawn some sprites in 4 different areas of the screen, specifically the ones created by drawing the 2 diagonals of the screen. I need to display the sprites in a certain order to form words that can be read from left to right for the top and bottom words and top to bottom for the words on the left and right hand side of the screen. The only problem is that when I try to Instantiate the sprites, I can't spawn them in the same place for different screens. For example, when I am testing the app on my computer through unity remote, the height of the screen is 600 pixels and I can display the top word by filling Screen.height - Screen.height/10 for the Y coordinates, and it is fine, bun when I build the app and run it on a physical device, where the height of the screen is 1920, or any value bigger than 600, everything gets messed up. I will post below an example of my problem, with pictures. So, let's focus only on the word that is supposed to be displayed at the top(i have some additional text lines in the game so I know that the word is displayed correctly) since I think that if the top word can be fixed, the others can be fixed too. This is how the top word should look like on an actual device:
It should be displayed somewhere in that area, where that P sprite is ( I made the game display all the sprites for a word one on top of the other so I can deal with the position on the X axis later). To get this result, I am setting the Y coordinate at Screen.height/2 + Screen.height/8 . I can't say exactly why I am using this value...it just seems right for it to be there. This is how the game looks on my computer, on a 600 px tall game view, with that value for the Y coordinate:
If I want the word to be displayed correctly in my game view on my computer, I am using Screen.height/2 + (Screen.height/11*4), which makes the top word vanish if I build the game and run it on my phone ( a Oneplus One, if it matters), the word having its Y coordinate too big, going out of the frame. How can I fix that? What I've done so far, after spending at least 30 hours on this problem: I made a method called myPosition, that returns a Vector3 with the position at which a sprite should be spawned and I am using that position when I am instantiating the prefab. This is the method:
Vector3 myPosition(int direction, int letterIndex,int length)
{
Vector3 final = new Vector3(Screen.width/2,Screen.height/2,0);
float space=0;
switch(direction)
{
case 0:
{
final += new Vector3(0, Screen.height/11*4, 0);
space = Screen.width / Letter.GetComponent<Transform>().localScale.x;
}break;
case 1:
{
}
break;
case 2:
{
final -= new Vector3(0, Screen.height / 8, 0);
}
break;
case 3:
{
}
break;
}
return final;
Let me explain my logic now. The direction parameter indicates wether I need to get the position for the top, right, bottom or left word, by having the values 0,1,2 or 3. The letterIndex is quite obvious and the length parameter is the length of the word, so I can determine how much space to leave between the sprites. I have figured out a mathematical equation that can position all the letter correctly on the screen, on the X axis and also on the Y axis, for the left and right words, but it only works on that 600 px tall game view on my computer and on my phone, the sprites are scattered everywhere and most of the times I can't see any of them because of the space between the letters. I have also though about storing the most common screen resolutions in 2 arrays for width and height and then storing the X and Y values for the words in another 2 arrays, but I want the game to be compatible with every device.
Your answer
Follow this Question
Related Questions
Screen scaling problem 1 Answer
2D Gameobject Location and scaling in different screen size(mobile) 0 Answers
Using JS class in C# 1 Answer
Asset bundles for use on both PC/Mac? 1 Answer
Unity, Xcode, Compatibility List? 1 Answer