68 lines
2.5 KiB
C#
68 lines
2.5 KiB
C#
#nullable enable
|
|
|
|
using UnityEngine;
|
|
|
|
namespace ReplacedPerson.Runtime
|
|
{
|
|
[DisallowMultipleComponent]
|
|
public sealed class ReplacedPersonAudio : MonoBehaviour
|
|
{
|
|
private AudioSource? _uiSource;
|
|
private AudioSource? _fxSource;
|
|
private AudioClip? _uiClick;
|
|
private AudioClip? _cardDraw;
|
|
private AudioClip? _cardSelect;
|
|
private AudioClip? _lock;
|
|
private AudioClip? _diceRoll;
|
|
private AudioClip? _hit;
|
|
private AudioClip? _victory;
|
|
private AudioClip? _defeat;
|
|
private AudioClip? _error;
|
|
|
|
private void Awake()
|
|
{
|
|
_uiSource = CreateSource("UI Audio");
|
|
_fxSource = CreateSource("Battle Audio");
|
|
_uiClick = Load("ui_click");
|
|
_cardDraw = Load("card_draw");
|
|
_cardSelect = Load("card_select");
|
|
_lock = Load("lock");
|
|
_diceRoll = Load("dice_roll");
|
|
_hit = Load("hit");
|
|
_victory = Load("victory");
|
|
_defeat = Load("defeat");
|
|
_error = Load("error");
|
|
}
|
|
|
|
public void PlayUiClick() => Play(_uiSource, _uiClick, .55f);
|
|
public void PlayCardDraw() => Play(_fxSource, _cardDraw, .72f);
|
|
public void PlayCardSelect() => Play(_uiSource, _cardSelect, .65f);
|
|
public void PlayLock() => Play(_fxSource, _lock, .74f);
|
|
public void PlayDice() => Play(_fxSource, _diceRoll, .78f);
|
|
public void PlayHit() => Play(_fxSource, _hit, .70f);
|
|
public void PlayVictory() => Play(_fxSource, _victory, .72f);
|
|
public void PlayDefeat() => Play(_fxSource, _defeat, .62f);
|
|
public void PlayError() => Play(_uiSource, _error, .56f);
|
|
|
|
private AudioSource CreateSource(string sourceName)
|
|
{
|
|
var sourceObject = new GameObject(sourceName, typeof(AudioSource));
|
|
sourceObject.transform.SetParent(transform, false);
|
|
var source = sourceObject.GetComponent<AudioSource>();
|
|
source.playOnAwake = false;
|
|
source.loop = false;
|
|
source.spatialBlend = 0f;
|
|
source.ignoreListenerPause = true;
|
|
return source;
|
|
}
|
|
|
|
private static AudioClip? Load(string name) => Resources.Load<AudioClip>("ReplacedPersonAudio/" + name);
|
|
|
|
private static void Play(AudioSource? source, AudioClip? clip, float volume)
|
|
{
|
|
if (source == null || clip == null) return;
|
|
source.PlayOneShot(clip, volume);
|
|
}
|
|
}
|
|
}
|