///////////////////////////////////////////////////////////////////////////// /// Function: SetVolume ///////////////////////////////////////////////////////////////////////////// public static void SetVolume(int iAudioID, float fVol) { // The below GameObject will contain the Audio Object. GameObject goAudioObject = null; // Loop through all active audio objects and retrieve the object that // matches the provided id. foreach (GameObject goAudio in m_liActiveAudioObjects) { if (goAudio == null) { m_liActiveAudioObjects.Remove(goAudio); return; } // Attempt to retrieve the audio clip object. CAudioClip aClip = goAudio.GetComponent <CAudioClip>(); if (aClip == null) { return; } if (aClip.ClipId == iAudioID) { // We found a match. goAudioObject = goAudio; } } // Check if we managed to find the gameobject. if (goAudioObject == null) { return; } // Retrieve the audio source component. AudioSource asAudio = goAudioObject.GetComponent <AudioSource>(); if (null == asAudio) { return; } // Set the new volume asAudio.volume = fVol; }
///////////////////////////////////////////////////////////////////////////// /// Function: ToggleMainMenuMode ///////////////////////////////////////////////////////////////////////////// public static void ToggleMainMenuMode() { // Toggle the main menu mode flag. m_bMainMenuMode = !m_bMainMenuMode; // Application expects us to be in main menu mode, we need to lower the volume. // and store the initial volume in a dictionary. if (true == m_bMainMenuMode) { foreach (GameObject goAudio in m_liActiveAudioObjects) { if (goAudio == null) { m_liActiveAudioObjects.Remove(goAudio); return; } // Get handle on audio clip component. CAudioClip aClip = goAudio.GetComponent <CAudioClip>(); if (null == aClip) { return; } // Get the clip ID; int iAudioID = aClip.ClipId; // Get handle on audio source. AudioSource asAudio = goAudio.GetComponent <AudioSource>(); if (null == asAudio) { return; } // Keep track of the initial volume. m_dVolumeContainer.Add(iAudioID, asAudio.volume); // Lower volume. asAudio.volume = 0.01f; } } else { // Return normal volume to all sounds. foreach (GameObject goAudio in m_liActiveAudioObjects) { if (goAudio == null) { m_liActiveAudioObjects.Remove(goAudio); return; } // Get handle on audio clip component. CAudioClip aClip = goAudio.GetComponent <CAudioClip>(); if (null == aClip) { return; } float fVolume = 0; // Check if the volume container contains the clip key. if (null != aClip && true == m_dVolumeContainer.ContainsKey(aClip.ClipId)) { // Get the initial volume for this audio object. fVolume = m_dVolumeContainer[aClip.ClipId]; } // Get handle on audio source. AudioSource asAudio = goAudio.GetComponent <AudioSource>(); if (null == asAudio) { return; } // Turn volume back up. asAudio.volume = fVolume; } // Clear out the volume container m_dVolumeContainer.Clear(); } }
///////////////////////////////////////////////////////////////////////////// /// Function: StopSound ///////////////////////////////////////////////////////////////////////////// public static void StopSound(int iSourceID, bool bFadeOut = true) { // The below GameObject will contain the Audio Object. GameObject goAudioObject = null; // Loop through all active audio objects and retrieve the object that // matches the provided id. foreach (GameObject goAudio in m_liActiveAudioObjects) { if (goAudio == null) { m_liActiveAudioObjects.Remove(goAudio); return; } // Attempt to retrieve the audio clip object. CAudioClip aClip = goAudio.GetComponent <CAudioClip>(); if (aClip == null) { return; } if (aClip.ClipId == iSourceID) { // We found a match. goAudioObject = goAudio; } } // Check if we managed to find the gameobject. if (goAudioObject == null) { return; } // Retrieve the clip information object. CAudioClip cClipInfo = goAudioObject.GetComponent <CAudioClip>(); // Check if it's marked for deletion. if (true == cClipInfo.MarkedForDestruction) { // Take out the object and try stopping the sound again. m_liActiveAudioObjects.Remove(goAudioObject); StopSound(iSourceID, bFadeOut); // No point in going forward, we can exit the function. return; } else { // Take out the object and try stopping the sound again. m_liActiveAudioObjects.Remove(goAudioObject); // Ensure we don't reprocess this clip. cClipInfo.MarkedForDestruction = true; } // Retrieve the audio source for the fadeout functionality. AudioSource asSource = goAudioObject.GetComponent <AudioSource>(); if (true == bFadeOut) { CStaticCoroutine.DoCoroutine(FadeOut(asSource)); } else { asSource.volume = 0; } }
///////////////////////////////////////////////////////////////////////////// /// Function: CreateAndPlayAudio ///////////////////////////////////////////////////////////////////////////// public static int CreateAndPlayAudio(Vector3 v3Position, string strAudioName, bool bLoop, bool bPlayOnAwake, bool bFadeIn, float fVolume) { // This function will create a GameObject using the provided parameters and will add an // AudioSource component to it which we will configure to suit our needs. The GameObject // will be destroyed once we're done with it. string strFunctionName = "CAudioControl::CreateAndPlayAudio()"; // Check if we have a collection available for the provided audio name. if (false == m_dAudioClipContainer.ContainsKey(strAudioName)) { Debug.LogError(string.Format("{0} {1} " + ErrorStrings.ERROR_UNRECOGNIZED_NAME, strFunctionName, strAudioName)); } // Get a list of audio clips available to us. List <AudioClip> liAudioClips = m_dAudioClipContainer[strAudioName]; // Attempt to select a random clip from the list. AudioClip aClip = liAudioClips.OrderBy(x => Guid.NewGuid()).FirstOrDefault(); GameObject goAudioClipObject = new GameObject(strAudioName); goAudioClipObject.tag = Tags.TAG_AUDIO; // Set the position of the object. goAudioClipObject.transform.position = v3Position; // Add the Audio Source component goAudioClipObject.AddComponent <AudioSource>(); // Add the Clip Info object. goAudioClipObject.AddComponent <CAudioClip>(); // Retrieve the Audio source component. We will use this reference to set up values, etc. AudioSource asSource = goAudioClipObject.GetComponent <AudioSource>(); // Retrieve the Clip information object. CAudioClip cClipInfo = goAudioClipObject.GetComponent <CAudioClip>(); // Set up the audio source. asSource.playOnAwake = bPlayOnAwake; asSource.loop = bLoop; asSource.clip = aClip; asSource.volume = 0f; // Check if we want to fade in the sound. if (true == bFadeIn) { CStaticCoroutine.DoCoroutine(FadeIn(asSource, fVolume)); } else { asSource.volume = fVolume; } // Play the clip and destroy the game object once we're done with it. asSource.Play(); if (false == bLoop) { Destroy(goAudioClipObject, aClip.length); } else { m_liActiveAudioObjects.Add(goAudioClipObject); return(cClipInfo.ClipId); } // Return an invalid id. return(-1); }