143 lines
3.3 KiB
C#
143 lines
3.3 KiB
C#
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<Button>(true);
|
|
|
|
foreach (Button btn in semuaButton)
|
|
{
|
|
btn.onClick.RemoveListener(PlayButtonClick);
|
|
btn.onClick.AddListener(PlayButtonClick);
|
|
}
|
|
}
|
|
|
|
public void PlayButtonClick()
|
|
{
|
|
if (isMuted) return;
|
|
if (sfxSource == null) return;
|
|
if (buttonClickSFX == null) return;
|
|
|
|
sfxSource.PlayOneShot(buttonClickSFX);
|
|
}
|
|
|
|
public void ToggleSound(Image buttonImage)
|
|
{
|
|
isMuted = !isMuted;
|
|
|
|
PlayerPrefs.SetInt("SoundMuted", isMuted ? 1 : 0);
|
|
PlayerPrefs.Save();
|
|
|
|
if (bgmSource != null)
|
|
bgmSource.mute = isMuted;
|
|
|
|
if (sfxSource != null)
|
|
sfxSource.mute = isMuted;
|
|
|
|
if (buttonImage != null)
|
|
buttonImage.sprite = isMuted ? soundOffIcon : soundOnIcon;
|
|
}
|
|
|
|
public void ResetMute()
|
|
{
|
|
isMuted = false;
|
|
PlayerPrefs.SetInt("SoundMuted", 0);
|
|
PlayerPrefs.Save();
|
|
|
|
if (bgmSource != null)
|
|
bgmSource.mute = false;
|
|
|
|
if (sfxSource != null)
|
|
sfxSource.mute = false;
|
|
}
|
|
} |