176 lines
6.4 KiB
C#
176 lines
6.4 KiB
C#
using UnityEngine;
|
||
using UnityEngine.SceneManagement;
|
||
using UnityEngine.UI;
|
||
|
||
namespace _Project.Scripts.Utilities
|
||
{
|
||
public class MainMenuController : MonoBehaviour
|
||
{
|
||
[Header("Сцены")]
|
||
[Tooltip("Имя игровой сцены (или оставь пустым, чтобы грузить следующую по Build Index)")]
|
||
[SerializeField] private string gameSceneName = "";
|
||
|
||
[Header("Панели UI")]
|
||
[SerializeField] private GameObject mainPanel;
|
||
[SerializeField] private GameObject settingsPanel;
|
||
|
||
[Header("Элементы настроек (UI)")]
|
||
[SerializeField] private Slider volumeSlider;
|
||
[SerializeField] private Slider sensitivitySlider;
|
||
[SerializeField] private Toggle fullscreenToggle;
|
||
[SerializeField] private Toggle vsyncToggle;
|
||
[SerializeField] private Toggle headbobToggle;
|
||
|
||
[Header("Значения по умолчанию")]
|
||
[SerializeField] private float defaultVolume = 0.8f;
|
||
[SerializeField] private float defaultSensitivity = 0.1f;
|
||
|
||
// Ключи для PlayerPrefs (совпадают с PlayerCamera.cs)
|
||
public const string KeyVolume = "MasterVolume";
|
||
public const string KeySensitivity = "MouseSensitivity";
|
||
public const string KeyFullscreen = "Fullscreen";
|
||
public const string KeyVSync = "VSync";
|
||
public const string KeyHeadbob = "EnableHeadBob";
|
||
|
||
private void Awake()
|
||
{
|
||
// На случай возврата из игровой сцены: возвращаем нормальное время и видимый курсор
|
||
Time.timeScale = 1f;
|
||
Cursor.lockState = CursorLockMode.None;
|
||
Cursor.visible = true;
|
||
}
|
||
|
||
private void Start()
|
||
{
|
||
ShowMainPanel();
|
||
LoadAndApplySettings();
|
||
BindUIEvents();
|
||
}
|
||
|
||
// ---------- КНОПКИ ГЛАВНОГО МЕНЮ (для Button -> OnClick) ----------
|
||
|
||
public void OnPlayButtonClicked()
|
||
{
|
||
PlayerPrefs.Save();
|
||
|
||
if (!string.IsNullOrEmpty(gameSceneName))
|
||
{
|
||
SceneManager.LoadScene(gameSceneName);
|
||
}
|
||
else
|
||
{
|
||
// Загружает следующую сцену в Build Settings (например, индекс 1)
|
||
int nextSceneIndex = SceneManager.GetActiveScene().buildIndex + 1;
|
||
if (nextSceneIndex < SceneManager.sceneCountInBuildSettings)
|
||
SceneManager.LoadScene(nextSceneIndex);
|
||
else
|
||
Debug.LogError("MainMenuController: Игровая сцена не добавлена в Build Settings!");
|
||
}
|
||
}
|
||
|
||
public void OnSettingsButtonClicked()
|
||
{
|
||
if (mainPanel != null) mainPanel.SetActive(false);
|
||
if (settingsPanel != null) settingsPanel.SetActive(true);
|
||
}
|
||
|
||
public void OnCloseSettingsButtonClicked()
|
||
{
|
||
PlayerPrefs.Save();
|
||
ShowMainPanel();
|
||
}
|
||
|
||
public void OnQuitButtonClicked()
|
||
{
|
||
PlayerPrefs.Save();
|
||
Debug.Log("Выход из игры...");
|
||
|
||
#if UNITY_EDITOR
|
||
UnityEditor.EditorApplication.isPlaying = false;
|
||
#else
|
||
Application.Quit();
|
||
#endif
|
||
}
|
||
|
||
private void ShowMainPanel()
|
||
{
|
||
if (mainPanel != null) mainPanel.SetActive(true);
|
||
if (settingsPanel != null) settingsPanel.SetActive(false);
|
||
}
|
||
|
||
// ---------- ЗАГРУЗКА И ИЗМЕНЕНИЕ НАСТРОЕК ----------
|
||
|
||
private void LoadAndApplySettings()
|
||
{
|
||
float volume = PlayerPrefs.GetFloat(KeyVolume, defaultVolume);
|
||
float sens = PlayerPrefs.GetFloat(KeySensitivity, defaultSensitivity);
|
||
bool isFullscreen = PlayerPrefs.GetInt(KeyFullscreen, Screen.fullScreen ? 1 : 0) == 1;
|
||
bool isVSync = PlayerPrefs.GetInt(KeyVSync, QualitySettings.vSyncCount > 0 ? 1 : 0) == 1;
|
||
bool isHeadbob = PlayerPrefs.GetInt(KeyHeadbob, 1) == 1;
|
||
|
||
// Применяем к движку
|
||
AudioListener.volume = volume;
|
||
Screen.fullScreen = isFullscreen;
|
||
QualitySettings.vSyncCount = isVSync ? 1 : 0;
|
||
|
||
// Выставляем ползунки и галочки в UI
|
||
if (volumeSlider != null) volumeSlider.SetValueWithoutNotify(volume);
|
||
if (sensitivitySlider != null)
|
||
{
|
||
// Настраиваем разумные границы для сенсы новой Input System (0.01 .. 0.8)
|
||
sensitivitySlider.minValue = 0.01f;
|
||
sensitivitySlider.maxValue = 0.8f;
|
||
sensitivitySlider.SetValueWithoutNotify(sens);
|
||
}
|
||
if (fullscreenToggle != null) fullscreenToggle.SetIsOnWithoutNotify(isFullscreen);
|
||
if (vsyncToggle != null) vsyncToggle.SetIsOnWithoutNotify(isVSync);
|
||
if (headbobToggle != null) headbobToggle.SetIsOnWithoutNotify(isHeadbob);
|
||
}
|
||
|
||
private void BindUIEvents()
|
||
{
|
||
if (volumeSlider != null)
|
||
volumeSlider.onValueChanged.AddListener(SetVolume);
|
||
|
||
if (sensitivitySlider != null)
|
||
sensitivitySlider.onValueChanged.AddListener(SetSensitivity);
|
||
|
||
if (fullscreenToggle != null)
|
||
fullscreenToggle.onValueChanged.AddListener(SetFullscreen);
|
||
|
||
if (vsyncToggle != null)
|
||
vsyncToggle.onValueChanged.AddListener(SetVSync);
|
||
|
||
if (headbobToggle != null)
|
||
headbobToggle.onValueChanged.AddListener(SetHeadbob);
|
||
}
|
||
|
||
public void SetVolume(float value)
|
||
{
|
||
AudioListener.volume = value;
|
||
PlayerPrefs.SetFloat(KeyVolume, value);
|
||
}
|
||
|
||
public void SetSensitivity(float value)
|
||
{
|
||
PlayerPrefs.SetFloat(KeySensitivity, value);
|
||
}
|
||
|
||
public void SetFullscreen(bool isFullscreen)
|
||
{
|
||
Screen.fullScreen = isFullscreen;
|
||
PlayerPrefs.SetInt(KeyFullscreen, isFullscreen ? 1 : 0);
|
||
}
|
||
|
||
public void SetVSync(bool enabled)
|
||
{
|
||
QualitySettings.vSyncCount = enabled ? 1 : 0;
|
||
PlayerPrefs.SetInt(KeyVSync, enabled ? 1 : 0);
|
||
}
|
||
|
||
public void SetHeadbob(bool enabled)
|
||
{
|
||
PlayerPrefs.SetInt(KeyHeadbob, enabled ? 1 : 0);
|
||
}
|
||
}
|
||
} |