Author Topic: UIButton Sound OnClick and OnMouseOver events  (Read 11617 times)

Doddler

  • Guest
UIButton Sound OnClick and OnMouseOver events
« on: May 08, 2013, 02:39:53 AM »
I'm sorry if this is explained elsewhere, what I'm doing is I have two UIButton Sound scripts on a single UIImage Button object, one for OnClick and one for OnMouseOver. The problem is that when you click, the OnMouseOver event is also triggered at the same time (even though the button never loses focus). The best I can tell, I couldn't find an easy way to have it only play a click sound when clicked. Is there anything I'm doing wrong here?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIButton Sound OnClick and OnMouseOver events
« Reply #1 on: May 08, 2013, 06:24:59 PM »
OnMouseOver is a Unity event. NGUI uses OnHover.

Doddler

  • Guest
Re: UIButton Sound OnClick and OnMouseOver events
« Reply #2 on: May 08, 2013, 10:03:20 PM »
OnMouseOver is a Unity event. NGUI uses OnHover.

Well, it says OnMouseOver on the dropdown, but I guess it does use OnHover on NGUI's UIButtonSound script. Here's how I have the button set up.



The Exit Button script at the moment does nothing, it's simply got an OnClick handler for the button that's not set up.

Looking at it the code, there's always an OnHover event being triggered with the OnClick event, even when the button isn't gaining or losing focus.

As a test, I created a script and attached it to a button (with no other scripts other than the UIImageButton script). The code is as follows:

  1.         void OnHover(bool isOver)
  2.         {
  3.                 Debug.Log("Button event OnHover triggered. isOver: " + isOver);
  4.         }
  5.  
  6.         void OnPress(bool isPressed)
  7.         {
  8.                 Debug.Log("Button event OnPress triggered. isPressed: " + isPressed);
  9.         }
  10.  
  11.         void OnClick()
  12.         {
  13.                 Debug.Log("Button event OnClick triggered.");
  14.         }

When run, the program generates the following output:



The first OnHover event is triggered when the mouse moves over the button. Then OnPress is triggered when the mousebutton is pressed down. When the mouse button is released, OnPress is triggered with isPressed now false, OnHover gets called a second time (I think this is the bug), followed by OnClick. Then lastly OnHover is false when the object loses focus.

Knowing how it works I guess it's pretty simple to make a work around, but I think it's still technically a bug?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIButton Sound OnClick and OnMouseOver events
« Reply #3 on: May 09, 2013, 07:19:05 AM »
The way it is right now is so that states are reverted correctly with minimal work on the user's part. Pressed state overwrites hover state, and when it's released, hover state returns.

Doddler

  • Guest
Re: UIButton Sound OnClick and OnMouseOver events
« Reply #4 on: May 09, 2013, 12:56:25 PM »
The way it is right now is so that states are reverted correctly with minimal work on the user's part. Pressed state overwrites hover state, and when it's released, hover state returns.

Alright, that makes sense, thanks for the help. In the mean-time, I've modified the OnHover method in NGUI's UIButtonSound class as follows to prevent the OnHover sound from playing when a click is performed.

  1.         private bool _currentOverState = false;
  2.  
  3.         void OnHover (bool isOver)
  4.         {
  5.                 if (_currentOverState == isOver)
  6.                         return;
  7.                 _currentOverState = isOver;
  8.                 if (enabled && ((isOver && trigger == Trigger.OnMouseOver) || (!isOver && trigger == Trigger.OnMouseOut)))
  9.                 {
  10.                         NGUITools.PlaySound(audioClip, volume, pitch);
  11.                 }
  12.         }