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:

15271-setup.png

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

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.