So For one can someone tell me what each number means(ex. (50, 70, 50, 75)which is x and y (first 2 right?)(next 2 scale right?)). Then can you please tell me if there is an easier way to place buttons.
Im using this script:
@script ExecuteInEditMode;
function OnGUI() {
if (GUI.Button(Rect(50,70,50,75),“Level 1”)){
Application.LoadLevel(“lvl1”);
}
if (GUI.Button(Rect(50,50,200,75),“Level 2”)){
Application.LoadLevel(“lvl2”);
}
if (GUI.Button(Rect(50,0,50,75),“Level 3”)){
Application.LoadLevel(“lvl3”);
}
if (GUI.Button(Rect(50,70,50,75),“Level 4”)){
Application.LoadLevel(“lvl4”);
}
if (GUI.Button(Rect(50,70,50,30),“Level 5”)){
Application.LoadLevel(“lvl5”);
}
if (GUI.Button(Rect(10,70,50,30),“Level 6”)){
Application.LoadLevel(“lvl6”);
}
}
The GUI.Button works like this:
if ((GUI.Button (new Rect (buttonPosition.x, buttonPosition.y, buttonSize.x, buttonSize.y), buttonTexture, GUIButtonStyle))
{
//button press logic
}
I tried to name the variables so they are self explanatory. You mentioned scale for one of the numbers which is incorrect.
buttonTexture could be replaced by a text string, generally you would want a texture there that looks like a button though.
GUIButtonStyle is a public GUIStyle variable for example:
public GUIStyle GUIButtonStyle;
You should try to format your questions though with the “Code Sample” button while you highlight the code as others mentioned.
Generally you should be able to figure out the basic questions with the documentation, but I’ve had a lot of trouble with learning effectively from the documentation when I was first learning all the syntax, so I’ll give you the benefit of the doubt.
For example the Rect information that you need:
Can not be seen directly on the GUI.Button page:
All it says is “static bool Button(Rect position, Texture image);”.
“Rect position” by itself may be a little misleading for a beginner because it does not make any mention of size.
Yeah I don’t do things that way. I just create an GUI Texture object and call it my button. Then I add a script to it like so to enable mouse rollover to change the buttons image to highlighted and such.
function OnMouseEnter () {
guiTexture.texture = CBHover;
}
function OnMouseExit () {
guiTexture.texture = CBNormal;
}
Then when you want to do something you put it in a OnMouseUp function. To disable the button just do
gameObject.Find("ButtonName").guiTexture.enabled = false;
Also note that this can’t be viewed in the scene view tab. You have to go to the game tab to see the gui textures in work.