Hello
I’ve been trying to get my gui buttons work on all resolutions for mobile devices.
I use Screen.width and Screen.height for setting the position and width and height of the button.
public float btnWidth = Screen.width / 2;
public float btnHeight = Screen.height /2;
private Rect playBtnRect;
void Start()
{
playBtnRect = new Rect(Screen.width / 2 - btnWidth / 2, Screen.height / 2 - btnHeight / 2, btnWidth, btnHeight);
}
The problem is that it doesn’t return the right values.
I used 480x800 and in the inspector it returns 162x473 which is wired.
Also if I change the resolution to a bigger one the values remain the same.
Idk what to do since it doesn’t return the correct values and if it would update them it might work for multiple resolutions.[/code]
Enlarge game screen, it’ll fix…
You mean to change the resolution or just make the Game window bigger?
Because I tried both and the values are still wrong.
I changed the buttons sizes
public float btnWidth = Screen.width / 2;
public float btnHeight = Screen.height / 10;
The result is 186x44 and no matter what resolution it is the size is the same.
I tested it on my phone it is the same.
Please try this, will see good in all phones. (I don’t have Unity3d now but it must be work)
public float sW;
public float sH;
void Awake(){
sw=Screen.width;
sH=Screen.height;
}
void OnGUI(){
GUI.Button(new Rect(sW/2f-sW*0.3f, sH/2f-sW*0.1f, sW*0.3f, sW*0.1f),"ButtonText");
}
I will give it a try when i get home.
It worked thanks, I think I only had to set Screen.width and height into Awake so it updates it every time.