Author Topic: Newbie Learning NGUI event/delegate for Multiple camera setup  (Read 9931 times)

rocki

  • Guest
Newbie Learning NGUI event/delegate for Multiple camera setup
« on: October 02, 2012, 11:43:56 PM »
Hi all,

I've just purchased NGUI and learning how to use this amazing system.  I have the following situation and would like to get some expert opinions from those who are experienced.

Situation: 
I have a 3D root on a default layer, a 2D root (Scroll_noLabels) on a GUI layer and a Controller also on a default layer. 
All three of the above are on the same hierarchy level.
I have a button (theButton) as a child in the 2D root (Scroll_noLabels)
The Controller contain a (Main.cs) script that should act as a event manager.

What I would like to do:  If I click on theButton, I would like for for objects/functions in the 3D, 2D and controller to be affected.

It seems from my research on the NGUI forum regarding various events/delegates practices, there are many ways to tackle this problem. 
What would some examples of best practice protocols for this situation.

Please be descriptive in the answers and not just a one liner.

If someone has an example project, it would really be fantastic as I am still very new to NGUI.

Thanks in advance





ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Newbie Learning NGUI event/delegate for Multiple camera setup
« Reply #1 on: October 03, 2012, 01:01:49 AM »
Am I understanding you correctly here -- you want a single event to arrive in all 3 places? If so, you will want to use a UICamera.genericEventHandler. Set it to your controller game object, and it will be receiving a copy of all the events, regardless of who handled them.

That said, the first collider is the one that will intercept this event, so if your 2D UI has a button that you click on, your 3D UI will never get this event. Not sure why you'd want to.

rocki

  • Guest
Re: Newbie Learning NGUI event/delegate for Multiple camera setup
« Reply #2 on: October 03, 2012, 08:08:27 AM »
Thanks Aren for the quick reply.

Yes, you understand my question Right-on.  I am actually a seasoned Flash/Flex Developer migrating to Unity so I am trying to understand how the NGUI event system works.  If anyone else knows Flash/Flex please chime in and give us a Analogy of how NGUI works with a Flash/Flex mindset.

Regarding the first collider intercepting the event... How then can I have one event broadcasting and those who are subscribing all react. 

A use case is where a button fires that moves something in the 3D view while at the same time updating other 2D gui controls.  Flash has this sort of event system.  And Flex adds databinding which allows GUI elements to be automatically update based on a changes in data. 


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Newbie Learning NGUI event/delegate for Multiple camera setup
« Reply #3 on: October 03, 2012, 08:48:52 AM »
NGUI events only go to whatever is "on top". If you have 3 buttons on top of each other, only the top-most button will actually receive the event. After all, having two windows on top of each other and clicking through one window onto another would be just... weird.

You can still subscribe to all events by doing so on the game object set as UICamera.genericEventHandler, but I suggest avoiding doing that if you want. It's a lot cleaner to attach a specific script to each button that does what you need it to. For example if clicking one button should load a certain level, use LoadLevelOnClick script attached to that button (and only to that button).

rocki

  • Guest
Re: Newbie Learning NGUI event/delegate for Multiple camera setup
« Reply #4 on: October 03, 2012, 09:40:58 AM »
 "If you have 3 buttons on top of each other, only the top-most button will actually receive the event."
I am not clear about this statement, I am understanding this as only the top-most button can SEND an event, am I wrong?

"It's a lot cleaner to attach a specific script to each button that does what you need it to. "
I am still very new to Unity and NGUI and trying to get my head around the script attachment paradigm.  My only concern is that coming from Flash/Flex, we do not want to hard couple the presentation controls with the Code.  In Flash development, it is often the case that we have to change the Graphic/Presentation layer.  How would one achieve this sort of separation of concern in Unity and NGUI.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Newbie Learning NGUI event/delegate for Multiple camera setup
« Reply #5 on: October 03, 2012, 07:35:03 PM »
UICamera is the script that sends the event (press, click, etc). When an event occurs, a ray is cast into the screen. The first collider it hits is the collider that gets the event (and thus -- its game object). What you do with that event from that point onward is up to you.

Pip Robbins

  • Guest
Re: Newbie Learning NGUI event/delegate for Multiple camera setup
« Reply #6 on: October 04, 2012, 02:35:30 AM »
I think there might be some confusion in this thread - or I am confused :) from what I understand you would like an event to be broad cast to any listeners.

The benefit being that the broadcaster does not need to know who or how many listeners are listening, enabling decoupled code.

As an example:

You press the power up button, it sends the message and you might have multiple systems listening for the message. GUI manager to which provides a visual effect, audio manager which plays an audio effect, power up manager who puts the power up on cool-down, player controller which might start running double speed.

There is multiple ways to do this:

Broadcast
  1. NGUITools.Broadcast("OnPowerUp", doubleSpeedPowerUp);
Using broadcast is the easiest way which will send to to all scripts on all game objects which has a function signature that matches, but I assume this is slow.

Send Message
SendMessage or UIButtonMessage, only problem is that the broadcaster needs to know who it sends it too. Which is not really what your after.

  1. target.SendMessage("PowerUp", "Speed Boost");
  2.  

Event Manager
UICamera.genericEventHandler, which you can set up a event manager which gets all events in the game, you need then specific what it does depending on what object was hit.

  1.  
  2. OnClick(object) {
  3.  
  4.     if(object == "Power Up Button") PowerUpEvent();
  5.  
  6. }
  7.  

You could set up loads of specific events, have listeners subscribe to those events (using delegates and events) or set up one event and have it send through a packet with all the data and each listener determines what to do with it or ignore it.

Hardcode
Hard code it all with direct links to other objects, but not what you want, as decoupled is much nicer.
  1. PowerUpManager.UsePowerUp("Speed Boost")

Notification Center

NotificationCenter which you can subscribe broadcasters and listeners, this is likely the best and most performance efficient way, but at the same time would be a lot of work.

http://wiki.unity3d.com/index.php/Scripts/General

Look at the messaging system for lots of examples.



Now I hope I did understand the question, also I am not confident enough to say that what I have previously said is right or even a valid approach. As it is, I am still hitting my head trying to decide the nicest way of decoupling my code (along with making it reusable and dynamic). So I would love some more input from anyone who has a more experience on how best handle NGUI.
« Last Edit: October 04, 2012, 02:49:14 AM by Pip Robbins »

rocki

  • Guest
Re: Newbie Learning NGUI event/delegate for Multiple camera setup
« Reply #7 on: October 04, 2012, 12:16:01 PM »
Pip,

Thanks for the very detailed breakdown of the various options.  Looks like the NotificationCenterGenerics could be a nice solution.  I will start looking into this approach.

Thanks.


rocki

  • Guest
Re: Newbie Learning NGUI event/delegate for Multiple camera setup
« Reply #8 on: October 04, 2012, 12:37:53 PM »
Aren,

Thanks for explaining how the event system works in 3D.  I hadn't realized how translating from 3D to 2D could be so tricky and the solution with Raycasting is quite clever.

rocki

  • Guest
Re: Newbie Learning NGUI event/delegate for Multiple camera setup
« Reply #9 on: October 04, 2012, 12:42:16 PM »
Pip,

I had a bit of a closer look into NotificationCenterGenerics,  it seems to be using the Unity messaging system which according to this post http://technology.blurst.com/unityscript-messaging-system/ not as fast as functions or delegates.  Aren is using delegates in NGUI event system and perhaps an extension onto NGUI's delegate with a broadcast subscribe architecture might be a viable approach.   

Pip Robbins

  • Guest
Re: Newbie Learning NGUI event/delegate for Multiple camera setup
« Reply #10 on: October 04, 2012, 07:00:04 PM »
The article is great, but unfortunately its 4 years old and also before generics were supported on iOS etc.

The some of the messaging systems from that link have been updated since which makes good use of delegates, events, generics and avoids array lists etc which are a bit more costly.