- Home /
Problem in setting GUI Button for mobile device
I want to position my gui button to bottom right corner of mobile devices.
But i am not getting the way to position my button which applies to all my devices with different resolution.
If you guys have any idea , then please help me.
THanks for your support and help in advance.
Answer by Hotshot10101 · Mar 28, 2013 at 01:47 PM
Usually I will use code like the following:
void OnGUI
{
int buttonWidth = Screen.width * 0.1f;
int buttonHeight = Screen.height * 0.1f;
if (GUI.Button (new Rect (Screen.width - buttonWidth, Screen.height - buttonHeight, buttonWidth, buttonHeight), "button text"))
{
// do button stuff here
}
}
Obviously there are things you can do to optimize so you are not doing the calculation every OnGUI, but only if you know the screen size is not going to change or you handle a screen size change event and do the calculations there.
I forgot to mention in case it isn't obvious that this will make a button that is 10% the height and width of the screen. You can do all kinds of calculations to get the buttons to be whatever size you desire based on the screen size.
Your answer
Follow this Question
Related Questions
Drag and Drop button on Mobile (Messenger style) 0 Answers
Button and resolution problem 1 Answer
Help with GUI clicking ??? 1 Answer
GUI.Button works in editor but not on Mobile device 1 Answer