How to scale unity GUI to fit different screen sizes

I don’t have code because I am stuck. No matter what I did was a huge failure. All I want is to center my GUI buttons (they’re created using scripts and using GUI.Button) so that they are not too big on small screens and not too small on big screens, that’s all I want, is for it to fit all resolutions, this game is being developed for Android at the moment.

I tried reading on Matrix but there isn’t enough information on how to do it in the documentation. This is frustrating :expressionless:

Well, you have Screen.width and Screen.height, so with these two values, you can theoretically fit any GUI elements to the screen size. If a button should always be 10% of the pixels the screen is wide, then the button’s width would be Mathf.RoundToIn(/float)Screen.width / (float)10). Just an example. For centering elements, you would just use Screen.width - (yourGUIelement.width / 2), and so on.

If all your buttons will have the same relative size (each one has the same relative dimension as all others), then it might make sense to just create different GUI skins for different screen sizes, and decide which one to use at the start of the game or whenever the resolution is changed. One might be for very small screens and have it’s button style’s dimensions accordingly smaller, etc.

Maybe, you need use GUI.matrix (write on CSharp):

 //Define original resolution for your GUI elements
 public float DesignWidth = 1280.0f;
 public float DesignHeight = 720.0f;
 
 void OnGUI() {
  //Calculate change aspects
  float resX = (float) (Screen.width) / DesignWidth;
  float resY = (float) (Screen.height) / DesignHeight;
  //Set matrix
  GUI.matrix = Matrix4x4.TRS(new Vector3(0, 0, 0), Quaternion.identity, new Vector3(resX, resY, 1));
  //Draw GUI elements
  //Button (Size: 200x150) in center define screen
  if (GUI.Button(new Rect(DesignWidth/2 - 200.0f/2, DesignHeight/2 - 150.0f/2, 200, 150), "It's button")) {
   //Do something
  }
 }

But, maybe, you need create GUIStyle too. I hope that it will help you.