Author Topic: NGUI sounds not playing (AudioSource)  (Read 5551 times)

Cabal

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 16
    • View Profile
NGUI sounds not playing (AudioSource)
« on: January 16, 2014, 02:47:43 PM »
Hi all,

We ran into an issue with the way NGUI is selecting its AudioSource to play sounds (in NGUITools) This issue may be related to a couple other AudioSource threads on these forums that did not have an answer/solution yet. Hopefully this helps others with similar problems.

We have one AudioSource and AudioListener on our main app object (initializing game singletons and our main controllers and classes)
On this main object we also have our own SoundManager class which uses the AudioSource on the object to play the background music.

We had issues when we would start a sound with NGUI (NGUITools.PlaySound()) immediately followed by the SoundManager trying to start a new track.
The root of the issue was the fact that NGUI would use the same AudioSource we were using for the music (shown as MusicSource in the code below), and running the following lines on the audio source would stop the NGUI sound effect from playing (specifically, replacing the clip).

  1. AudioClip clip = (AudioClip)Resources.Load(clipName, typeof(AudioClip));
  2. MusicSource.clip = CurrentClip;
  3. MusicSource.Play();
  4.  

NGUITools is retrieving the AudioSource at runtime if it doesn't have one, which is great is you want to get some sounds playing without much setup. In our case though, we needed more control over what AudioSource to use. In addition, because this code was happening in PlaySound and because NGUI retrieves the AudioSource from the AudioListener, there was no easy way to tell NGUI which AudioSource to use.

In the end, we added a new static member to NGUITools to specify the AudioSource to use and we changed the code in PlaySound to use this AudioSource if it was set already, and keep the original behavior otherwise. This allows us to point NGUI at its own AudioSource and use a different one for music.

Something like that:
  1. static public class NGUITools
  2. {
  3.     ...
  4.     static public AudioSource SoundSource;
  5.     ....
  6.     static public AudioSource PlaySound (AudioClip clip, float volume, float pitch)
  7.    {
  8.         ....
  9.         // use the static member if it's set, otherwise use the original behavior to find/create one
  10.         if (SoundSource == null)
  11.         {
  12.             SoundSource = mListener.audio;
  13.             if (SoundSource == null) SoundSource = mListener.gameObject.AddComponent<AudioSource>();
  14.         }
  15.         SoundSource.pitch = pitch;
  16.         etc. ...
  17. }
  18.  

Hope this helps.

jarenas

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 2
    • View Profile
Re: NGUI sounds not playing (AudioSource)
« Reply #1 on: November 03, 2014, 05:45:10 PM »
Hi,

I have seen the code. But I see the default examples doesn't work the sound in editor mode. When I create the version I can listen any sound and music. I have a camera. The camera of the example. I assign it like main camera. But, when I play the example no sounds are listenning. I have change something. My main gameobject has a listenner and a Audiosource assigned. But nothing improve.

Regads,
Jordi

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: NGUI sounds not playing (AudioSource)
« Reply #2 on: November 04, 2014, 10:04:18 AM »
Why use the same audio source? Create a different one. In Windward I have a separate AudioSource for music. Not only is it separate, it's also detached from the camera altogether and set to DontDestroyOnLoad so that it seamlessly travels from scene to scene. This way the music doesn't get interrupted while the scene is loading.