123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using DG.Tweening;
- using UnityEngine.UI;
- public class FlashWhenEnable : MonoBehaviour
- {
- [SerializeField] private SpriteRenderer _image;
- [Header("Flash Settings")]
- [SerializeField] private float _fadeDuration = 1.5f;
- [SerializeField] private float _minAlpha = 0.4f;
-
- private Sequence _flashSequence;
- private void Awake()
- {
- _image = GetComponent<SpriteRenderer>();
- }
- void OnEnable()
- {
- StartFlashing();
- }
- void OnDisable()
- {
- StopFlashing();
- }
- private void StartFlashing()
- {
- // Kill any existing animation
- if (_flashSequence != null && _flashSequence.IsActive())
- _flashSequence.Kill();
- // Reset to original color
- Color originalColor = _image.color;
- originalColor.a = 1f;
- _image.color = originalColor;
- // Create subtle pulse animation
- _flashSequence = DOTween.Sequence()
- .Append(_image.DOFade(_minAlpha, _fadeDuration))
- .Append(_image.DOFade(1f, _fadeDuration))
- .Append(_image.transform.DOScale(0.385f, 0.34f))
- .SetLoops(-1, LoopType.Yoyo)
- .SetEase(Ease.InOutSine);
- }
- private void StopFlashing()
- {
- _flashSequence?.Kill();
- // Reset to full opacity
- Color c = _image.color;
- c.a = 1f;
- _image.color = c;
-
- }
- }
|