From d95020b910bc69ab733f87d5d0238fe2d1164590 Mon Sep 17 00:00:00 2001 From: miepzerino <o.skotnik@gmail.com> Date: Sat, 16 Dec 2023 18:52:45 +0000 Subject: [PATCH] Overhauled audio management --- Assets/Scripts/Damageable.cs | 26 +++++------- Assets/Scripts/SoundManager.cs | 22 ++++++---- Assets/Characters/Player/AC_Player.controller | 2 Assets/Scripts/Enums/SoundNames.cs | 30 +++++++++++++++ Assets/Characters/Player/Player.prefab | 6 +- Assets/Scripts/StateMachine/PlayOneShotBehaviour.cs | 3 + Assets/Scenes/GameplayScene.unity | 12 ++---- Assets/Scripts/Pickup.cs | 3 + Assets/Objects/HealthPickup.prefab | 1 Assets/Scripts/Enums/SoundNames.cs.meta | 11 +++++ 10 files changed, 79 insertions(+), 37 deletions(-) diff --git a/Assets/Characters/Player/AC_Player.controller b/Assets/Characters/Player/AC_Player.controller index 032366e..5e6c5c9 100644 --- a/Assets/Characters/Player/AC_Player.controller +++ b/Assets/Characters/Player/AC_Player.controller @@ -176,7 +176,7 @@ m_Script: {fileID: 11500000, guid: b945ea9644e2087478dac705e081e0d6, type: 3} m_Name: m_EditorClassIdentifier: - audioClipName: PlayerDeath + audioClipName: 2 playOnEnter: 1 playOnExit: 0 playAfterDelay: 0 diff --git a/Assets/Characters/Player/Player.prefab b/Assets/Characters/Player/Player.prefab index a58fdfa..4b725e2 100644 --- a/Assets/Characters/Player/Player.prefab +++ b/Assets/Characters/Player/Player.prefab @@ -458,6 +458,6 @@ _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 diff --git a/Assets/Objects/HealthPickup.prefab b/Assets/Objects/HealthPickup.prefab index 0e4d033..09759fd 100644 --- a/Assets/Objects/HealthPickup.prefab +++ b/Assets/Objects/HealthPickup.prefab @@ -100,6 +100,7 @@ m_EditorClassIdentifier: healthRestore: 20 spinRotatationSpeed: {x: 0, y: 180, z: 0} + soundName: 4 --- !u!58 &7542207600701139278 CircleCollider2D: m_ObjectHideFlags: 0 diff --git a/Assets/Scenes/GameplayScene.unity b/Assets/Scenes/GameplayScene.unity index a44ff0c..cb4c36b 100644 --- a/Assets/Scenes/GameplayScene.unity +++ b/Assets/Scenes/GameplayScene.unity @@ -1055,7 +1055,7 @@ volMusic: 0.5 volSFX: 0.5 sounds: - - name: MusicHappy + - name: 1 clip: {fileID: 8300000, guid: 7003df9618c25a2469a053e1b6887b7e, type: 3} mixerGroup: 1 volume: 0.016 @@ -1063,7 +1063,7 @@ randomVolume: 0 randomPitch: 0 loop: 1 - - name: PlayerDeathSound + - name: 2 clip: {fileID: 8300000, guid: 2c515a2b0290e94478bb9d181223a774, type: 3} mixerGroup: 2 volume: 0.699 @@ -1071,7 +1071,7 @@ randomVolume: 0 randomPitch: 0 loop: 0 - - name: PlayerHit + - name: 3 clip: {fileID: 8300000, guid: a56db7bda08a296458a6841eff8a58c5, type: 3} mixerGroup: 2 volume: 0.699 @@ -1079,7 +1079,7 @@ randomVolume: 0.5 randomPitch: 0.5 loop: 0 - - name: Money + - name: 4 clip: {fileID: 8300000, guid: 3a1b4e297698d49418effa2c18e862b7, type: 3} mixerGroup: 3 volume: 0.699 @@ -3482,10 +3482,6 @@ 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 diff --git a/Assets/Scripts/Damageable.cs b/Assets/Scripts/Damageable.cs index 09fa72f..6fd7016 100644 --- a/Assets/Scripts/Damageable.cs +++ b/Assets/Scripts/Damageable.cs @@ -1,3 +1,4 @@ +using Assets.Scripts.Enums; using System; using System.Collections; using System.Collections.Generic; @@ -23,9 +24,9 @@ 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 { @@ -48,10 +49,11 @@ 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); + //} } } } @@ -101,10 +103,7 @@ isInvincible = true; CharacterEvents.characterDamaged.Invoke(gameObject, actualDamageAmount); - if (hitSound != null) - { - SoundManager.instance.PlaySoundAtPoint(gameObject, hitSound.name); - } + SoundManager.instance.PlaySoundAtPoint(gameObject, hitSound); } } @@ -119,10 +118,7 @@ CharacterEvents.characterHealed.Invoke(gameObject, actualHealAmount); result = true; - if (healSound != null) - { - SoundManager.instance.PlaySoundAtPoint(gameObject, healSound.name); - } + SoundManager.instance.PlaySoundAtPoint(gameObject, healSound); } return result; diff --git a/Assets/Scripts/Enums/SoundNames.cs b/Assets/Scripts/Enums/SoundNames.cs new file mode 100644 index 0000000..d8514c3 --- /dev/null +++ b/Assets/Scripts/Enums/SoundNames.cs @@ -0,0 +1,30 @@ +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 + } +} \ No newline at end of file diff --git a/Assets/Scripts/Enums/SoundNames.cs.meta b/Assets/Scripts/Enums/SoundNames.cs.meta new file mode 100644 index 0000000..312d540 --- /dev/null +++ b/Assets/Scripts/Enums/SoundNames.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 1faed8898e02be74ba94f65d08c77492 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Pickup.cs b/Assets/Scripts/Pickup.cs index 28f2069..c2ed000 100644 --- a/Assets/Scripts/Pickup.cs +++ b/Assets/Scripts/Pickup.cs @@ -1,3 +1,4 @@ +using Assets.Scripts.Enums; using System.Collections; using System.Collections.Generic; using UnityEngine; @@ -8,6 +9,7 @@ public Vector3 spinRotatationSpeed = new Vector3(0, 180, 0); + public SoundName soundName; private void OnTriggerEnter2D(Collider2D collision) @@ -21,6 +23,7 @@ // Pickup health if not at max HP if (damageable.Heal(healthRestore)) { + SoundManager.instance.PlaySoundAtPoint(gameObject, soundName); Destroy(gameObject); } } diff --git a/Assets/Scripts/SoundManager.cs b/Assets/Scripts/SoundManager.cs index 84a6a6a..0ecdc0d 100644 --- a/Assets/Scripts/SoundManager.cs +++ b/Assets/Scripts/SoundManager.cs @@ -10,7 +10,7 @@ [System.Serializable] public class Sound { - public string name; + public SoundName name; public AudioClip clip; public AudioMixerGroupEnum mixerGroup; @@ -88,11 +88,14 @@ 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) @@ -106,8 +109,11 @@ 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++) { @@ -128,8 +134,6 @@ 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 @@ -142,8 +146,8 @@ 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; @@ -175,7 +179,7 @@ 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++) { diff --git a/Assets/Scripts/StateMachine/PlayOneShotBehaviour.cs b/Assets/Scripts/StateMachine/PlayOneShotBehaviour.cs index b032804..9f35227 100644 --- a/Assets/Scripts/StateMachine/PlayOneShotBehaviour.cs +++ b/Assets/Scripts/StateMachine/PlayOneShotBehaviour.cs @@ -1,10 +1,11 @@ +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 -- Gitblit v1.9.3