Can I adjust the width and height of a GUILayout.Button?

Hello everyone,

I’m sure it’s possible somehow, but I can’t find it. I love using the GUILayout for button placement, but is there a way I can have any control over the buttons’ height and width? They’re looking rather long and thin right now. =P
This is probably not needed in this case, but here’s a code snippet:

var showMenu : String = "Main";
var buttonWidth : int = 250;
var buttonHeight : int = 50;
var spacing = 25;

function OnGUI(){
	GUILayout.BeginArea(Rect(Screen.width/4, 70, Screen.width/2, Screen.height/1.5));
		GUILayout.BeginVertical();
			switch(showMenu){
				case "Main":
				if(GUILayout.Button("Start Game")){
					Debug.Log("Main Button clicked!");
				}
				GUILayout.Space(spacing);
				if(GUILayout.Button("Customize Robot")){
					Debug.Log("Main Button clicked!");
				}
			}
		GUILayout.EndVertical();
	GUILayout.EndArea();
}

Thanks in advance,
Patrick

1 Like

Yes you can.

According to the documentation :

static function Button (text : string, params options : GUILayoutOption[ ]) : bool

Where the options parameter is :

options : An optional list of layout options that specify extra layouting properties. Any values passed in here will override settings defined by the style.
See Also: GUILayout.Width, GUILayout.Height, GUILayout.MinWidth, GUILayout.MaxWidth, GUILayout.MinHeight, GUILayout.MaxHeight, GUILayout.ExpandWidth, GUILayout.ExpandHeight

So just add this to your code :

if(GUILayout.Button("Customize Robot",  GUILayout.Width(100), GUILayout.Height(100))){
					Debug.Log("Main Button clicked!");
				}
9 Likes

…D’oh, I figured that would override the entire GUILayout area it was in. Thanks! =)

H have same problem,i need help to add button hight on this script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class flashlight : MonoBehaviour
{

private bool Active;
private AndroidJavaObject camera1;

void FL_Start()
{
AndroidJavaClass cameraClass = new AndroidJavaClass(“android.hardware.Camera”);
WebCamDevice[ ] devices = WebCamTexture.devices;

int camID = 0;
camera1 = cameraClass.CallStatic(“open”, camID);

if (camera1 != null)
{
AndroidJavaObject cameraParameters = camera1.Call(“getParameters”);
cameraParameters.Call(“setFlashMode”, “torch”);
camera1.Call(“setParameters”, cameraParameters);
///FIX/////
camera1.Call(“startPreview”);
Active = true;
}
else
{
Debug.LogError(“[CameraParametersAndroid] Camera not available”);
}

}

void OnDestroy()
{
FL_Stop();
}

void FL_Stop()
{

if (camera1 != null)
{
camera1.Call(“stopPreview”);
camera1.Call(“release”);
Active = false;
}
else
{
Debug.LogError(“[CameraParametersAndroid] Camera not available”);
}

}

void OnGUI()
{

GUILayout.BeginArea(new Rect(Screen.width * 0.05f, Screen.height * 0.5f, Screen.width * 0.9f, Screen.height * 0.01f));

if (!Active)
{
if (GUILayout.Button(“ENABLE FLASHLIGHT”))
FL_Start();
}
else
{
if (GUILayout.Button(“DISABLE FLASHLIGHT”))
FL_Stop();
}
GUILayout.EndArea();
}
}

EVERYTHING WORKS BUT I NEED TO ADD BUTTON HEIGH BECAUSE ITS SO THIN