143 lines
4.4 KiB
C#
143 lines
4.4 KiB
C#
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);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Ïåðåêëþ÷àåò íà ñëåäóþùèé ïðåñåò â ìàññèâå.
|
|
/// </summary>
|
|
public void SwitchToNextPreset()
|
|
{
|
|
if (presets == null || presets.Length == 0) return;
|
|
|
|
int nextIndex = (_currentPresetIndex + 1) % presets.Length;
|
|
SwitchToPreset(nextIndex);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Ïåðåêëþ÷àåò íà ïðåñåò ïî óêàçàííîìó èíäåêñó.
|
|
/// </summary>
|
|
public void SwitchToPreset(int index)
|
|
{
|
|
if (presets == null || index < 0 || index >= presets.Length)
|
|
{
|
|
Debug.LogWarning("Íåâåðíûé èíäåêñ ïðåñåòà èëè ìàññèâ ïðåñåòîâ ïóñò.");
|
|
return;
|
|
}
|
|
|
|
ApplyPreset(presets[index]);
|
|
_currentPresetIndex = index;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Ìãíîâåííî ïðèìåíÿåò âñå ïàðàìåòðû èç óêàçàííîãî ïðåñåòà.
|
|
/// </summary>
|
|
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;
|
|
}
|
|
}
|
|
}
|