using System.Collections; using System.Collections.Generic; using UnityEngine; using NaughtyAttributes; using Variables; public class DarknessController : MonoBehaviour { [SerializeField, Header("References")] private Material m_darknessMat; [SerializeField] private Reference m_lightLevel; [SerializeField] private Reference m_isInputDown; [SerializeField, BoxGroup("Settings")] private float m_darknessSpeed = 0.25f; [SerializeField, BoxGroup("Settings")] private float m_lightSpeed = 1.0f; [SerializeField, BoxGroup("Settings")] private AnimationCurve m_lightCurve = AnimationCurve.Linear(0,0,1,1); [ShowNonSerializedField] private bool m_isDarknessChanging; private void OnEnable() { m_isInputDown.OnValueChanged += OnInputChange; m_darknessMat.SetFloat("_Apply", 1); m_lightLevel.Value = 1; } private void OnDisable() { m_isInputDown.OnValueChanged -= OnInputChange; m_darknessMat.SetFloat("_Apply", 1); m_lightLevel.Value = 1; } private void OnInputChange(bool value) { StopAllCoroutines(); Debug.Log($"Here {m_isDarknessChanging}"); if (value) { if (!m_isDarknessChanging) { StartCoroutine(ChangeDarkness(0, m_darknessSpeed)); } else { StartCoroutine(ChangeDarkness(0, 0)); } } else StartCoroutine(ChangeDarkness(1, m_lightSpeed, m_lightCurve)); } private IEnumerator ChangeDarkness(float end, float time) { yield return StartCoroutine(ChangeDarkness(end, time, AnimationCurve.Linear(0, 0, 1, 1))); } private IEnumerator ChangeDarkness(float end, float time, AnimationCurve curve) { float start = m_lightLevel; float elapsedTime = 0; time = Mathf.InverseLerp(end, start, m_lightLevel) * time; m_isDarknessChanging = true; while(elapsedTime < time) { m_lightLevel.Value = Mathf.Lerp(start, end, curve.Evaluate(elapsedTime / time)); m_darknessMat.SetFloat("_Apply", m_lightLevel); yield return new WaitForEndOfFrame(); elapsedTime += Time.deltaTime; } m_isDarknessChanging = false; m_lightLevel.Value = end; m_darknessMat.SetFloat("_Apply", m_lightLevel); } }