using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; using System.Collections; public class AudioManager : MonoBehaviour { public static AudioManager instance; [Header("Audio Source")] public AudioSource bgmSource; public AudioSource sfxSource; [Header("Audio Clip")] public AudioClip bgmClip; public AudioClip buttonClickSFX; [Header("UI Icons")] public Sprite soundOnIcon; public Sprite soundOffIcon; private bool isMuted = false; private void Awake() { if (instance == null) { instance = this; DontDestroyOnLoad(gameObject); isMuted = PlayerPrefs.GetInt("SoundMuted", 0) == 1; SiapkanAudioSource(); PutarBGM(); SceneManager.sceneLoaded += SaatSceneBaruDibuka; } else { Destroy(gameObject); } } private void OnDestroy() { if (instance == this) { SceneManager.sceneLoaded -= SaatSceneBaruDibuka; } } private void SiapkanAudioSource() { if (bgmSource != null) { bgmSource.mute = isMuted; bgmSource.loop = true; bgmSource.spatialBlend = 0f; if (bgmClip != null) bgmSource.clip = bgmClip; } if (sfxSource != null) { sfxSource.mute = isMuted; sfxSource.loop = false; sfxSource.playOnAwake = false; sfxSource.spatialBlend = 0f; sfxSource.volume = 1f; } } private void PutarBGM() { if (bgmSource == null) return; if (bgmSource.clip == null) return; if (!bgmSource.isPlaying) bgmSource.Play(); } private void SaatSceneBaruDibuka(Scene scene, LoadSceneMode mode) { StartCoroutine(PasangSoundButtonSetelahSceneLoad()); } private IEnumerator PasangSoundButtonSetelahSceneLoad() { yield return null; PasangSoundKeSemuaButton(); } public void PasangSoundKeSemuaButton() { Button[] semuaButton = FindObjectsOfType