ManualLightBehavior.cs 907 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using UnityEngine;
  2. namespace Assets.Scripts.Utils
  3. {
  4. [RequireComponent(typeof(Light))]
  5. internal sealed class ManualLightBehavior : MonoBehaviour
  6. {
  7. public AnimationCurve LightCurve;
  8. public float GraphScaleX, GraphScaleY;
  9. private bool _isLightAnimationStarted;
  10. private float _startTime;
  11. private Light _lightSource;
  12. public void Play()
  13. {
  14. _startTime = Time.time;
  15. _isLightAnimationStarted = true;
  16. _lightSource.enabled = true;
  17. }
  18. private void Start()
  19. {
  20. _lightSource = GetComponent<Light>();
  21. }
  22. private void Update()
  23. {
  24. if (!_isLightAnimationStarted)
  25. return;
  26. var time = Time.time - _startTime;
  27. if (time > GraphScaleX)
  28. {
  29. //_startTime = Time.time;
  30. _isLightAnimationStarted = false;
  31. }
  32. var eval = LightCurve.Evaluate(time / GraphScaleX) / GraphScaleY;
  33. _lightSource.intensity = eval;
  34. }
  35. }
  36. }