Files
Echo/Assets/_Project/Scripts/EndingSystem/EndingLightController.cs
T
2026-07-08 20:42:51 +08:00

89 lines
2.3 KiB
C#

using System.Collections;
using UnityEngine;
#if DOTWEEN
using DG.Tweening;
#endif
namespace EndingSystem
{
public class EndingLightController : MonoBehaviour
{
[Header("Light Reference")]
public Light targetLight;
[Header("Animation Settings")]
[Tooltip("The final intensity of the light when fully turned on")]
public float targetIntensity = 5f;
[Tooltip("How long it takes for the light to reach target intensity")]
public float fadeDuration = 3f;
#if DOTWEEN
[Header("DOTween")]
public Ease fadeEase = Ease.InOutSine;
private Tween currentTween;
#endif
private void Awake()
{
if (targetLight != null)
{
targetLight.intensity = 0f;
targetLight.enabled = true;
}
}
public void TurnOnLight(bool useUnscaledTime = false)
{
if (targetLight != null)
{
#if DOTWEEN
PlayFade(useUnscaledTime);
#else
StartCoroutine(FadeLightCoroutine());
#endif
}
}
#if DOTWEEN
private void OnDisable()
{
if (currentTween != null)
{
currentTween.Kill(false);
currentTween = null;
}
}
private void PlayFade(bool useUnscaledTime)
{
if (currentTween != null)
{
currentTween.Kill(false);
currentTween = null;
}
float duration = Mathf.Max(0.0001f, fadeDuration);
currentTween = DOTween.To(() => targetLight.intensity, v => targetLight.intensity = v, targetIntensity, duration)
.SetEase(fadeEase)
.SetTarget(targetLight)
.SetUpdate(useUnscaledTime);
}
#endif
private IEnumerator FadeLightCoroutine()
{
float t = 0f;
float startIntensity = targetLight.intensity;
while (t < 1f)
{
t += Time.deltaTime / fadeDuration;
targetLight.intensity = Mathf.Lerp(startIntensity, targetIntensity, t);
yield return null;
}
targetLight.intensity = targetIntensity;
}
}
}