- Home /
Same button position in all Android devices
Hello everyone, my problem is that I can't configure out how to adjust a button in the same position in all android devices. I use Screen.width and Screen.height with variables like, Screen.height / 2 - x etc... Is there any way to do it?
Thanks in advance. Any help is appreciated :)
Hello well what i use is the scale and height with respect to width and height of the screen. For Example: GUI.Button(new Rect(Screen.width/2,Screen.height/2, Screen.width/8, Screen.height/8),"Hello");
Answer by robhuhn · Aug 05, 2013 at 02:23 PM
If you want to use dp instead of px you could use that script "DisplayMetricsUtil" http://answers.unity3d.com/questions/337460/scaling-gui-buttons-to-be-the-right-size-no-mobile.html
The script provide some extension methods for converting dp to px and px to dp:
if(GUI.Button(new Rect(Screen.width - 110f.DpToPixel(), Screen.height - 30f.DpToPixel(), 100f.DpToPixel(), 20f.DpToPixel()),"MyButton"))
No need to convert Screen.width because it's the full width of the device.
Answer by Joyrider · Aug 05, 2013 at 01:30 PM
You seem to have given the answer yourself.. and ahaykal gave an example of what you said...
Just another exemple taking into account the button's size: If you'd want a button to always be at 5 pixels from the lower right corner and your button was 100x20, you'd write (in c#):
if(GUI.Button(new Rect(Screen.width - 5f - 100f, Screen.height - 5f - 20f, 100f, 20f),"MyButton"))
{
}
You could also add a resizing factor (horizontal screen width for exemple) to resize your buttons. If your base resolution would be 1024x768 for exemple you'd write
float factor = Screen.width/1024;
if(GUI.Button(new Rect(Screen.width - factor *5f - factor *100f, Screen.height - factor *5f - factor *20f, factor *100f, factor *20f),"MyButton"))
{
}
The button would always be in the lower right corner and it's size and distance to the border would be proportially changed to the horizontal resolution of your screen.
On a 2048x1536 (ipad retina) or 2048x768 (doesn't exist) screen, your button would be twice as big and twice as far from the border (10px)