Author Topic: How to listen click event which doesn't belong to current button  (Read 11090 times)

4everlove

  • Guest
Hi,

I've had the following need:

I have a button which will trigger a customized popup menu. While that menu expanded, I'd like to have it collapse whenever I click elsewhere, for example, click another button, or click the game area where there is no GUI element.

Here come's the question, how could I listen to such kind of onClickLeave event uniformly

Thanks!
« Last Edit: October 15, 2012, 03:23:41 AM by 4everlove »

joreldraw

  • Guest
Re: How to listen click event which doesn't belong to current button
« Reply #1 on: June 13, 2012, 03:05:20 AM »
Use UIForwardEvents

4everlove

  • Guest
Re: How to listen click event which doesn't belong to current button
« Reply #2 on: June 13, 2012, 03:39:44 AM »
Use UIForwardEvents

Could you give me small examples? And how to detect click on area which doesn't contain UI elements?

joreldraw

  • Guest
Re: How to listen click event which doesn't belong to current button
« Reply #3 on: June 13, 2012, 04:08:57 AM »
Is easy to use.
Attach UIForwardEvent to the button you like to send her event to another element.
Check on Inspector what Event like to Forward and set in Target the element you like to listen the event.

About area without UI Element, you need this attached to a NGUI Camera, and with a collider, and can take this events too.
« Last Edit: June 13, 2012, 04:10:28 AM by joreldraw »

Rafe

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 78
    • View Profile
Re: How to listen click event which doesn't belong to current button
« Reply #4 on: July 21, 2012, 01:29:11 AM »
I'm looking for this as well. UIForwardEvents doesn't make sense when you want to listen to events that could hit many other objects. I have a feeling it will involve the UICamera static eventHandler but I'm still searching for any examples or discussion.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to listen click event which doesn't belong to current button
« Reply #5 on: July 21, 2012, 01:46:18 AM »
Check UICamera.genericEventHandler. If you set it to something, this object will receive a copy of all the events, regardless of where they go to.

Rafe

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 78
    • View Profile
Re: How to listen click event which doesn't belong to current button
« Reply #6 on: July 21, 2012, 02:08:58 AM »
Thanks for the reply. I'm beginning to see how I can adapt what I have to nGUI. This other thread helped and this discussion continues there:
http://www.tasharen.com/forum/index.php?topic=138.0


Rafe

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 78
    • View Profile
Re: How to listen click event which doesn't belong to current button
« Reply #7 on: July 24, 2012, 02:35:42 AM »
Hi,

I had an AhHa moment and I wanted to share this pattern. I think it could be formalized further as well, but it is working.

In one script's Awake() (any script really, it doesn't matter which object is set as the Generic Event Listener)...
  1. UICamera.genericEventHandler = this.gameObject

Now any script can subscribe to an event using UIEventListener with UICamera's static access to genericEventListener...
  1. UIEventListener.Get(UICamera.genericEventHandler).onClick += this.OnClick;

In one script I also do a GameObject comparison with thefall-through gameObject to determine if I am getting a UI event or a fall-through event (because the genericEventHandler gets all events, including the fall-through).

If this is a valid workflow, I would highly recommend rolling this in to the core of nGUI. If necessary, nGUI could create a hidden GameObject to send Messages through and provide a reference for users (in addition to the genericEventHandler). It would only take a few minutes to implement this way. Perhaps it could be another method in the UIEventListener, or even an overload of Get(), when there is no argument.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to listen click event which doesn't belong to current button
« Reply #8 on: July 24, 2012, 03:03:49 AM »
That's a perfectly valid approach, and is actually pretty good idea.

Rafe

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 78
    • View Profile
Re: How to listen click event which doesn't belong to current button
« Reply #9 on: July 24, 2012, 03:36:18 AM »
I made a mistake. The game object passed to the delegate is always the gameObject passed to Get(). For example ...
  1.         UIEventListener.Get(UICamera.genericEventHandler).onClick += this.OnClickDelegate;
  2.  
  1. private void OnClickDelegate(GameObject obj)
  2. {
  3.     Debug.Log(obj);   // obj is always UICamera.genericEventHandler
  4. }

I had hoped OnClick always provides the collider that was clicked.

There really is no easy way to tell when the GUI is hit, since the generic and fall-through both get an OnClick event. I would have to use two objects to catch each then compare somehow. I'll drop in a workaround for my workaround, using a bool or something  ;).

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to listen click event which doesn't belong to current button
« Reply #10 on: July 24, 2012, 03:40:12 AM »
UICamera.lastHit.collider gives you the collider.

Rafe

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 78
    • View Profile
Re: How to listen click event which doesn't belong to current button
« Reply #11 on: July 24, 2012, 03:53:27 AM »
(I really shouldn't be posting so late at night!)

Great!

It looks like the hit collider is null when the GUI isn't hit. Is this reliable?...because it would be the key for me to detect GUI hits vs non-GUI hits. I tested with this:

  1.         if (UICamera.lastHit.collider == null)
  2.             Debug.Log("Click: ?");
  3.         else
  4.             Debug.Log("Click: " + UICamera.lastHit.collider.name);

It seems to be working... (famous late-night words)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to listen click event which doesn't belong to current button
« Reply #12 on: July 24, 2012, 03:55:07 AM »
It may be null if you'll get the event in UICamera.fallThrough or UICamera.genericEventHandler. Otherwise it's not null.

rob

  • Guest
Re: How to listen click event which doesn't belong to current button
« Reply #13 on: June 05, 2013, 12:55:22 AM »
Just resurrecting an old thread - I'm looking for the same functionality, where I have a custom class that handles "OnPress" and through the above event, an "OnPressOutside" method.

For the "OnPressOutside" method, I was hoping that I could test if the current gameobject's collider was equal to UICamera.lastHit, and ignore it if it was.

The problem is that UICamera.lastHit caches the last hit collider until the next collider is hit. Makes perfect sense, but in this case I would require it to return null when the empty space around the colliders is clicked - instead I get the last collider, which - if it was my own - would be ignored as per the previous logic.

Is there an alternative I'm missing?

Thanks in advance.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to listen click event which doesn't belong to current button
« Reply #14 on: June 06, 2013, 08:47:43 AM »
You can always clear it using UICamera.lastHit = new RaycastHit().