141 lines
4.3 KiB
C#
141 lines
4.3 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;
|
|
|
|
[Tooltip("Âñå CursorAnimator íà ñöåíå (ïî îäíîìó íà êàæäîãî èãðîêà)")]
|
|
[SerializeField] private CursorAnimator[] cursorAnimators;
|
|
|
|
[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("Âîäà (Îñòðîâ)")]
|
|
public Color waterShallowColorIsland;
|
|
public Color waterDeepColorIsland;
|
|
public Color waterFresnelColorIsland;
|
|
|
|
[Header("Âîäà (Îêåàí)")]
|
|
public Color waterShallowColorOcean;
|
|
public Color waterDeepColorOcean;
|
|
public Color waterFresnelColorOcean;
|
|
|
|
[Header("UI Êóðñîð")]
|
|
public Color cursorFreeColor = new Color(0.1f, 1f, 0.1f, 0.85f);
|
|
public Color cursorBlockedColor = new Color(1f, 0.1f, 0.1f, 0.85f);
|
|
}
|
|
|
|
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);
|
|
}
|
|
else if(timeOfDay == "Äåíü_2")
|
|
{
|
|
SwitchToPreset(2);
|
|
}
|
|
}
|
|
|
|
/// <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 (cursorAnimators != null)
|
|
foreach (var ca in cursorAnimators)
|
|
if (ca != null)
|
|
ca.SetColors(preset.cursorFreeColor, preset.cursorBlockedColor);
|
|
}
|
|
}
|