using UnityEngine;
using UnityEngine.UI; // Добавлено для работы с Image/Renderer
public class EnvironmentManager : MonoBehaviour
{
[Header("Ссылки на объекты сцены")]
[SerializeField] private Light directionalLight;
[SerializeField] private Material waterMaterialIsland;
[SerializeField] private Material waterMaterialOcean;
[SerializeField] private Material cursorRenderer;
[SerializeField] private Material cursorFrameRenderer;
[Header("Пресеты окружения")]
[SerializeField] private EnvironmentPreset[] presets;
private int _currentPresetIndex = -1;
[System.Serializable]
public class EnvironmentPreset
{
public string name; // Для удобства в инспекторе
public Material skybox;
public Color lightColor = Color.white;
[Range(0, 8)] public float lightIntensity = 1.0f;
[Header("Вода (Остров)")]
[ColorUsage(true, true)] public Color waterShallowColorIsland;
[ColorUsage(true, true)] public Color waterDeepColorIsland;
[ColorUsage(true, true)] public Color waterFresnelColorIsland;
[Header("Вода (Океан)")]
[ColorUsage(true, true)] public Color waterShallowColorOcean;
[ColorUsage(true, true)] public Color waterDeepColorOcean;
[ColorUsage(true, true)] public Color waterFresnelColorOcean;
[Header("UI Курсор")]
public Color cursorColor = Color.white;
public Color cursorFrameColor = Color.white;
}
private void Start()
{
// Подписываемся на событие изменения настроек
if (SettingsManager.instance != null)
{
SettingsManager.instance.OnSettingsChanged.AddListener(OnSettingsChanged);
}
// Устанавливаем начальное состояние
OnSettingsChanged();
}
private void OnDestroy()
{
// Отписываемся от события
if (SettingsManager.instance != null)
{
SettingsManager.instance.OnSettingsChanged.RemoveListener(OnSettingsChanged);
}
}
private void OnSettingsChanged()
{
if (SettingsManager.instance == null) return;
string timeOfDay = SettingsManager.instance.GetSettingValue("Игровая тема");
if (timeOfDay == "День")
{
SwitchToPreset(0);
}
else if (timeOfDay == "Ночь")
{
SwitchToPreset(1);
}
}
///
/// Переключает на следующий пресет в массиве.
///
public void SwitchToNextPreset()
{
if (presets == null || presets.Length == 0) return;
int nextIndex = (_currentPresetIndex + 1) % presets.Length;
SwitchToPreset(nextIndex);
}
///
/// Переключает на пресет по указанному индексу.
///
public void SwitchToPreset(int index)
{
if (presets == null || index < 0 || index >= presets.Length)
{
Debug.LogWarning("Неверный индекс пресета или массив пресетов пуст.");
return;
}
ApplyPreset(presets[index]);
_currentPresetIndex = index;
}
///
/// Мгновенно применяет все параметры из указанного пресета.
///
private void ApplyPreset(EnvironmentPreset preset)
{
if (directionalLight != null)
{
directionalLight.color = preset.lightColor;
directionalLight.intensity = preset.lightIntensity;
}
if (preset.skybox != null)
{
RenderSettings.skybox = preset.skybox;
}
if (waterMaterialIsland != null)
{
waterMaterialIsland.SetColor("_ShallowColor", preset.waterShallowColorIsland);
waterMaterialIsland.SetColor("_DeepColor", preset.waterDeepColorIsland);
waterMaterialIsland.SetColor("_FresnelColor", preset.waterFresnelColorIsland);
}
if (waterMaterialOcean != null)
{
waterMaterialOcean.SetColor("_ShallowColor", preset.waterShallowColorOcean);
waterMaterialOcean.SetColor("_DeepColor", preset.waterDeepColorOcean);
waterMaterialOcean.SetColor("_FresnelColor", preset.waterFresnelColorOcean);
}
if (cursorRenderer != null)
{
cursorRenderer.color = preset.cursorColor;
}
if (cursorFrameRenderer != null)
{
cursorFrameRenderer.color = preset.cursorFrameColor;
}
}
}