76 lines
2.1 KiB
C#
76 lines
2.1 KiB
C#
using UnityEngine;
|
|
|
|
namespace Ashwild.Environment
|
|
{
|
|
// Pilote la saison globale de tous les shaders qui lisent "_Season" (ex: StylizedGrass).
|
|
// ExecuteAlways : la transition est visible directement dans l'éditeur, sans lancer le jeu.
|
|
[ExecuteAlways]
|
|
[AddComponentMenu("GAME/Environment/Season Manager")]
|
|
public class SeasonManager : MonoBehaviour
|
|
{
|
|
public static SeasonManager Instance { get; private set; }
|
|
|
|
public enum Season { Summer = 0, Autumn = 1, Winter = 2 }
|
|
|
|
[Header("Saison")]
|
|
[Tooltip("0 = Été (vert), 1 = Automne, 2 = Hiver, 3 = reboucle à l'été.")]
|
|
[Range(0f, 3f)] [SerializeField] private float season = 0f;
|
|
|
|
[Header("Cycle automatique")]
|
|
[SerializeField] private bool autoCycle = false;
|
|
[Tooltip("Durée d'une année complète (les 4 saisons) en secondes. N'avance qu'en Play.")]
|
|
[SerializeField] private float yearDuration = 120f;
|
|
|
|
private static readonly int SeasonId = Shader.PropertyToID("_Season");
|
|
|
|
public float SeasonValue => season;
|
|
|
|
void OnEnable()
|
|
{
|
|
Instance = this;
|
|
Apply();
|
|
}
|
|
|
|
void OnValidate()
|
|
{
|
|
yearDuration = Mathf.Max(0.01f, yearDuration);
|
|
Apply();
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (autoCycle && Application.isPlaying)
|
|
{
|
|
season += (3f / yearDuration) * Time.deltaTime;
|
|
if (season >= 3f) season -= 3f;
|
|
}
|
|
Apply();
|
|
}
|
|
|
|
void Apply()
|
|
{
|
|
Shader.SetGlobalFloat(SeasonId, season);
|
|
|
|
#if UNITY_EDITOR
|
|
// Rafraîchit la Scene view quand on scrute le slider hors Play.
|
|
if (!Application.isPlaying)
|
|
UnityEditor.SceneView.RepaintAll();
|
|
#endif
|
|
}
|
|
|
|
// --- API publique (gameplay / calendrier) -------------------------------------
|
|
|
|
public void SetSeason(float value)
|
|
{
|
|
season = Mathf.Repeat(value, 3f);
|
|
Apply();
|
|
}
|
|
|
|
public void SetSeason(Season s)
|
|
{
|
|
season = (float)s;
|
|
Apply();
|
|
}
|
|
}
|
|
}
|