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
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.

avatar image vexe AndyMartin458 · Sep 09, 2013 at 05:28 PM 0
Share

Thanks, that's helpful. NGUI always surprised me with its goodies :) - I will wait a bit to see if there will be other good answers about different ideas before I mark your answer as correct.

avatar image vexe AndyMartin458 · Sep 09, 2013 at 05:49 PM 0
Share

I've read that Send$$anonymous$$essage operates a bit slow and it's better off using GetComponent. But since I'm not gonna be sending messages a lot, I think it's fine. I just don't like to create a whole script for each button, just so that I tell them what they should call or communicate with.

avatar image AndyMartin458 AndyMartin458 · Sep 09, 2013 at 05:54 PM 0
Share

It's true that it is probably slower, and that might not be the way that I would have programmed it. I can't think of a better way though if you just want to use NGUI out of the box. That's the way I've been using it, and I don't notice it being slow.

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.

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 brows local folders and get images from it and display them in a unity application? 1 Answer

Initialising List array for use in a custom Editor 1 Answer

Call C# function from within as3? 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