gather-and-defend/Assets/Scripts/Audio/AudioPlayerComponent.cs
Craftelia 4711ae0230 design/afterGala3BalancePatch (#22)
- Massive rebalance of all units and levels (game is more focused on early base building and late powerful waves)
- Added all desert levels
- Added spearman, archer lvl3, gatherers lvl2 and lvl3, temp art skittering husk
- Added stone house (more pop)
- Tooltip visual rework
- Some bugs fixed
- Playing sounds when placing units

Reviewed-on: http://gitea.clubconjure.com/Conjure/gather-and-defend/pulls/22
Reviewed-by: Ader_Alisma <ader.alisma.1@ens.etsmtl.ca>
Co-authored-by: Craftelia <william-gin1@hotmail.com>
Co-committed-by: Craftelia <william-gin1@hotmail.com>
2026-01-24 15:50:46 +00:00

44 lines
1.4 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace GatherAndDefend
{
[RequireComponent(typeof(AudioSource))]
public class AudioPlayerComponent : MonoBehaviour
{
[SerializeField] private AudioTemplate[] playableSounds;
private Dictionary<string, AudioTemplate> _playableSounds;
private AudioSource _audioSource;
private void Awake()
{
_audioSource = GetComponent<AudioSource>();
_playableSounds = new Dictionary<string, AudioTemplate>();
if (playableSounds == null || playableSounds.Length == 0) return;
foreach (var sound in playableSounds)
{
_playableSounds.Add(sound.name, sound);
}
}
public void PlaySound(string soundName, float volumeMultiplier = 1f, float overrideVolume = -1f)
{
if (_playableSounds.TryGetValue(soundName, out AudioTemplate sound))
{
PlaySound(sound, volumeMultiplier, overrideVolume);
}
}
public void PlaySound(AudioTemplate sound, float volumeMultiplier = 1f, float overrideVolume = -1f)
{
_audioSource.clip = sound.GetRandomClip();
_audioSource.pitch = sound.GetRandomPitch();
_audioSource.volume = ((overrideVolume != -1f) ? overrideVolume : sound.GetVolumeToUse()) * volumeMultiplier;
_audioSource.Play();
}
}
}