Author Topic: Disabling button's sound when button is disabled  (Read 1758 times)

wallabie

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 200
    • View Profile
Disabling button's sound when button is disabled
« on: April 21, 2014, 09:45:47 PM »
How to disable the sound on a button when button.enabled = false ?

ATM, the button is disabled but it still reacts to clicks.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Disabling button's sound when button is disabled
« Reply #1 on: April 22, 2014, 04:49:37 AM »
Disable the UIPlaySound script.

wallabie

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 200
    • View Profile
Re: Disabling button's sound when button is disabled
« Reply #2 on: April 22, 2014, 08:19:36 AM »
If I had 20 Go's that are listening to the onclick event from the button, is there a way to stop the button from firing a onClick event when the button is disabled? 


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Disabling button's sound when button is disabled
« Reply #3 on: April 23, 2014, 06:37:41 AM »
UIPlaySound script doesn't listen to the button's On Click. It listens to the generic OnClick event, which comes from the event system. It's a stand-alone component and is not related to the button itself. Here, I've modified UIPlaySound to check the button.
  1. //----------------------------------------------
  2. //            NGUI: Next-Gen UI kit
  3. // Copyright © 2011-2014 Tasharen Entertainment
  4. //----------------------------------------------
  5.  
  6. using UnityEngine;
  7.  
  8. /// <summary>
  9. /// Plays the specified sound.
  10. /// </summary>
  11.  
  12. [AddComponentMenu("NGUI/Interaction/Play Sound")]
  13. public class UIPlaySound : MonoBehaviour
  14. {
  15.         public enum Trigger
  16.         {
  17.                 OnClick,
  18.                 OnMouseOver,
  19.                 OnMouseOut,
  20.                 OnPress,
  21.                 OnRelease,
  22.                 Custom,
  23.         }
  24.  
  25.         public AudioClip audioClip;
  26.         public Trigger trigger = Trigger.OnClick;
  27.  
  28.         bool mIsOver = false;
  29.  
  30. #if UNITY_3_5
  31.         public float volume = 1f;
  32.         public float pitch = 1f;
  33. #else
  34.         [Range(0f, 1f)] public float volume = 1f;
  35.         [Range(0f, 2f)] public float pitch = 1f;
  36. #endif
  37.  
  38.         bool canPlay
  39.         {
  40.                 get
  41.                 {
  42.                         if (!enabled) return false;
  43.                         UIButton btn = GetComponent<UIButton>();
  44.                         return (btn == null || btn.isEnabled);
  45.                 }
  46.         }
  47.  
  48.         void OnHover (bool isOver)
  49.         {
  50.                 if (trigger == Trigger.OnMouseOver)
  51.                 {
  52.                         if (mIsOver == isOver) return;
  53.                         mIsOver = isOver;
  54.                 }
  55.  
  56.                 if (canPlay && ((isOver && trigger == Trigger.OnMouseOver) || (!isOver && trigger == Trigger.OnMouseOut)))
  57.                         NGUITools.PlaySound(audioClip, volume, pitch);
  58.         }
  59.  
  60.         void OnPress (bool isPressed)
  61.         {
  62.                 if (trigger == Trigger.OnPress)
  63.                 {
  64.                         if (mIsOver == isPressed) return;
  65.                         mIsOver = isPressed;
  66.                 }
  67.  
  68.                 if (canPlay && ((isPressed && trigger == Trigger.OnPress) || (!isPressed && trigger == Trigger.OnRelease)))
  69.                         NGUITools.PlaySound(audioClip, volume, pitch);
  70.         }
  71.  
  72.         void OnClick ()
  73.         {
  74.                 if (canPlay && trigger == Trigger.OnClick)
  75.                         NGUITools.PlaySound(audioClip, volume, pitch);
  76.         }
  77.  
  78.         void OnSelect (bool isSelected)
  79.         {
  80.                 if (canPlay && (!isSelected || UICamera.currentScheme == UICamera.ControlScheme.Controller))
  81.                         OnHover(isSelected);
  82.         }
  83.  
  84.         public void Play ()
  85.         {
  86.                 NGUITools.PlaySound(audioClip, volume, pitch);
  87.         }
  88. }

wallabie

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 200
    • View Profile
Re: Disabling button's sound when button is disabled
« Reply #4 on: April 23, 2014, 06:55:10 AM »
Thanks.