• 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
1
Question by vexe · Sep 09, 2013 at 05:13 PM · c#nguicommunicationorganizationpattern

Best way/pattern to exchange messages between GUI elements (buttons, windows, etc)

Hello, I'm using NGUI. In my UI, I have buttons, windows, title bars, etc. Clicking a button might do something to the window its in. For example a 'X' (close) button will close the window its in.

Here's what I have:

alt text

(The 2nd bar above the file manager's address bar shouldn't be there) As you can probably tell, there's a lot of things going on. For example:

  • If you click on ITEMS you will see the items view, click on FILES to see the file manager. Now, this involved hiding/showing the parent object holding the items/file manager.

  • Clicking on the close button, will deactivate the inventory's panel.

  • Clicking on the little tetris icon, will sort the items.

  • Clicking on the green down arrow, will discard all items.

In the last two examples, the buttons had to communicate with the items' bag, and call the necessary methods. For that, I create a script called button_Sort and button_Discard, have a public serialized Bag variable, and assign it in the inspector before I play.

If not in the inspector, assuming the button is a child somewhere under the bag, the bag accesses the button's button_Sort script and assign itself to the Bag variable inside there.

This sounds tedious, is there a better way than this?

And in general, what's the best way to handle this type of communication between such components? I've recently learned about the Mediator pattern, I'm thinking how I could use it here. How about SendMessage? - If you do suggest it, I'd appreciate a brief explanation about how it works, how it sends the message, etc because I haven't used it before.

Thanks for you ideas and suggestions in advance.

setup.png (124.5 kB)
Comment
Add comment · Show 12
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 AndyMartin458 · Sep 09, 2013 at 05:17 PM 1
Share

You add an event listener and the name of the function to the button (try using UIButton$$anonymous$$essage). I'm not sure if that's what you've already done, but then NGUI will use the Send$$anonymous$$essage function under the hood which will in turn call the function on the object specified.

Show more comments
avatar image Fattie · Sep 10, 2013 at 06:10 PM 1
Share

just purely FWIW for anyone reading. 2DToolkit now has a UI system. I find it unbelievably easy to use, and it's the best sprite handling by a lightyear. i sound like a broken record often mentioning T$$anonymous$$, but there's a reason almost everyone uses the damn thing. Rock on! regarding messages, see comment below on Send$$anonymous$$essage .. use it, of course

1 Reply

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

Answer by vexe · Sep 10, 2013 at 12:56 PM

I ended up writing my own event handlers, which is what I originally did to handle communication.

"In the last two examples, the buttons had to communicate with the items' bag, and call the necessary methods. For that, I create a script called button_Sort and button_Discard, have a public serialized Bag variable, and assign it in the inspector before I play."

ArenMook also recommended this approach if I wanted more control, here's what he said.

I could then centralize my communication using a mediator I give to each of my buttons for them to register themselves in, and once an OnClick even fires, the button accesses the mediator and tell it to execute or do something. More info on the mediator pattern.

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to pass context from unity to Kotlin library 0 Answers

Unhandled Exception caused by UnityEngine.UI 0 Answers

An OS design issue: File types associated with their appropriate programs 1 Answer


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