48 lines
1.5 KiB
C#
48 lines
1.5 KiB
C#
|
|
using System;
|
||
|
|
using System.Collections;
|
||
|
|
using UnityEngine;
|
||
|
|
using UnityEngine.UI;
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 버튼 클릭 시 눌리는 애니메이션(스케일 펀치)을 재생합니다.
|
||
|
|
/// </summary>
|
||
|
|
public static class ButtonAnimator
|
||
|
|
{
|
||
|
|
/// <summary>버튼을 눌렀다 돌아오는 스케일 펀치 후 콜백 실행</summary>
|
||
|
|
public static void Play(Button button, Action onComplete = null,
|
||
|
|
float punchScale = 0.85f, float duration = 0.12f)
|
||
|
|
{
|
||
|
|
if (button == null) { onComplete?.Invoke(); return; }
|
||
|
|
button.StartCoroutine(PunchRoutine(button.transform, punchScale, duration, onComplete));
|
||
|
|
}
|
||
|
|
|
||
|
|
private static IEnumerator PunchRoutine(Transform t, float punchScale,
|
||
|
|
float halfDuration, Action onComplete)
|
||
|
|
{
|
||
|
|
Vector3 original = t.localScale;
|
||
|
|
Vector3 small = original * punchScale;
|
||
|
|
|
||
|
|
// 축소
|
||
|
|
float elapsed = 0f;
|
||
|
|
while (elapsed < halfDuration)
|
||
|
|
{
|
||
|
|
elapsed += Time.unscaledDeltaTime;
|
||
|
|
t.localScale = Vector3.Lerp(original, small, elapsed / halfDuration);
|
||
|
|
yield return null;
|
||
|
|
}
|
||
|
|
t.localScale = small;
|
||
|
|
|
||
|
|
// 복원
|
||
|
|
elapsed = 0f;
|
||
|
|
while (elapsed < halfDuration)
|
||
|
|
{
|
||
|
|
elapsed += Time.unscaledDeltaTime;
|
||
|
|
t.localScale = Vector3.Lerp(small, original, elapsed / halfDuration);
|
||
|
|
yield return null;
|
||
|
|
}
|
||
|
|
t.localScale = original;
|
||
|
|
|
||
|
|
onComplete?.Invoke();
|
||
|
|
}
|
||
|
|
}
|