From 7f493f682503f5186308de7b8f74b5b49233cfe4 Mon Sep 17 00:00:00 2001 From: chai <215380520@qq.com> Date: Thu, 2 Nov 2023 11:51:31 +0800 Subject: +init --- GameCode/DaytimeSensitiveLight.cs | 83 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 GameCode/DaytimeSensitiveLight.cs (limited to 'GameCode/DaytimeSensitiveLight.cs') diff --git a/GameCode/DaytimeSensitiveLight.cs b/GameCode/DaytimeSensitiveLight.cs new file mode 100644 index 0000000..da571d3 --- /dev/null +++ b/GameCode/DaytimeSensitiveLight.cs @@ -0,0 +1,83 @@ +using System.Collections; +using UnityEngine; + +[RequireComponent(typeof(Light))] +public class DaytimeSensitiveLight : MonoBehaviour, DayNightCycle.IDaytimeSensitive +{ + [SerializeField] + private Color dayColor = Color.white; + + [SerializeField] + private Color sunsetColor; + + [SerializeField] + private Color nightColor; + + [SerializeField] + private float dayToSunsetDuration = 2f; + + [SerializeField] + private float sunsetToNightDuration = 2f; + + private Light targetLight; + + private void Start() + { + targetLight = GetComponent(); + DayNightCycle.Instance.RegisterDaytimeSensitiveObject(this); + } + + public void OnDawn_AfterSunrise() + { + } + + public void OnDusk() + { + StopAllCoroutines(); + StartCoroutine(ToNight()); + } + + public void OnDawn_BeforeSunrise() + { + StopAllCoroutines(); + StartCoroutine(ToDay()); + } + + private IEnumerator ToDay() + { + float timer2 = 0f; + while (timer2 <= sunsetToNightDuration) + { + timer2 += Time.deltaTime; + targetLight.color = Color.Lerp(nightColor, sunsetColor, timer2 / sunsetToNightDuration); + yield return null; + } + timer2 = 0f; + while (timer2 <= dayToSunsetDuration) + { + timer2 += Time.deltaTime; + targetLight.color = Color.Lerp(sunsetColor, dayColor, timer2 / dayToSunsetDuration); + yield return null; + } + targetLight.color = dayColor; + } + + private IEnumerator ToNight() + { + float timer2 = 0f; + while (timer2 <= dayToSunsetDuration) + { + timer2 += Time.deltaTime; + targetLight.color = Color.Lerp(dayColor, sunsetColor, timer2 / dayToSunsetDuration); + yield return null; + } + timer2 = 0f; + while (timer2 <= sunsetToNightDuration) + { + timer2 += Time.deltaTime; + targetLight.color = Color.Lerp(dayColor, nightColor, timer2 / sunsetToNightDuration); + yield return null; + } + targetLight.color = nightColor; + } +} -- cgit v1.1-26-g67d0