Author Topic: Detect click outside current UI component  (Read 6897 times)

Shifty Geezer

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 9
  • Posts: 226
    • View Profile
Detect click outside current UI component
« on: January 19, 2014, 04:20:16 PM »
I have a selector (scrollview list of buttons) that appears when I long-press a button. I want any press outside this widget to cause it to close, whether that's on the game background or another NGUI widget.

vallcrist

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 49
    • View Profile
Re: Detect click outside current UI component
« Reply #1 on: January 19, 2014, 04:22:45 PM »
Hey there.

I have this in my game, i made it with a big ( giant ) widget behind the main one, with a big collider and an UIButton script that sends an event to the widget that needs to be closed.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Detect click outside current UI component
« Reply #2 on: January 20, 2014, 12:26:38 AM »
NGUI sents OnSelect(false) when another object gets pressed.

Shifty Geezer

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 9
  • Posts: 226
    • View Profile
Re: Detect click outside current UI component
« Reply #3 on: January 20, 2014, 05:12:56 AM »
I'm clicking buttons within the scrollview. The scrollview's collider itself never registers these presses, so I can't use 'OnSelect(false)' with the scrollview to register a press outside the scrollview. I can't use the button's OnSelect to notify the scrollview to close either in case the next button pressed is within the scrollview.



Ugur

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 60
    • View Profile
Re: Detect click outside current UI component
« Reply #4 on: January 20, 2014, 06:51:14 AM »
yeah, a panel/scrollview etc should best trigger a different event like OnPressOutside which is triggered when clicking outside the boundaries (but is not triggered when clicking something like a different button inside the panel)

Shifty Geezer

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 9
  • Posts: 226
    • View Profile
Re: Detect click outside current UI component
« Reply #5 on: January 23, 2014, 04:47:03 AM »
In case someone else has this issue, here's how I've solved it.

On my pop-up panel, I have an empty background widget with collider that fills the screen and contains the selection widget. The selection widget works as normal, and if I click outside of it I hit the collider of the background widget. I have to deactivate all other components when it pops up though. I have a little SetEnabled() script that deactivates boxcolliders that I apply on the root containers for my various panels.

I think it'd be nice and sensible to have a 'pop-up' widget type that automates this as it's a common feature. It would be easy to create a prefab that has the background widget and actual component but the deactivation of everything else might be tricky. If there's a way to 'z-sort' controls, this deactivating step wouldn't be needed making the whole thing even easier. A 'pop-up' script would add a full-screen widget and a widget within set up to capture the outside box events.