Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: stilghar on April 10, 2013, 03:56:52 PM

Title: [solved]Problem with UISoundVolume
Post by: stilghar on April 10, 2013, 03:56:52 PM
Hi!

I recently bought NGUI, having lots of fun with it! Great tool. Now the question of a newbie in trouble:

I have attached the following simple script to my main camera (not the UI camera):

http://hastebin.com/coyiloxugu.py (http://hastebin.com/coyiloxugu.py)

And then I put a UISoundVolume script on an NGUI slider. But the volume of the song played doesn't seem to be affected. What may be wrong? I tried adding a Debug.Log inside the OnSliderChange of the UISoundVolume to see if this was called at all and it seems to be the case. So, I don't understand then what could be the problem.

Any help?

Stilghar
Title: Re: Problem with UISoundVolume
Post by: ArenMook on April 10, 2013, 03:58:31 PM
Adjusting the slider will not affect the volume of the sounds that are currently playing. Only sounds that you will play after the adjustment.
Title: Re: Problem with UISoundVolume
Post by: stilghar on April 10, 2013, 04:01:59 PM
Ok, then it seems I can't use UISoundVolume for what I wanted.

I have created a menu where I have two sliders one for music and one for effects. I wanted these to affect all the music and sounds in my game. Any tip on how to achieve this? What is the point then of having NGUITools.soundVolume in the first place then? I don't get it...

Stilghar
Title: Re: Problem with UISoundVolume
Post by: ArenMook on April 10, 2013, 08:50:03 PM
  1. using UnityEngine;
  2.  
  3. [RequireComponent(typeof(UISlider))]
  4. public class UIVolumeSlider : MonoBehaviour
  5. {
  6.         public AudioSource audioSource;
  7.  
  8.         void Awake ()
  9.         {
  10.                 UISlider slider = GetComponent<UISlider>();
  11.                 slider.sliderValue = audioSource.volume;
  12.                 slider.eventReceiver = gameObject;
  13.         }
  14.  
  15.         void OnSliderChange (float val)
  16.         {
  17.                 audioSource.volume = val;
  18.         }
  19. }
Title: Re: Problem with UISoundVolume
Post by: stilghar on April 11, 2013, 02:27:50 PM
Thanks for the tip! This helps!

...still I don't understand the purpose of NGUITools.SoundVolume though... What is the use case for it then?
Title: Re: Problem with UISoundVolume
Post by: ArenMook on April 11, 2013, 08:43:54 PM
All NGUI sounds are played taking that volume value into consideration. UI sounds are generally short, so it doesn't matter if it alters the actively playing sounds or not.

In your case you are altering the volume of something that's actively playing -- music. You need to both play it and approach it differently, thus the script above.
Title: Re: Problem with UISoundVolume
Post by: stilghar on April 13, 2013, 03:16:55 PM
Many thanks Aren! It makes sense.