Files
AppleHillsProduction/Assets/Scripts/UI/BlinkingCanvasGroup.cs
tschesky 5a85a602bd Revamp game pausing and input handling. Fix minigame tutorial and end sequence. (#39)
- Revamp pausing and centralize management in GameManager
- Switch Pause implementation to be counter-based to solve corner case of multiple pause requests
- Remove duplicated Pause logic from other components
- Add pausing when browsing the card album
- Fully deliver the exclusive UI implementation
- Spruce up the MiniGame tutorial with correct pausing, hiding other UI
- Correctly unpause after showing tutorial
- Fix minigame ending sequence. The cinematic correctly plays only once now
- Replaying the minigame works

Co-authored-by: Michal Adam Pikulski <michal@foolhardyhorizons.com>
Co-authored-by: Michal Pikulski <michal@foolhardyhorizons.com>
Reviewed-on: #39
2025-10-24 11:09:32 +00:00

112 lines
3.3 KiB
C#

using Pixelplacement;
using UnityEngine;
namespace UI
{
/// <summary>
/// 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.
/// </summary>
[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<CanvasGroup>();
if (_canvasGroup == null)
{
_canvasGroup = gameObject.AddComponent<CanvasGroup>();
}
// 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();
}
/// <summary>
/// Start the continuous blinking tween.
/// </summary>
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);
}
/// <summary>
/// Stops the blinking tween if running.
/// </summary>
public void StopBlinking()
{
if (_activeTween != null)
{
try
{
_activeTween.Cancel();
}
catch
{
// Some versions of the tween API may not expose Cancel; ignore safely.
}
_activeTween = null;
}
}
}
}