using UnityEngine; using UnityEngine.Audio; namespace Unity.FPS.Game { public class AudioUtility { static AudioManager s_AudioManager; public enum AudioGroups { DamageTick, Impact, EnemyDetection, Pickup, WeaponShoot, WeaponOverheat, WeaponChargeBuildup, WeaponChargeLoop, HUDVictory, HUDObjective, EnemyAttack } public static void CreateSFX(AudioClip clip, Vector3 position, AudioGroups audioGroup, float spatialBlend, float rolloffDistanceMin = 1f) { GameObject impactSfxInstance = new GameObject(); impactSfxInstance.transform.position = position; AudioSource source = impactSfxInstance.AddComponent(); source.clip = clip; source.spatialBlend = spatialBlend; source.minDistance = rolloffDistanceMin; source.Play(); source.outputAudioMixerGroup = GetAudioGroup(audioGroup); TimedSelfDestruct timedSelfDestruct = impactSfxInstance.AddComponent(); timedSelfDestruct.LifeTime = clip.length; } public static AudioMixerGroup GetAudioGroup(AudioGroups group) { if (s_AudioManager == null) s_AudioManager = GameObject.FindObjectOfType(); var groups = s_AudioManager.FindMatchingGroups(group.ToString()); if (groups.Length > 0) return groups[0]; Debug.LogWarning("Didn't find audio group for " + group.ToString()); return null; } public static void SetMasterVolume(float value) { if (s_AudioManager == null) s_AudioManager = GameObject.FindObjectOfType(); if (value <= 0) value = 0.001f; float valueInDb = Mathf.Log10(value) * 20; s_AudioManager.SetFloat("MasterVolume", valueInDb); } public static float GetMasterVolume() { if (s_AudioManager == null) s_AudioManager = GameObject.FindObjectOfType(); s_AudioManager.GetFloat("MasterVolume", out var valueInDb); return Mathf.Pow(10f, valueInDb / 20.0f); } } }