Search Unity

Unity3d 4.6 UI - Toggle Sound

Discussion in 'Scripting' started by Aprial, Dec 15, 2014.

  1. Aprial

    Aprial

    Joined:
    Dec 22, 2013
    Posts:
    28
    Hello,
    I am using Unity3d 4.6 UI which include the GUI for toggle the Sound. How could I turn off/on the sound using C#.

    I appreciate your help,
    Aprial
     
  2. TSnake

    TSnake

    Joined:
    Mar 12, 2014
    Posts:
    13
    You can disable with "barbarian" code:

    Code (CSharp):
    1.     int Count;
    2.     int Leght;
    3.  
    4.     void Start()
    5.     {
    6.         AudioSettings.GetDSPBufferSize(out Leght,out Count);
    7.     }
    8.     public void ChangeSound(bool Boolean)
    9.     {
    10.         if (Boolean)
    11.         {
    12.             AudioSettings.SetDSPBufferSize(0, 0);
    13.         }
    14.         else
    15.         {
    16.             AudioSettings.SetDSPBufferSize(Leght, Count);
    17.         }
    18.     }
    19.  
    20.  
    Add ChangeSound() on OnValueChanged(Boolean) on your toggle.

    Note: That cause small lag and stop the music, when you re-on.
     
  3. Aprial

    Aprial

    Joined:
    Dec 22, 2013
    Posts:
    28
    Thanks TSnake.! Do you know how to contrtrol the volume? Thank you for your help, Aprial.