GUI.Toolbar with deselect option

Is there a simple way to create the GUI.Toolbar with deselect option, so that clicking a selected button would deselect it and set the .selected index to -1 or similar?

3 Answers

3

Try this one:

var toolbarInt : int = -1;
var lastbarInt : int;
var toolbarStrings : String[] = ["Toolbar1", "Toolbar2", "Toolbar3"];

function OnGUI () {
    toolbarInt = GUI.Toolbar (Rect (25, 25, 250, 30), toolbarInt, toolbarStrings);
    if(GUI.changed){
        if(lastbarInt == toolbarInt){
            toolbarInt = -1;
        } 
            lastbarInt = toolbarInt;
    }
}

Thanks. Almost the same as I've created myself, but mine was missing the condition (GUI.changed) and that is probably why it was not working as expected.

–

Sure, you could have a button that sets a bool such as Toolbar active. Then in your on GUI method check the bool before drawing the tool bar

Code untested.

public class GUIManager : MonoBehaviour{
private bool toolbarActive;
void OnGUI()
{
    //show the topmost menu if we have one
    if (toolbarActive())
    {
        //Draw your toolbar here
    }
    //this would be the button control separate from your toolbar
    if (GUI.Button(new Rect(10, 10, 50, 50), btnTexture))
       toolbarActive = !toolbarActive;
}
}

You must code it by yourself, or try to search in the wiki