demo
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
#nullable enable
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
|
||||
namespace ReplacedPerson.Runtime
|
||||
{
|
||||
[DisallowMultipleComponent]
|
||||
public sealed class ReplacedCardMotion : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, ISelectHandler, IDeselectHandler
|
||||
{
|
||||
private CanvasGroup? _group;
|
||||
private Coroutine? _entrance;
|
||||
private Action? _focus;
|
||||
private bool _hovered;
|
||||
private bool _dragging;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_group = GetComponent<CanvasGroup>() ?? gameObject.AddComponent<CanvasGroup>();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (_entrance != null || _group == null) return;
|
||||
var targetScale = _dragging ? .96f : _hovered ? 1.04f : 1f;
|
||||
var targetAlpha = _dragging ? .42f : 1f;
|
||||
transform.localScale = Vector3.Lerp(transform.localScale, Vector3.one * targetScale, 1f - Mathf.Exp(-18f * Time.unscaledDeltaTime));
|
||||
_group.alpha = Mathf.Lerp(_group.alpha, targetAlpha, 1f - Mathf.Exp(-22f * Time.unscaledDeltaTime));
|
||||
}
|
||||
|
||||
public void Configure(Action focus, float entranceDelay)
|
||||
{
|
||||
_focus = focus;
|
||||
if (_entrance != null) StopCoroutine(_entrance);
|
||||
_entrance = StartCoroutine(PlayEntrance(entranceDelay));
|
||||
}
|
||||
|
||||
public void SetDragging(bool dragging)
|
||||
{
|
||||
_dragging = dragging;
|
||||
}
|
||||
|
||||
public void OnPointerEnter(PointerEventData eventData)
|
||||
{
|
||||
_hovered = true;
|
||||
_focus?.Invoke();
|
||||
}
|
||||
|
||||
public void OnPointerExit(PointerEventData eventData) => _hovered = false;
|
||||
|
||||
public void OnSelect(BaseEventData eventData)
|
||||
{
|
||||
_hovered = true;
|
||||
_focus?.Invoke();
|
||||
}
|
||||
|
||||
public void OnDeselect(BaseEventData eventData) => _hovered = false;
|
||||
|
||||
private IEnumerator PlayEntrance(float delay)
|
||||
{
|
||||
if (_group == null) yield break;
|
||||
_group.alpha = 0f;
|
||||
transform.localScale = Vector3.one * .90f;
|
||||
if (delay > 0) yield return new WaitForSecondsRealtime(delay);
|
||||
const float duration = .20f;
|
||||
var elapsed = 0f;
|
||||
while (elapsed < duration)
|
||||
{
|
||||
elapsed += Time.unscaledDeltaTime;
|
||||
var t = Mathf.Clamp01(elapsed / duration);
|
||||
var eased = 1f - Mathf.Pow(1f - t, 3f);
|
||||
_group.alpha = eased;
|
||||
transform.localScale = Vector3.one * Mathf.Lerp(.90f, 1f, eased);
|
||||
yield return null;
|
||||
}
|
||||
_group.alpha = 1f;
|
||||
transform.localScale = Vector3.one;
|
||||
_entrance = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user