152 lines
3.7 KiB
C#
152 lines
3.7 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
|
|
public class SettingsManager : MonoBehaviour
|
|
{
|
|
public static SettingsManager Instance;
|
|
|
|
public int qualityIndex;
|
|
public int resolutionIndex;
|
|
public bool isFullScreen;
|
|
|
|
private Resolution[] allResolutions;
|
|
public Resolution[] FilteredResolutions { get; private set; }
|
|
|
|
private readonly Vector2Int[] commonResolutions =
|
|
{
|
|
new(1280, 720),
|
|
new(1280, 768),
|
|
new(1280, 800),
|
|
new(1360, 768),
|
|
new(1366, 768),
|
|
new(1680, 1050),
|
|
new(1920, 1080)
|
|
};
|
|
|
|
private void Awake()
|
|
{
|
|
Debug.Log("SettingsManager Awake: " + gameObject.name);
|
|
|
|
if (Instance != null)
|
|
{
|
|
Destroy(gameObject);
|
|
return;
|
|
}
|
|
|
|
Instance = this;
|
|
DontDestroyOnLoad(gameObject);
|
|
|
|
InitResolutions();
|
|
LoadSettings();
|
|
|
|
// 🔥 추가
|
|
SyncResolutionIndexWithCurrentScreen();
|
|
|
|
ApplySettings();
|
|
}
|
|
|
|
void InitResolutions()
|
|
{
|
|
allResolutions = Screen.resolutions;
|
|
|
|
var list = new System.Collections.Generic.List<Resolution>();
|
|
|
|
foreach (var res in allResolutions)
|
|
{
|
|
foreach (var common in commonResolutions)
|
|
{
|
|
if (res.width == common.x && res.height == common.y)
|
|
{
|
|
if (!list.Exists(r => r.width == res.width && r.height == res.height))
|
|
list.Add(res);
|
|
}
|
|
}
|
|
}
|
|
|
|
FilteredResolutions = list.ToArray();
|
|
}
|
|
|
|
// ===== 적용 메서드 =====
|
|
public void SetQuality(int index)
|
|
{
|
|
qualityIndex = index;
|
|
QualitySettings.SetQualityLevel(index);
|
|
PlayerPrefs.SetInt("Quality", index);
|
|
}
|
|
|
|
public void SetResolution(int index)
|
|
{
|
|
resolutionIndex = index;
|
|
ApplyResolution();
|
|
PlayerPrefs.SetInt("Resolution", index);
|
|
}
|
|
|
|
public void SetFullScreen(bool full)
|
|
{
|
|
isFullScreen = full;
|
|
|
|
Screen.fullScreenMode = full
|
|
? FullScreenMode.FullScreenWindow
|
|
: FullScreenMode.Windowed;
|
|
|
|
ApplyResolution();
|
|
PlayerPrefs.SetInt("FullScreen", full ? 1 : 0);
|
|
}
|
|
|
|
void ApplyResolution()
|
|
{
|
|
var res = FilteredResolutions[resolutionIndex];
|
|
Screen.SetResolution(
|
|
res.width,
|
|
res.height,
|
|
isFullScreen ? FullScreenMode.FullScreenWindow : FullScreenMode.Windowed
|
|
);
|
|
}
|
|
|
|
void LoadSettings()
|
|
{
|
|
qualityIndex = PlayerPrefs.GetInt("Quality", QualitySettings.names.Length - 1);
|
|
resolutionIndex = PlayerPrefs.GetInt("Resolution", 0);
|
|
isFullScreen = PlayerPrefs.GetInt("FullScreen", 1) == 1;
|
|
}
|
|
|
|
void ApplySettings()
|
|
{
|
|
SetQuality(qualityIndex);
|
|
Screen.fullScreenMode = isFullScreen
|
|
? FullScreenMode.FullScreenWindow
|
|
: FullScreenMode.Windowed;
|
|
ApplyResolution();
|
|
}
|
|
|
|
void SyncResolutionIndexWithCurrentScreen()
|
|
{
|
|
for (int i = 0; i < FilteredResolutions.Length; i++)
|
|
{
|
|
if (FilteredResolutions[i].width == Screen.width &&
|
|
FilteredResolutions[i].height == Screen.height)
|
|
{
|
|
resolutionIndex = i;
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
SceneManager.sceneLoaded += OnSceneLoaded;
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
SceneManager.sceneLoaded -= OnSceneLoaded;
|
|
}
|
|
|
|
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
|
|
{
|
|
ApplySettings(); // 🔥 씬 로드 직후 다시 적용
|
|
}
|
|
}
|