private static PlaySoundResult PlaySoundIfAvailable(string soundGroupName, AudioInfo info, Vector3 sourcePosition, float volumePercentage, ref bool forgetSoundPlayed, float? pitch = null, AudioGroupInfo audioGroup = null, Transform sourceTrans = null, bool attachToSource = false, float delaySoundTime = 0f, bool useVector3 = false, bool makePlaySoundResult = false, bool isChaining = false, bool isSingleSubscribedPlay = false) { if (info.source == null) { // this avoids false errors when stopping the game (from became "invisible" event callers) return null; } MasterAudioGroup maGroup = audioGroup._group; if (maGroup.curVariationMode == MasterAudioGroup.VariationMode.Normal && info.source.audio.isPlaying) { var playedPercentage = AudioUtil.GetAudioPlayedPercentage(info.source); var retriggerPercent = maGroup.retriggerPercentage; if (playedPercentage < retriggerPercent) { return null; // wait for this to stop playing or play further. } } info.variation.Stop(); info.variation.ObjectToFollow = null; if (useVector3) { info.source.transform.position = sourcePosition; if (MasterAudio.Instance.prioritizeOnDistance) { AudioPrioritizer.Set3dPriority(info.source); } } else if (sourceTrans != null) { if (attachToSource) { info.variation.ObjectToFollow = sourceTrans; } else { info.source.transform.position = sourceTrans.position; info.variation.ObjectToTriggerFrom = sourceTrans; } if (MasterAudio.Instance.prioritizeOnDistance) { AudioPrioritizer.Set3dPriority(info.source); } } else { // "2d manner" - from Master Audio location if (MasterAudio.Instance.prioritizeOnDistance) { AudioPrioritizer.Set2dSoundPriority(info.source); } info.source.transform.localPosition = Vector3.zero; // put it back in MA prefab position after being detached. } var groupVolume = maGroup.groupMasterVolume; var busVolume = GetBusVolume(maGroup); float calcVolume = info.originalVolume * groupVolume * busVolume * MasterAudio.Instance.masterAudioVolume; // set volume to percentage. float volume = calcVolume * volumePercentage; var targetVolume = volume; var randomVol = 0f; if (info.variation.useRandomVolume) { // random volume randomVol = UnityEngine.Random.Range(info.variation.randomVolumeMin, info.variation.randomVolumeMax); switch (info.variation.randomVolumeMode) { case SoundGroupVariation.RandomVolumeMode.AddToClipVolume: targetVolume += randomVol; break; case SoundGroupVariation.RandomVolumeMode.IgnoreClipVolume: targetVolume = randomVol; break; } } info.source.audio.volume = targetVolume; // save these for on the fly adjustments afterward info.lastPercentageVolume = volumePercentage; info.lastRandomVolume = randomVol; bool isActive = false; #if UNITY_3_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5 isActive = info.source.audio.gameObject.active; #else isActive = info.source.audio.gameObject.activeInHierarchy; #endif if (!isActive) { return null; } PlaySoundResult result = null; if (makePlaySoundResult) { result = new PlaySoundResult(); result.ActingVariation = info.variation; if (delaySoundTime > 0f) { result.SoundScheduled = true; } else { result.SoundPlayed = true; } } else { forgetSoundPlayed = true; } var playSoundParams = new SoundGroupVariation.PlaySoundParams(maGroup.name, volumePercentage, targetVolume, pitch, sourceTrans, attachToSource, delaySoundTime, maGroup.curVariationMode == MasterAudioGroup.VariationMode.LoopedChain, isSingleSubscribedPlay); if (playSoundParams.isChainLoop && !isChaining) { maGroup.ChainLoopCount = 0; } if (playSoundParams.isChainLoop) { // make sure there isn't 2 going, ever MasterAudio.StopAllOfSound(playSoundParams.soundType); } info.variation.Play(pitch, targetVolume, playSoundParams); return result; }
private static PlaySoundResult PlaySoundIfAvailable(string soundGroupName, AudioInfo info, Vector3 sourcePosition, float volumePercentage, ref bool forgetSoundPlayed, float? pitch = null, AudioGroupInfo audioGroup = null, Transform sourceTrans = null, bool attachToSource = false, float delaySoundTime = 0f, bool useVector3 = false, bool makePlaySoundResult = false, bool isChaining = false) { if (info.source == null) { // this avoids false errors when stopping the game (from became invisible event callers) return null; } MasterAudioGroup maGroup = audioGroup._group; if (info.source.audio.isPlaying) { var playedPercentage = AudioUtil.GetAudioPlayedPercentage(info.source); var retriggerPercent = maGroup.retriggerPercentage; if (playedPercentage < retriggerPercent) { return null; // wait for this to stop playing or play further. } } info.variation.Stop(); info.variation.ObjectToFollow = null; if (useVector3) { info.source.transform.position = sourcePosition; if (MasterAudio.Instance.prioritizeOnDistance) { AudioPrioritizer.Set3dPriority(info.source); } } else if (sourceTrans != null) { if (attachToSource) { info.variation.ObjectToFollow = sourceTrans; } else { info.source.transform.position = sourceTrans.position; } if (MasterAudio.Instance.prioritizeOnDistance) { AudioPrioritizer.Set3dPriority(info.source); } } else { // "2d manner" if (MasterAudio.Instance.prioritizeOnDistance) { AudioPrioritizer.Set2dSoundPriority(info.source); } info.source.transform.localPosition = Vector3.zero; // put it back in MA prefab position after being detached. } var groupVolume = maGroup.groupMasterVolume; var busVolume = GetBusVolume(maGroup); float calcVolume = info.originalVolume * groupVolume * busVolume * MasterAudio.Instance.masterAudioVolume; // set volume to percentage. float volume = calcVolume * volumePercentage; // random volume var randomVol = 0f; if (info.variation.randomVolume != 0f) { randomVol = UnityEngine.Random.Range(-info.variation.randomVolume, info.variation.randomVolume); } var targetVolume = volume + randomVol; info.source.audio.volume = targetVolume; // save these for on the fly adjustments afterward info.lastPercentageVolume = volumePercentage; info.lastRandomVolume = randomVol; bool isActive = false; #if UNITY_3_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5 isActive = info.source.audio.gameObject.active; #else isActive = info.source.audio.gameObject.activeInHierarchy; #endif if (!isActive) { return null; } PlaySoundResult result = null; if (makePlaySoundResult) { result = new PlaySoundResult(); result.ActingVariation = info.variation; if (delaySoundTime > 0f) { result.SoundScheduled = true; } else { result.SoundPlayed = true; } } else { forgetSoundPlayed = true; } var playSoundParams = new SoundGroupVariation.PlaySoundParams(maGroup.name, volumePercentage, calcVolume, pitch, sourceTrans, attachToSource, delaySoundTime, maGroup.curVariationMode == MasterAudioGroup.VariationMode.LoopedChain); if (playSoundParams.isChainLoop && !isChaining) { maGroup.ChainLoopCount = 0; } if (playSoundParams.isChainLoop) { // make sure there isn't 2 going, ever MasterAudio.StopAllOfSound(playSoundParams.soundType); } info.variation.Play(pitch, targetVolume, playSoundParams); if (delaySoundTime == 0f) { // sound play worked! Duck music if a ducking sound. var ma = MasterAudio.Instance; var matchingDuck = ma.musicDuckingSounds.Find(delegate(DuckGroupInfo obj) { return obj.soundType == soundGroupName; }); if (ma.EnableMusicDucking && matchingDuck != null) { // duck music var duckLength = info.source.audio.clip.length; var duckPitch = info.source.pitch; var pcs = PlaylistController.Instances; for (var i = 0; i < pcs.Count; i++) { pcs[i].DuckMusicForTime(duckLength, duckPitch, matchingDuck.riseVolStart); } if (pcs.Count == 0) { Debug.LogWarning("Playlist Controller is not in the Scene. Cannot duck music."); } } } return result; }