Author Topic: Dreaded question (regarding event bubbling)  (Read 4970 times)

finky45

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 7
    • View Profile
Dreaded question (regarding event bubbling)
« on: June 17, 2014, 02:52:41 PM »
From what I read looking at posts it's not possible to do event bubbling with NGUI. Is it?

Imagine an RTS. You need to drag the view around with OnDrag events, but at the same time allow OnClick events on your scene game objects. The problem is you have colliders stacked on top of each other and only the top one gets the event.

I put an invisible widget in the GUI to catch OnDrag events, and OnClick events on my scene game objects. None of the events get passed the invisible widget's box collider which takes up the whole screen. Other than putting OnDrag functions on all my game objects and doing away with the invisible widget, is there a more elegant solution to this? I can't afford waiting for the new uGUI to come out (which supposedly has bubbling)

I was thinking of artificially creating an OnClick event and passing it futher down to the 3D scene but I don't know how to do it. This must be a common issue, how do people get past it?

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Dreaded question (regarding event bubbling)
« Reply #1 on: June 17, 2014, 05:42:34 PM »
The OnClick, OnPress etc is not really meant for in game things. You can use it like that, assuming your UICamera has that layer as an event mask, but it's not really directly meant for that. I tend to go with the invisible widget with a collider as the lowest UI element and when that catches a click, it allows the game to use the touch (via Input as normal).

finky45

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: Dreaded question (regarding event bubbling)
« Reply #2 on: June 18, 2014, 12:28:06 PM »
"(via Input as normal)"

Can you elaborate on this? What would code look like in a OnClick() function in the invisible widget to pass it further down?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Dreaded question (regarding event bubbling)
« Reply #3 on: June 18, 2014, 04:49:14 PM »
You can do this quite easily, finky45 -- just set UICamera's genericEventHandler to some object. This object will be receiving a copy of all events, whether they were handled by something else or not. Likewise if you want to handle only events that were not handled by something already, use UICamera.fallThrough.

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Dreaded question (regarding event bubbling)
« Reply #4 on: June 18, 2014, 04:56:04 PM »
With Input I meant http://docs.unity3d.com/ScriptReference/Input.html

Then you would set up a custom execution order, so your UI is handled first and your own custom game handler is handled afterwards, such that the invisible widget sets a globally accessible variable that allows input handling; something like this

  1. //invisible widget
  2. public class TouchAllower{
  3. public static bool touchAllowed = false;
  4. void LateUpdate(){
  5. touchAllowed = false; //clears the setting for this frame
  6. }
  7.  
  8. void OnClick() {touchAllowed = true; }
  9. }
  10.  
  11.  
  12. //game code
  13. void Update()
  14. {
  15. if (TouchAllower.touchAllowed)
  16. {
  17.  //do your ingame stuff with the input class.
  18. }
  19. }
  20.  
  21. }
  22.  

Granted it can get somewhat messy, but it can work. Doing it directly with UICamera might be nicer.