- Home /
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:
(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.
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.
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.
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.
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.
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
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613113846im_/https://answers.unity.com/themes/thub/images/avi.jpg)