Attaching Scripts to GUI Components

Hi guys,

I ran into a problem when I tried to store GUI elements like GUI.Toolbar in a variable.

I need to attach a script to some GUI elements to make them accessible via a multitouch table. Actually, I'd like to store the Toolbar in a variable and add the script as a component to it, but it seems this is not the appropriate solution in Unity. I cannot store the Toolbar in a variable, because the return type is an Integer, representing the selected element in the Toolbar.

Is there any possibility to attach scripts to GUI elements I created this way? If not, does anyone have a good advice to look at this from a different angle?

By the way, I'm using C# scripts.

edit: @Jesse Anders I'm trying to attach a script to that toolbar. I have not written this script by myself since it belongs to a set of scripts called uniTUIO. uniTUIO is used to process input events from the TUIO interface, which can be touches from a multitouch table. The implementation is very basic and I didn't even find a proper documentation for how to use their scripts. And now that I know that I have to add a certain script to the objects I want to become "multitouchable", I need to find out how to do so.

edit2: While trying a bit harder to analyze the actual reason why I would need to add that script to a toolbar, I searched around the uniTUIO forums and found a surprisingly simple answer to my question on how to add the multitouch-functionality uniTUIO provides to GUI created by scripts with onGUI. "Sorry - no chance with the regular onGUI stuff." Sad but true.

I need to reconsider faking the Toolbar then. The current way of dealing with this problem seems to be using a GUITexture as a GameObject and modificate it. I have not fully understood how it works, yet.

As for my initial question "How to attach a script to a toolbar created with GUI.Toolbar", it's simply not possible since objects created that way are no GameObjects. Statement's advice using a delegate will do for most purposes, I guess. Thanks a lot for the answer, nevertheless. I'm sure I'll need that pattern anyway.

You can use delegates or classes to build your gui compositionally.

Class example:


[System.Serializable]
class Toolbar
{
    public Rect rect;
    public int selected;
    public GUIContent[] contents;
    public GUIStyle style;

    int OnGUI()
    {
        selected = GUI.Toolbar(rect, selected, contents, style);
        return selected;
    }
}

usage:

class Example : MonoBehaviour
{
    public Toolbar toolbar;

    void OnGUI()
    {
        int selected = toolbar.OnGUI();
    }
}

Functional example:


delegate int Toolbar();
Toolbar MakeToolbar(Rect rect, GUIContent[] contents, GUIStyle style)
{
    int selected = 0;
    Toolbar toolbar = delegate()
    {
        selected = GUI.Toolbar(rect, selected, contents, style);
        return selected;
    };
    return toolbar;
}

usage:

class Example : MonoBehaviour
{
    private Toolbar toolbar;

    public Rect rect;
    public GUIContent[] contents;
    public GUIStyle style;

    void Awake()
    {
        toolbar = MakeToolbar(rect, contents, style);
    }

    void OnGUI()
    {
        int selected = toolbar();
    }
}

If you feel a bit overwhelmed by the functional example, rest assured you won't miss much at all (if any) going with a class approach. The functional pattern can yield some other interesting effects but it is generally quite bulky.