miepzerino
2023-12-16 d95020b910bc69ab733f87d5d0238fe2d1164590
Overhauled audio management

added enums to sound names for no string duplications
8 files modified
2 files added
116 ■■■■■ changed files
Assets/Characters/Player/AC_Player.controller 2 ●●● patch | view | raw | blame | history
Assets/Characters/Player/Player.prefab 6 ●●●● patch | view | raw | blame | history
Assets/Objects/HealthPickup.prefab 1 ●●●● patch | view | raw | blame | history
Assets/Scenes/GameplayScene.unity 12 ●●●●● patch | view | raw | blame | history
Assets/Scripts/Damageable.cs 26 ●●●●● patch | view | raw | blame | history
Assets/Scripts/Enums/SoundNames.cs 30 ●●●●● patch | view | raw | blame | history
Assets/Scripts/Enums/SoundNames.cs.meta 11 ●●●●● patch | view | raw | blame | history
Assets/Scripts/Pickup.cs 3 ●●●●● patch | view | raw | blame | history
Assets/Scripts/SoundManager.cs 22 ●●●●● patch | view | raw | blame | history
Assets/Scripts/StateMachine/PlayOneShotBehaviour.cs 3 ●●●● patch | view | raw | blame | history
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
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
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
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
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;
Assets/Scripts/Enums/SoundNames.cs
New file
@@ -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
    }
}
Assets/Scripts/Enums/SoundNames.cs.meta
New file
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1faed8898e02be74ba94f65d08c77492
MonoImporter:
  externalObjects: {}
  serializedVersion: 2
  defaultReferences: []
  executionOrder: 0
  icon: {instanceID: 0}
  userData:
  assetBundleName:
  assetBundleVariant:
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);
            }
        }
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++)
        {
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