using Pixelplacement;
using UnityEngine;
namespace UI
{
///
/// Adds a CanvasGroup (if missing) and plays a continuous blinking tween on its alpha.
/// Attach to any GameObject to make it pulse/fade in and out.
///
[DisallowMultipleComponent]
[RequireComponent(typeof(CanvasGroup))]
public class BlinkingCanvasGroup : MonoBehaviour
{
[Header("Blink Settings")] [Tooltip("Minimum alpha value during blink")] [Range(0f, 1f)]
public float minAlpha;
[Tooltip("Maximum alpha value during blink")] [Range(0f, 1f)]
public float maxAlpha = 1f;
[Tooltip("Duration of one leg (min->max or max->min)")]
public float legDuration = 0.5f;
[Tooltip("Delay before starting the blinking")]
public float startDelay;
[Tooltip("Whether the tween should obey Time.timeScale (false = unscaled)")]
public bool obeyTimescale;
[Tooltip("Optional animation curve for easing. Leave null for default ease in/out.")]
public AnimationCurve easeCurve;
// Internal
private CanvasGroup _canvasGroup;
private Pixelplacement.TweenSystem.TweenBase _activeTween;
void Awake()
{
// Ensure we have a CanvasGroup
_canvasGroup = GetComponent();
if (_canvasGroup == null)
{
_canvasGroup = gameObject.AddComponent();
}
// Ensure starting alpha
_canvasGroup.alpha = minAlpha;
}
void Start()
{
StartBlinking();
}
void OnEnable()
{
// If re-enabled, ensure tween is running
if (_activeTween == null)
StartBlinking();
}
void OnDisable()
{
StopBlinking();
}
void OnDestroy()
{
StopBlinking();
}
///
/// Start the continuous blinking tween.
///
public void StartBlinking()
{
StopBlinking();
if (_canvasGroup == null) return;
// Use PingPong-like behavior by chaining two opposite legs with LoopType.Loop
// Simpler: Tween.CanvasGroupAlpha has an overload that sets startAlpha then end.
// We'll tween min->max then set LoopType.PingPong if available. The API supports LoopType.PingPong.
// Start from minAlpha to maxAlpha
_canvasGroup.alpha = minAlpha;
// Use the Tween.Value overload for float with obeyTimescale parameter
_activeTween = Tween.Value(minAlpha, maxAlpha, v => _canvasGroup.alpha = v, legDuration, startDelay,
easeCurve ?? Tween.EaseInOut, Tween.LoopType.PingPong, null, null, obeyTimescale);
}
///
/// Stops the blinking tween if running.
///
public void StopBlinking()
{
if (_activeTween != null)
{
try
{
_activeTween.Cancel();
}
catch
{
// Some versions of the tween API may not expose Cancel; ignore safely.
}
_activeTween = null;
}
}
}
}