/
dedikins
/
Clicker
Обзор
Документация
Войти
/
dedikins
/
Clicker
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Assets/Scripts/AudioManager.cs
300 строк
6 KB
dedikins
Добавление музыки
02 июн 2026, 23:07
02 июн 2026, 23:07
7b9c663
Код
Авторство
О чём код?
using System.Collections; using UnityEngine; public class AudioManager : MonoBehaviour { public static AudioManager Instance; [Header("Sounds")] public AudioClip clickSound; public AudioClip upgradeSound; public AudioClip achievementSound; public AudioClip skinSound; [Header("Music")] public AudioClip menuMusic; public AudioClip gameMusic; private AudioSource soundSource; private AudioSource musicSource; private bool soundEnabled; private bool musicEnabled; private bool menuMusicEnabled; private bool gameMusicEnabled; private float soundVolume; private float musicVolume; private Coroutine fadeRoutine; private void Awake() { if (Instance == null) { Instance = this; } else { Destroy(gameObject); return; } AudioSource[] sources = GetComponents<AudioSource>(); soundSource = sources[0]; musicSource = sources[1]; soundEnabled = PlayerPrefs.GetInt( "SoundEnabled", 1) == 1; musicEnabled = PlayerPrefs.GetInt( "MusicEnabled", 1) == 1; menuMusicEnabled = PlayerPrefs.GetInt( "MenuMusicEnabled", 1) == 1; gameMusicEnabled = PlayerPrefs.GetInt( "GameMusicEnabled", 1) == 1; soundVolume = PlayerPrefs.GetFloat( "SoundVolume", 1f); musicVolume = PlayerPrefs.GetFloat( "MusicVolume", 0.3f); soundSource.volume = soundVolume; musicSource.volume = musicVolume; musicSource.loop = true; } private void Play(AudioClip clip) { if (!soundEnabled) return; if (clip == null) return; soundSource.PlayOneShot( clip, soundVolume); } public void PlayClick() { Play(clickSound); } public void PlayUpgrade() { Play(upgradeSound); } public void PlayAchievement() { Play(achievementSound); } public void PlaySkin() { Play(skinSound); } public void PlayMenuMusic() { if (!musicEnabled) return; if (!menuMusicEnabled) return; StartFadeMusic(menuMusic); } public void PlayGameMusic() { if (!musicEnabled) return; if (!gameMusicEnabled) return; StartFadeMusic(gameMusic); } private void StartFadeMusic(AudioClip clip) { if (musicSource.clip == clip) return; if (fadeRoutine != null) StopCoroutine(fadeRoutine); fadeRoutine = StartCoroutine( FadeMusic(clip)); } private IEnumerator FadeMusic(AudioClip newClip) { while (musicSource.volume > 0) { musicSource.volume -= Time.deltaTime; yield return null; } musicSource.Stop(); musicSource.clip = newClip; musicSource.Play(); while (musicSource.volume < musicVolume) { musicSource.volume += Time.deltaTime; yield return null; } musicSource.volume = musicVolume; } public void SetMusicVolume(float value) { musicVolume = value; musicSource.volume = value; PlayerPrefs.SetFloat( "MusicVolume", value); } public void SetSoundVolume(float value) { soundVolume = value; soundSource.volume = value; PlayerPrefs.SetFloat( "SoundVolume", value); } public float GetMusicVolume() { return musicVolume; } public float GetSoundVolume() { return soundVolume; } public void ToggleMenuMusic() { menuMusicEnabled = !menuMusicEnabled; PlayerPrefs.SetInt( "MenuMusicEnabled", menuMusicEnabled ? 1 : 0); if (!menuMusicEnabled) musicSource.Stop(); } public void ToggleGameMusic() { gameMusicEnabled = !gameMusicEnabled; PlayerPrefs.SetInt( "GameMusicEnabled", gameMusicEnabled ? 1 : 0); if (!gameMusicEnabled) musicSource.Stop(); } public bool IsMenuMusicEnabled() { return menuMusicEnabled; } public bool IsGameMusicEnabled() { return gameMusicEnabled; } public void ToggleSound() { soundEnabled = !soundEnabled; PlayerPrefs.SetInt( "SoundEnabled", soundEnabled ? 1 : 0); } public bool IsSoundEnabled() { return soundEnabled; } public void ToggleMusic() { musicEnabled = !musicEnabled; PlayerPrefs.SetInt( "MusicEnabled", musicEnabled ? 1 : 0); if (!musicEnabled) { musicSource.Stop(); } else { if (UIController.Instance.IsInMenu()) { musicSource.clip = menuMusic; } else { musicSource.clip = gameMusic; } musicSource.volume = musicVolume; musicSource.Play(); } } public bool IsMusicEnabled() { return musicEnabled; } }