Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Leviathan · Jan 10, 2011 at 09:56 PM · guimultitouchtoolbar

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.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jesse Anders · Jan 10, 2011 at 10:05 PM 0
Share

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?

avatar image Statement · Jan 10, 2011 at 10:09 PM 0
Share

The gui calls can be wrapped in classes or delegates.

avatar image Statement · Jan 10, 2011 at 10:11 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Leviathan · Jan 11, 2011 at 09:02 AM 0
Share

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.

avatar image Statement · Jan 11, 2011 at 09:46 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

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

What is frame, How OnGuI is called every frame? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges