using DG.Tweening; using System.Collections; using System.Collections.Generic; using UnityEngine; [RequireComponent(typeof(AudioSource))] public class SoundPlayer : MonoBehaviour { public AudioSource audioSource; float volume; private void Awake() { if (audioSource == null) audioSource = GetComponent(); volume = audioSource.volume; } public void Play(float pitch = 1, bool PLAY = true) { if(audioSource == null) audioSource = GetComponent(); audioSource.pitch = pitch; audioSource.volume = volume; if (PLAY) { audioSource.Play(); Play_Sync(); } else audioSource.Stop(); } public float durationFadeOut = 0.5f; public void Play_Fade(bool PLAY = true) { if (audioSource == null) audioSource = GetComponent(); if (PLAY) { audioSource.Play(); Play_Sync(); } else { float vol2 = audioSource.volume; audioSource.DOFade(0, durationFadeOut).OnComplete(() => { audioSource.Stop(); audioSource.volume = vol2; }); } } public List syncs; public void Play_Sync() { for (int i = 0; i < syncs.Count; i++) SoundManager.Instance.EffectPlay(syncs[i].gameObject); } public void Volume(float rate, System.Action OnEnd) { audioSource.DOFade(volume * rate, durationFadeOut).OnComplete(() => { audioSource.volume = volume * rate; if (OnEnd != null) OnEnd(); }); } public void Volume(float rate) { audioSource.volume = volume * rate; } }