RepoTown/Assets/Scripts/GameSettingsManager.cs
2025-07-25 15:29:14 +05:00

242 lines
7.0 KiB
C#

using UnityEngine;
using UnityEngine.Audio;
public class GameSettingsManager : MonoBehaviour
{
public static GameSettingsManager Instance;
[Header("Settings")]
[Range(0f, 1f)]
public float musicVolume;
[Range(0.1f, 10f)]
public float mouseHorizontalSensitivity = 2f;
[Range(0.1f, 10f)]
public float mouseVerticalSensitivity = 2f;
public bool invertMouse = false;
public bool fullScreen = true;
public int shadowQuality = 2; // 0 = Off, 1 = Low, 2 = High
public int textureQuality = 2; // 0 = Low, 1 = Medium, 2 = High
[Header("Audio")]
public AudioSource musicAudioSource;
void Awake()
{
// Singleton pattern - persist across scenes
if (Instance != null && Instance != this)
{
Destroy(gameObject);
return;
}
Instance = this;
DontDestroyOnLoad(gameObject);
// Load settings from PlayerPrefs
LoadSettings();
}
void Start()
{
// Apply all settings
ApplyAllSettings();
}
public void LoadSettings()
{
Debug.Log("[GameSettingsManager] Loading settings from PlayerPrefs");
// Load from PlayerPrefs with default values
musicVolume = PlayerPrefs.GetFloat("MusicVolume", musicVolume);
mouseHorizontalSensitivity = PlayerPrefs.GetFloat("XSensitivity", 2f);
mouseVerticalSensitivity = PlayerPrefs.GetFloat("YSensitivity", 2f);
invertMouse = PlayerPrefs.GetInt("Inverted", 0) == 1;
fullScreen = PlayerPrefs.GetInt("FullScreen", 1) == 1;
shadowQuality = PlayerPrefs.GetInt("Shadows", 2);
textureQuality = PlayerPrefs.GetInt("Textures", 2);
}
public void SaveSettings()
{
// Save to PlayerPrefs
PlayerPrefs.SetFloat("MusicVolume", musicVolume);
PlayerPrefs.SetFloat("XSensitivity", mouseHorizontalSensitivity);
PlayerPrefs.SetFloat("YSensitivity", mouseVerticalSensitivity);
PlayerPrefs.SetInt("Inverted", invertMouse ? 1 : 0);
PlayerPrefs.SetInt("FullScreen", fullScreen ? 1 : 0);
PlayerPrefs.SetInt("Shadows", shadowQuality);
PlayerPrefs.SetInt("Textures", textureQuality);
PlayerPrefs.Save();
}
public void ApplyAllSettings()
{
ApplyMusicVolume();
ApplyVideoSettings();
NotifyPlayerController();
}
public void ApplyMusicVolume()
{
// Apply to AudioManager if it exists (primary method)
if (AudioManager.Instance != null)
{
AudioManager.Instance.SetMusicVolume(musicVolume);
}
// Apply to local music source if assigned (backup method)
if (musicAudioSource != null)
{
musicAudioSource.volume = musicVolume;
}
// Find any music sources in the scene and apply volume (legacy support)
AudioSource[] audioSources = FindObjectsByType<AudioSource>(FindObjectsSortMode.None);
foreach (AudioSource source in audioSources)
{
if (source.gameObject.name.ToLower().Contains("music") ||
source.gameObject.tag == "MusicPlayer")
{
source.volume = musicVolume;
}
}
}
public void ApplyVideoSettings()
{
// Apply fullscreen
if (Screen.fullScreen != fullScreen)
{
Screen.fullScreen = fullScreen;
}
// Apply shadow quality
switch (shadowQuality)
{
case 0: // Off
QualitySettings.shadowCascades = 0;
QualitySettings.shadowDistance = 0;
break;
case 1: // Low
QualitySettings.shadowCascades = 2;
QualitySettings.shadowDistance = 75;
break;
case 2: // High
QualitySettings.shadowCascades = 4;
QualitySettings.shadowDistance = 500;
break;
}
// Apply texture quality
switch (textureQuality)
{
case 0: // Low
QualitySettings.globalTextureMipmapLimit = 2;
break;
case 1: // Medium
QualitySettings.globalTextureMipmapLimit = 1;
break;
case 2: // High
QualitySettings.globalTextureMipmapLimit = 0;
break;
}
}
public void NotifyPlayerController()
{
// Find and update the FirstPersonController with mouse sensitivity
var playerController = FindFirstObjectByType<Polyart.FirstPersonController_Polyart>();
if (playerController != null)
{
playerController.UpdateMouseSettings(mouseHorizontalSensitivity, mouseVerticalSensitivity, invertMouse);
}
}
// Public methods for UI to call when settings change
public void SetMusicVolume(float volume)
{
musicVolume = Mathf.Clamp01(volume);
SaveSettings();
ApplyMusicVolume();
}
public void SetMouseSensitivity(float horizontal, float vertical)
{
mouseHorizontalSensitivity = Mathf.Clamp(horizontal, 0.1f, 10f);
mouseVerticalSensitivity = Mathf.Clamp(vertical, 0.1f, 10f);
SaveSettings();
NotifyPlayerController();
}
public void SetInvertMouse(bool invert)
{
invertMouse = invert;
SaveSettings();
NotifyPlayerController();
}
public void SetFullScreen(bool fullscreen)
{
fullScreen = fullscreen;
SaveSettings();
ApplyVideoSettings();
}
public void SetShadowQuality(int quality)
{
shadowQuality = Mathf.Clamp(quality, 0, 2);
SaveSettings();
ApplyVideoSettings();
}
public void SetTextureQuality(int quality)
{
textureQuality = Mathf.Clamp(quality, 0, 2);
SaveSettings();
ApplyVideoSettings();
}
// Called when a new scene loads to ensure settings are applied
void OnEnable()
{
UnityEngine.SceneManagement.SceneManager.sceneLoaded += OnSceneLoaded;
}
void OnDisable()
{
UnityEngine.SceneManagement.SceneManager.sceneLoaded -= OnSceneLoaded;
}
private void OnSceneLoaded(UnityEngine.SceneManagement.Scene scene, UnityEngine.SceneManagement.LoadSceneMode mode)
{
// Reload and apply settings when a new scene loads
LoadSettings();
// Small delay to ensure AudioManager and UI are ready
StartCoroutine(DelayedSettingsApplication());
}
private System.Collections.IEnumerator DelayedSettingsApplication()
{
// Wait for a frame to ensure everything is initialized
yield return new WaitForEndOfFrame();
ApplyAllSettings();
}
// Legacy method for older Unity versions
void OnLevelWasLoaded(int level)
{
ApplyAllSettings();
}
// Public method to refresh settings (call this when settings change in UI)
public void RefreshSettings()
{
LoadSettings();
ApplyAllSettings();
}
}