Add sfx login

This commit is contained in:
HungDK
2025-10-29 17:02:43 +07:00
parent 0fb8b0d406
commit cbfceab8ac
3 changed files with 68 additions and 0 deletions
+8
View File
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: eca6878d79ebfcf419afac70ed533edd
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,58 @@
using System.Collections;
using UnityEngine;
public class AudioManager : MonoBehaviour
{
public static AudioManager Instance;
public AudioSource bgmSource;
void Awake()
{
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
}
else Destroy(gameObject);
}
public void PlayBGM(AudioClip clip, float fadeTime = 1f)
{
StartCoroutine(FadeInBGM(clip, fadeTime));
}
public void StopBGM(float fadeTime = 1f)
{
StartCoroutine(FadeOut(fadeTime));
}
IEnumerator FadeInBGM(AudioClip clip, float fadeTime)
{
if (bgmSource.isPlaying)
yield return StartCoroutine(FadeOut(fadeTime));
bgmSource.clip = clip;
bgmSource.Play();
float t = 0f;
while (t < fadeTime)
{
t += Time.deltaTime;
bgmSource.volume = Mathf.Lerp(0, 1, t / fadeTime);
yield return null;
}
}
IEnumerator FadeOut(float fadeTime)
{
float startVol = bgmSource.volume;
float t = 0f;
while (t < fadeTime)
{
t += Time.deltaTime;
bgmSource.volume = Mathf.Lerp(startVol, 0, t / fadeTime);
yield return null;
}
bgmSource.Stop();
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: ab7ebb437fbd3fb41a7300a381fcab00