Overhauled audio management
added enums to sound names for no string duplications
8 files modified
2 files added
| | |
| | | m_Script: {fileID: 11500000, guid: b945ea9644e2087478dac705e081e0d6, type: 3} |
| | | m_Name: |
| | | m_EditorClassIdentifier: |
| | | audioClipName: PlayerDeath |
| | | audioClipName: 2 |
| | | playOnEnter: 1 |
| | | playOnExit: 0 |
| | | playAfterDelay: 0 |
| | |
| | | _isAlive: 1 |
| | | isInvincible: 0 |
| | | invincibilityTime: 0.25 |
| | | hitSound: {fileID: 8300000, guid: a56db7bda08a296458a6841eff8a58c5, type: 3} |
| | | healSound: {fileID: 0} |
| | | deathSound: {fileID: 8300000, guid: 2c515a2b0290e94478bb9d181223a774, type: 3} |
| | | hitSound: 3 |
| | | healSound: 0 |
| | | deathSound: 0 |
| | |
| | | m_EditorClassIdentifier: |
| | | healthRestore: 20 |
| | | spinRotatationSpeed: {x: 0, y: 180, z: 0} |
| | | soundName: 4 |
| | | --- !u!58 &7542207600701139278 |
| | | CircleCollider2D: |
| | | m_ObjectHideFlags: 0 |
| | |
| | | volMusic: 0.5 |
| | | volSFX: 0.5 |
| | | sounds: |
| | | - name: MusicHappy |
| | | - name: 1 |
| | | clip: {fileID: 8300000, guid: 7003df9618c25a2469a053e1b6887b7e, type: 3} |
| | | mixerGroup: 1 |
| | | volume: 0.016 |
| | |
| | | randomVolume: 0 |
| | | randomPitch: 0 |
| | | loop: 1 |
| | | - name: PlayerDeathSound |
| | | - name: 2 |
| | | clip: {fileID: 8300000, guid: 2c515a2b0290e94478bb9d181223a774, type: 3} |
| | | mixerGroup: 2 |
| | | volume: 0.699 |
| | |
| | | randomVolume: 0 |
| | | randomPitch: 0 |
| | | loop: 0 |
| | | - name: PlayerHit |
| | | - name: 3 |
| | | clip: {fileID: 8300000, guid: a56db7bda08a296458a6841eff8a58c5, type: 3} |
| | | mixerGroup: 2 |
| | | volume: 0.699 |
| | |
| | | randomVolume: 0.5 |
| | | randomPitch: 0.5 |
| | | loop: 0 |
| | | - name: Money |
| | | - name: 4 |
| | | clip: {fileID: 8300000, guid: 3a1b4e297698d49418effa2c18e862b7, type: 3} |
| | | mixerGroup: 3 |
| | | volume: 0.699 |
| | |
| | | serializedVersion: 3 |
| | | m_TransformParent: {fileID: 0} |
| | | m_Modifications: |
| | | - target: {fileID: 2350719025294399868, guid: c220ec455fce341408d66d880b464cad, type: 3} |
| | | propertyPath: healSound |
| | | value: |
| | | objectReference: {fileID: 8300000, guid: 3a1b4e297698d49418effa2c18e862b7, type: 3} |
| | | - target: {fileID: 2368348636056148999, guid: c220ec455fce341408d66d880b464cad, type: 3} |
| | | propertyPath: m_LocalPosition.x |
| | | value: 0 |
| | |
| | | using Assets.Scripts.Enums; |
| | | using System; |
| | | using System.Collections; |
| | | using System.Collections.Generic; |
| | |
| | | private float timeSinceHit = 0f; |
| | | public float invincibilityTime = 0.25f; |
| | | |
| | | public AudioClip hitSound; |
| | | public AudioClip healSound; |
| | | public AudioClip deathSound; |
| | | public SoundName hitSound; |
| | | public SoundName healSound; |
| | | public SoundName deathSound; |
| | | |
| | | public int MaxHealth |
| | | { |
| | |
| | | if (value <= 0) |
| | | { |
| | | IsAlive = false; |
| | | if (deathSound != null) |
| | | { |
| | | SoundManager.instance.PlaySoundAtPoint(gameObject, deathSound.name); |
| | | } |
| | | // Not needed here as it's played from the Animator |
| | | //if (deathSound != null) |
| | | //{ |
| | | SoundManager.instance.PlaySoundAtPoint(gameObject, deathSound); |
| | | //} |
| | | } |
| | | } |
| | | } |
| | |
| | | isInvincible = true; |
| | | |
| | | CharacterEvents.characterDamaged.Invoke(gameObject, actualDamageAmount); |
| | | if (hitSound != null) |
| | | { |
| | | SoundManager.instance.PlaySoundAtPoint(gameObject, hitSound.name); |
| | | } |
| | | SoundManager.instance.PlaySoundAtPoint(gameObject, hitSound); |
| | | } |
| | | } |
| | | |
| | |
| | | CharacterEvents.characterHealed.Invoke(gameObject, actualHealAmount); |
| | | |
| | | result = true; |
| | | if (healSound != null) |
| | | { |
| | | SoundManager.instance.PlaySoundAtPoint(gameObject, healSound.name); |
| | | } |
| | | SoundManager.instance.PlaySoundAtPoint(gameObject, healSound); |
| | | |
| | | } |
| | | return result; |
New file |
| | |
| | | using System.Reflection; |
| | | using System; |
| | | using UnityEditor; |
| | | using UnityEngine; |
| | | |
| | | namespace Assets.Scripts.Enums |
| | | { |
| | | public enum SoundName |
| | | { |
| | | None = 0, |
| | | #region music |
| | | |
| | | MusicHappy, |
| | | |
| | | #endregion |
| | | |
| | | #region player sounds |
| | | |
| | | PlayerDeathSound, |
| | | PlayerHit, |
| | | |
| | | #endregion |
| | | |
| | | #region SFX |
| | | |
| | | Money, |
| | | |
| | | #endregion |
| | | } |
| | | } |
New file |
| | |
| | | fileFormatVersion: 2 |
| | | guid: 1faed8898e02be74ba94f65d08c77492 |
| | | MonoImporter: |
| | | externalObjects: {} |
| | | serializedVersion: 2 |
| | | defaultReferences: [] |
| | | executionOrder: 0 |
| | | icon: {instanceID: 0} |
| | | userData: |
| | | assetBundleName: |
| | | assetBundleVariant: |
| | |
| | | using Assets.Scripts.Enums; |
| | | using System.Collections; |
| | | using System.Collections.Generic; |
| | | using UnityEngine; |
| | |
| | | |
| | | public Vector3 spinRotatationSpeed = new Vector3(0, 180, 0); |
| | | |
| | | public SoundName soundName; |
| | | |
| | | |
| | | private void OnTriggerEnter2D(Collider2D collision) |
| | |
| | | // Pickup health if not at max HP |
| | | if (damageable.Heal(healthRestore)) |
| | | { |
| | | SoundManager.instance.PlaySoundAtPoint(gameObject, soundName); |
| | | Destroy(gameObject); |
| | | } |
| | | } |
| | |
| | | [System.Serializable] |
| | | public class Sound |
| | | { |
| | | public string name; |
| | | public SoundName name; |
| | | public AudioClip clip; |
| | | public AudioMixerGroupEnum mixerGroup; |
| | | |
| | |
| | | sounds[i].SetSource(_go.AddComponent<AudioSource>(), audioMixer); |
| | | } |
| | | DontDestroyOnLoad(this); |
| | | PlaySound("MusicHappy"); |
| | | PlaySound(SoundName.MusicHappy); |
| | | } |
| | | |
| | | public void PlaySound(string _name) |
| | | public void PlaySound(SoundName _name) |
| | | { |
| | | if (_name == SoundName.None) |
| | | return; |
| | | |
| | | for (int i = 0; i < sounds.Length; i++) |
| | | { |
| | | if (sounds[i].name == _name) |
| | |
| | | Debug.LogWarning("SoundManager: Sounds not found in list: " + _name); |
| | | } |
| | | |
| | | public void PlaySoundAtPoint(GameObject gObject, string _name) |
| | | public void PlaySoundAtPoint(GameObject gObject, SoundName _name) |
| | | { |
| | | if (_name == SoundName.None) |
| | | return; |
| | | |
| | | gObject.AddComponent<AudioSource>(); |
| | | for (int i = 0; i < sounds.Length; i++) |
| | | { |
| | |
| | | |
| | | private static AudioSource PlayClipAtPointCustom(AudioSource audioSource, Vector3 pos, Sound sound) |
| | | { |
| | | audioSource.volume = sound.volume * (1 + Random.Range(-sound.randomVolume / 2f, sound.randomVolume / 2f)); |
| | | audioSource.pitch = sound.pitch * (1 + Random.Range(-sound.randomPitch / 2f, sound.randomPitch / 2f)); |
| | | GameObject tempGO = new GameObject("TempAudio"); // create the temp object |
| | | tempGO.transform.position = pos; // set its position |
| | | AudioSource tempASource = tempGO.AddComponent<AudioSource>(); // add an audio source |
| | |
| | | tempASource.playOnAwake = audioSource.playOnAwake; |
| | | tempASource.loop = audioSource.loop; |
| | | tempASource.priority = audioSource.priority; |
| | | tempASource.volume = audioSource.volume; |
| | | tempASource.pitch = audioSource.pitch; |
| | | tempASource.volume = audioSource.volume * (1 + Random.Range(-sound.randomVolume / 2f, sound.randomVolume / 2f)); |
| | | tempASource.pitch = audioSource.pitch * (1 + Random.Range(-sound.randomPitch / 2f, sound.randomPitch / 2f)); |
| | | tempASource.panStereo = audioSource.panStereo; |
| | | tempASource.spatialBlend = audioSource.spatialBlend; |
| | | tempASource.reverbZoneMix = audioSource.reverbZoneMix; |
| | |
| | | audioMixer.SetFloat("sfxVol", 20f * Mathf.Log10(_volume)); |
| | | } |
| | | |
| | | public void ChangeMusic(string _name) |
| | | public void ChangeMusic(SoundName _name) |
| | | { |
| | | for (int i = 0; i < sounds.Length; i++) |
| | | { |
| | |
| | | using Assets.Scripts.Enums; |
| | | using System.Collections; |
| | | using System.Collections.Generic; |
| | | using UnityEngine; |
| | | |
| | | public class PlayOneShotBehaviour : StateMachineBehaviour |
| | | { |
| | | public string audioClipName; |
| | | public SoundName audioClipName; |
| | | public bool playOnEnter = true, playOnExit = false, playAfterDelay = false; |
| | | |
| | | // Delayed sound timer |