GUI.Button creates a button for you - but you already know thatā¦
You want two new buttons? Call GUI.Button function twice with appropriate arguments.
You use only one OnGUI function to handle all gui inside it.
if (GUI.Button (Rect (620,300,150,20), āButton oneā)) { then create two new buttons.
if (GUI.Button (Rect (620,300,150,20), "Button one")) {
if (GUI.Button (Rect (620,300,150,20), "SubButton one")) {
//Do some stuff here
}
if (GUI.Button (Rect (620,300,150,20), "SubButton two")) {
//Do some other stuff here
}
}
so the code you wrote if we press button one two sub buttons gonna show? and yeah we will put something in sub buttons like Application.LoadLevel (2); or Application.Quit but i am confused about if we click on button one how it would show sub buttons
No it wonāt. My mistake. Checked out documentation and turns out the way it works is every frame OnGUI is called and GUI.Button will draw (or redraw rather) the button every frame so in code above our sub buttons would exist only for one frame after button one was clicked.
It actually should be:
void OnGUI()
{
static bool butOneClicked = false;
if(GUI.Button(new Rect(0,0,100,100), "Button one"))
{
butOneClicked = true;
}
if(butOneClicked)
{
if(GUI.Button(new Rect (100,100,200,200), "SubButton one"))
{
//Do some stuff here
}
if(GUI.Button(new Rect(0,0,100,100), "SubButton two"))
{
//Do some other stuff here
}
}
}
Except C# doesnāt have method scope static variables. Is there an alternative or does it have to be class scope?