using UnityEngine;
using UnityEngine.UI;
using Pixelplacement;
namespace Utils.UI
{
///
/// UI component that animates a screenshot flying away and disappearing.
/// Uses Pixelplacement Tween library for smooth animations with curves.
/// Attach to a UI prefab with frame (Image) and picture (Image) components.
///
public class ScreenshotFlyawayAnimation : MonoBehaviour
{
[Header("UI Components")]
[SerializeField] private Image _frame;
[SerializeField] private Image _picture;
[Header("Animation Settings")]
[Tooltip("Target offset from starting position (in UI space)")]
[SerializeField] private Vector2 _targetOffset = new Vector2(0, 300f);
[Tooltip("Number of full rotations during animation")]
[SerializeField] private float _spinRotations = 2f;
[Tooltip("Duration of the entire animation in seconds")]
[SerializeField] private float _duration = 1.5f;
[Tooltip("Movement curve for position animation")]
[SerializeField] private AnimationCurve _moveCurve = AnimationCurve.EaseInOut(0, 0, 1, 1);
[Tooltip("Scale curve for shrinking animation")]
[SerializeField] private AnimationCurve _scaleCurve = AnimationCurve.EaseInOut(0, 1, 1, 0);
[Tooltip("Rotation curve for spinning animation")]
[SerializeField] private AnimationCurve _rotationCurve = AnimationCurve.Linear(0, 0, 1, 1);
private RectTransform _rectTransform;
private bool _isAnimating;
private Transform _targetTransform; // Target to fly toward (optional)
private void Awake()
{
_rectTransform = GetComponent();
// Hide initially - will be shown when animation plays
gameObject.SetActive(false);
// Validate components
if (_frame == null)
{
Debug.LogWarning("[ScreenshotFlyawayAnimation] Frame image not assigned!");
}
if (_picture == null)
{
Debug.LogWarning("[ScreenshotFlyawayAnimation] Picture image not assigned!");
}
}
///
/// Start the flyaway animation with the captured screenshot texture.
/// Unparents to root canvas to survive viewfinder destruction.
///
/// The screenshot texture to display
/// Optional target transform to fly toward (if null, uses _targetOffset)
public void PlayAnimation(Texture2D photo, Transform target = null)
{
if (_isAnimating)
{
Debug.LogWarning("[ScreenshotFlyawayAnimation] Animation already playing!");
return;
}
if (photo == null)
{
Debug.LogError("[ScreenshotFlyawayAnimation] Photo texture is null!");
Destroy(gameObject);
return;
}
// Store target for StartAnimation
_targetTransform = target;
// Show the GameObject before playing animation
gameObject.SetActive(true);
// Unparent to root canvas to survive viewfinder destruction
Canvas rootCanvas = GetComponentInParent