- Home /
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.
GUI controls aren't objects, and they can't be stored (not that I know of, at least). As for the problem you're trying to solve, could you perhaps edit your post and explain it in a bit more detail? Are you wanting to modify the behavior of the toolbar? And if so, in what way?
I have actually made wrappers for all GUI controls in the past. It's quite easy and it gives you customization abilities since the classes can be serialized. That way you can modify individual gui objects through the inspector. The key is to store the state of a gui call in member variables.
Answer by Statement · Jan 10, 2011 at 10:08 PM
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.
First of all, thank you very much. I come from Java and had no idea that delegates are handled this way in C#. Unfortunately, I have yet to solve the problem of adding a script to this toolbar delegate. The delegate itself works fine, but as it isn't a GameObject (and I can't cast it into one), I cannot add the script with toolbar.addComponent("$$anonymous$$yScript"), obviously.
Well actually, the delegate/functional method I showed make use of currying to embed a higher order function inside a function with less (well, none at all) parameters. No, you can't add a delegate to a game object like this. It only acts as a variable you can "pass around". In this example it would be meant to add "Example" to your game object. Note that my last example didn't include the $$anonymous$$akeToolbar function which must be present in some form to make the call.
Your answer
Follow this Question
Related Questions
3D GUI + uniTUIO 0 Answers
Add a button to toolbar/selection grid? 1 Answer
Get variable from another object 1 Answer
disable code when level is rendered 1 Answer