FlashWhenEnable.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using DG.Tweening;
  5. using UnityEngine.UI;
  6. public class FlashWhenEnable : MonoBehaviour
  7. {
  8. [SerializeField] private SpriteRenderer _image;
  9. [Header("Flash Settings")]
  10. [SerializeField] private float _fadeDuration = 1.5f;
  11. [SerializeField] private float _minAlpha = 0.4f;
  12. private Sequence _flashSequence;
  13. private void Awake()
  14. {
  15. _image = GetComponent<SpriteRenderer>();
  16. }
  17. void OnEnable()
  18. {
  19. StartFlashing();
  20. }
  21. void OnDisable()
  22. {
  23. StopFlashing();
  24. }
  25. private void StartFlashing()
  26. {
  27. // Kill any existing animation
  28. if (_flashSequence != null && _flashSequence.IsActive())
  29. _flashSequence.Kill();
  30. // Reset to original color
  31. Color originalColor = _image.color;
  32. originalColor.a = 1f;
  33. _image.color = originalColor;
  34. // Create subtle pulse animation
  35. _flashSequence = DOTween.Sequence()
  36. .Append(_image.DOFade(_minAlpha, _fadeDuration))
  37. .Append(_image.DOFade(1f, _fadeDuration))
  38. .Append(_image.transform.DOScale(0.385f, 0.34f))
  39. .SetLoops(-1, LoopType.Yoyo)
  40. .SetEase(Ease.InOutSine);
  41. }
  42. private void StopFlashing()
  43. {
  44. _flashSequence?.Kill();
  45. // Reset to full opacity
  46. Color c = _image.color;
  47. c.a = 1f;
  48. _image.color = c;
  49. }
  50. }