61 lines
1.4 KiB
C#
61 lines
1.4 KiB
C#
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;
|
|
|
|
}
|
|
}
|