51 lines
1.5 KiB
C#
51 lines
1.5 KiB
C#
#nullable enable
|
|
|
|
using System.Collections;
|
|
using UnityEngine;
|
|
|
|
namespace ReplacedPerson.Runtime
|
|
{
|
|
[DisallowMultipleComponent]
|
|
public sealed class ReplacedScreenMotion : MonoBehaviour
|
|
{
|
|
private CanvasGroup? _group;
|
|
private Coroutine? _animation;
|
|
|
|
public void Play()
|
|
{
|
|
if (_group == null) _group = GetComponent<CanvasGroup>() ?? gameObject.AddComponent<CanvasGroup>();
|
|
if (_animation != null) StopCoroutine(_animation);
|
|
_animation = StartCoroutine(Animate());
|
|
}
|
|
|
|
private IEnumerator Animate()
|
|
{
|
|
if (_group == null) yield break;
|
|
_group.alpha = 0f;
|
|
transform.localScale = Vector3.one * .985f;
|
|
const float duration = .22f;
|
|
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(.985f, 1f, eased);
|
|
yield return null;
|
|
}
|
|
_group.alpha = 1f;
|
|
transform.localScale = Vector3.one;
|
|
_animation = null;
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
if (_animation != null) StopCoroutine(_animation);
|
|
_animation = null;
|
|
if (_group != null) _group.alpha = 1f;
|
|
transform.localScale = Vector3.one;
|
|
}
|
|
}
|
|
}
|