타이틀 타이밍
This commit is contained in:
133
Assets/02_Scripts/Intro/IntroBGMController.cs
Normal file
133
Assets/02_Scripts/Intro/IntroBGMController.cs
Normal file
@@ -0,0 +1,133 @@
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
|
||||
[RequireComponent(typeof(AudioSource))]
|
||||
public class IntroBGMController : MonoBehaviour
|
||||
{
|
||||
[Header("Audio Source")]
|
||||
[SerializeField] private AudioSource audioSource;
|
||||
|
||||
[Header("Start Timing")]
|
||||
[SerializeField] private bool playOnStart = true;
|
||||
[SerializeField] private float startDelay = 0.5f;
|
||||
|
||||
[Header("Fade In")]
|
||||
[SerializeField] private bool useFadeIn = true;
|
||||
[SerializeField] private float fadeInDuration = 2.0f;
|
||||
[Range(0f, 1f)]
|
||||
[SerializeField] private float targetVolume = 0.55f;
|
||||
|
||||
[Header("Loop")]
|
||||
[SerializeField] private bool loop = true;
|
||||
|
||||
private Coroutine bgmRoutine;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (audioSource == null)
|
||||
{
|
||||
audioSource = GetComponent<AudioSource>();
|
||||
}
|
||||
|
||||
audioSource.playOnAwake = false;
|
||||
audioSource.loop = loop;
|
||||
audioSource.volume = 0f;
|
||||
audioSource.spatialBlend = 0f;
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
if (playOnStart)
|
||||
{
|
||||
PlayBGM();
|
||||
}
|
||||
}
|
||||
|
||||
public void PlayBGM()
|
||||
{
|
||||
if (audioSource == null || audioSource.clip == null)
|
||||
{
|
||||
Debug.LogWarning("[IntroBGMController] AudioSource 또는 AudioClip이 없습니다.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (bgmRoutine != null)
|
||||
{
|
||||
StopCoroutine(bgmRoutine);
|
||||
}
|
||||
|
||||
bgmRoutine = StartCoroutine(PlayBGMRoutine());
|
||||
}
|
||||
|
||||
private IEnumerator PlayBGMRoutine()
|
||||
{
|
||||
if (startDelay > 0f)
|
||||
{
|
||||
yield return new WaitForSeconds(startDelay);
|
||||
}
|
||||
|
||||
audioSource.volume = useFadeIn ? 0f : targetVolume;
|
||||
audioSource.loop = loop;
|
||||
audioSource.Play();
|
||||
|
||||
if (useFadeIn)
|
||||
{
|
||||
float elapsed = 0f;
|
||||
|
||||
while (elapsed < fadeInDuration)
|
||||
{
|
||||
elapsed += Time.deltaTime;
|
||||
float t = Mathf.Clamp01(elapsed / fadeInDuration);
|
||||
float easedT = EaseOutCubic(t);
|
||||
|
||||
audioSource.volume = Mathf.Lerp(0f, targetVolume, easedT);
|
||||
|
||||
yield return null;
|
||||
}
|
||||
|
||||
audioSource.volume = targetVolume;
|
||||
}
|
||||
|
||||
bgmRoutine = null;
|
||||
}
|
||||
|
||||
public void StopBGM(float fadeOutDuration = 1.0f)
|
||||
{
|
||||
if (audioSource == null)
|
||||
return;
|
||||
|
||||
if (bgmRoutine != null)
|
||||
{
|
||||
StopCoroutine(bgmRoutine);
|
||||
}
|
||||
|
||||
bgmRoutine = StartCoroutine(StopBGMRoutine(fadeOutDuration));
|
||||
}
|
||||
|
||||
private IEnumerator StopBGMRoutine(float fadeOutDuration)
|
||||
{
|
||||
float startVolume = audioSource.volume;
|
||||
float elapsed = 0f;
|
||||
|
||||
while (elapsed < fadeOutDuration)
|
||||
{
|
||||
elapsed += Time.deltaTime;
|
||||
float t = Mathf.Clamp01(elapsed / fadeOutDuration);
|
||||
float easedT = EaseOutCubic(t);
|
||||
|
||||
audioSource.volume = Mathf.Lerp(startVolume, 0f, easedT);
|
||||
|
||||
yield return null;
|
||||
}
|
||||
|
||||
audioSource.volume = 0f;
|
||||
audioSource.Stop();
|
||||
|
||||
bgmRoutine = null;
|
||||
}
|
||||
|
||||
private float EaseOutCubic(float t)
|
||||
{
|
||||
return 1f - Mathf.Pow(1f - t, 3f);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user