Hi forums,
I am new to Unity and new to scripting (learning Python, but very new to Java Script)
I am trying to create a menu button (button1) that sits ‘in game’ all the time, and when pressed, reveals a sub-menu of eg. 4 more buttons that also sit ‘in game’. That sub-menu remains and is functional etc, until button1 is pressed again and the sub-menu disappears.
Im having trouble implementing the concept…
have gone through:
using a standard button which turns the sub-menu on, then off again
using a toggle that looks like a button style
using a toolbar (but i cant seem to find info on whether those toolbar buttons are toggles? 0/1)
has anyone got an insight into what is the best method to go for?
im sure this is noob stuff, but hopefully someone can help out a suffering artist when it comes to scripting
thanks in advance for the help - i really do appreciate it!
That’s quite easy with Unity.
Conceptually what you’re looking for is to implement a small state machine to control whether or not your sub-menu is displayed or not. Something as simple as…
private var submenuDisplay = false;
Then when your button is clicked just toggle that boolean to enable the display of the subsequent buttons.
function OnGUI () {
if (GUI.Button (Rect (10,10,150,100), "Menu")) {
submenuDisplay = !submenuDisplay;
}
// Only display the submenu buttons when user has clicked initial button
if (submenuDisplay == true) {
if (GUI.Button (Rect (10,10,150,100), "Button 2")) {
print("Button 1);
}
if (GUI.Button (Rect (10,10,150,100), "Button 2")) {
print("Button 2);
}
}
}
Dynamic control over your gui in this fashion is quite easy once you get used to it, and quite powerful.
wow, thanks Quietus!
such a fast response with a simple solution! This has cleared a few things up for me, will implement and do some investigation ASAP
thanks again
AA
Hi,
Just FYI specifying style with a string parameter is quite slow. Since it instantiates GUIStyle class that does hashtable lookup in available styles.
SO instead do this once (setup global variable or some other way you prefer):
GUIStyle button1Style = "Button";
void OnGUI()
{
GUILayout.Button("button", button1Style);
}
Cheers!