53 lines
1.4 KiB
C#
53 lines
1.4 KiB
C#
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using System.Collections;
|
|
|
|
public class ButtonAnimasi : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
|
|
{
|
|
public float bounceScale = 1.15f;
|
|
public float duration = 0.3f;
|
|
|
|
private Vector3 originalScale;
|
|
private bool isBouncing = false;
|
|
|
|
void Start()
|
|
{
|
|
originalScale = transform.localScale;
|
|
}
|
|
|
|
public void OnPointerDown(PointerEventData eventData)
|
|
{
|
|
if (!isBouncing)
|
|
StartCoroutine(Bounce());
|
|
}
|
|
|
|
public void OnPointerUp(PointerEventData eventData) { }
|
|
|
|
IEnumerator Bounce()
|
|
{
|
|
isBouncing = true;
|
|
|
|
// Membesar smooth
|
|
float elapsed = 0f;
|
|
while (elapsed < duration / 2)
|
|
{
|
|
elapsed += Time.deltaTime;
|
|
float t = Mathf.SmoothStep(0f, 1f, elapsed / (duration / 2));
|
|
transform.localScale = Vector3.Lerp(originalScale, originalScale * bounceScale, t);
|
|
yield return null;
|
|
}
|
|
|
|
// Kembali smooth
|
|
elapsed = 0f;
|
|
while (elapsed < duration / 2)
|
|
{
|
|
elapsed += Time.deltaTime;
|
|
float t = Mathf.SmoothStep(0f, 1f, elapsed / (duration / 2));
|
|
transform.localScale = Vector3.Lerp(originalScale * bounceScale, originalScale, t);
|
|
yield return null;
|
|
}
|
|
|
|
transform.localScale = originalScale;
|
|
isBouncing = false;
|
|
}
|
|
} |