Initial commit
This commit is contained in:
87
Assets/Scripts/BouncyButton.cs
Normal file
87
Assets/Scripts/BouncyButton.cs
Normal file
@@ -0,0 +1,87 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
public class BouncyButton : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
|
||||
{
|
||||
bool isHovered = false;
|
||||
bool isHeld = false;
|
||||
bool didBounce = false;
|
||||
Vector3 originalScale;
|
||||
Vector3 finalScale;
|
||||
|
||||
void Start()
|
||||
{
|
||||
originalScale = transform.localScale;
|
||||
finalScale = originalScale * 1.1f;
|
||||
}
|
||||
|
||||
public void OnPointerEnter(PointerEventData eventData) => isHovered = true;
|
||||
public void OnPointerExit(PointerEventData eventData)
|
||||
{
|
||||
isHovered = false;
|
||||
ResetScale();
|
||||
}
|
||||
void OnDisable()
|
||||
{
|
||||
isHovered = false;
|
||||
ResetScale();
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (Application.isMobilePlatform)
|
||||
{
|
||||
isHeld = Touchscreen.current != null && Touchscreen.current.press.isPressed;
|
||||
}
|
||||
else
|
||||
{
|
||||
isHeld = Mouse.current != null && Mouse.current.leftButton.isPressed;
|
||||
}
|
||||
|
||||
if (isHovered && isHeld && !didBounce)
|
||||
{
|
||||
StartCoroutine(BounceOnce());
|
||||
didBounce = true;
|
||||
}
|
||||
|
||||
if ((!isHovered || !isHeld) && didBounce)
|
||||
{
|
||||
ResetScale();
|
||||
}
|
||||
}
|
||||
|
||||
void ResetScale()
|
||||
{
|
||||
StopAllCoroutines();
|
||||
transform.localScale = originalScale;
|
||||
didBounce = false;
|
||||
}
|
||||
|
||||
System.Collections.IEnumerator BounceOnce()
|
||||
{
|
||||
float t = 0.125f;
|
||||
Vector3 s1 = originalScale * 1.12f;
|
||||
Vector3 s2 = originalScale * 1.06f;
|
||||
Vector3 s3 = finalScale;
|
||||
|
||||
yield return LerpScale(originalScale, s1, t);
|
||||
yield return LerpScale(s1, s2, t * 0.5f);
|
||||
yield return LerpScale(s2, s3, t * 0.5f);
|
||||
|
||||
transform.localScale = s3;
|
||||
}
|
||||
|
||||
System.Collections.IEnumerator LerpScale(Vector3 from, Vector3 to, float duration)
|
||||
{
|
||||
float elapsed = 0f;
|
||||
while (elapsed < duration)
|
||||
{
|
||||
elapsed += Time.deltaTime;
|
||||
float p = elapsed / duration;
|
||||
transform.localScale = Vector3.Lerp(from, to, p);
|
||||
yield return null;
|
||||
}
|
||||
transform.localScale = to;
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/BouncyButton.cs.meta
Normal file
2
Assets/Scripts/BouncyButton.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 77dc06bcc97db4033b6cd70f52cf499a
|
||||
21
Assets/Scripts/BouncyText.cs
Normal file
21
Assets/Scripts/BouncyText.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using UnityEngine;
|
||||
using TMPro;
|
||||
|
||||
public class BouncyText : MonoBehaviour
|
||||
{
|
||||
public float frequency = 2f;
|
||||
public float minSize = 10f;
|
||||
public float maxSize = 12f;
|
||||
private TextMeshProUGUI text;
|
||||
|
||||
void Start()
|
||||
{
|
||||
text = GetComponent<TextMeshProUGUI>();
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
float newsize = (Mathf.Sin(Time.time * frequency) + 1f) / 2f;
|
||||
text.fontSize = Mathf.Lerp(minSize, maxSize, newsize);
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/BouncyText.cs.meta
Normal file
2
Assets/Scripts/BouncyText.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 47710343a5b0f4f9bac81fe019d3b7b8
|
||||
Reference in New Issue
Block a user