94 lines
2.6 KiB
C#
94 lines
2.6 KiB
C#
using System.Collections;
|
||
using UnityEngine;
|
||
using UnityEngine.InputSystem;
|
||
|
||
public class AButtonPress : MonoBehaviour
|
||
{
|
||
[Header("Target")]
|
||
[SerializeField] private RectTransform buttonRect; // your Button image RectTransform
|
||
|
||
[Header("Input")]
|
||
[SerializeField] private InputActionReference advanceAction; // same action you use to advance text
|
||
|
||
[Header("Active Guard (optional but recommended)")]
|
||
[SerializeField] private CanvasGroup textboxCanvas; // the CanvasGroup of your textbox
|
||
[SerializeField] private float visibleCutoff = 0.95f; // only animate when alpha >= this
|
||
|
||
[Header("Motion")]
|
||
[SerializeField] private float offsetY = -6f; // “few millimeters” in UI pixels
|
||
[SerializeField] private float downTime = 0.06f;
|
||
[SerializeField] private float upTime = 0.08f;
|
||
|
||
Vector2 startPos;
|
||
Coroutine anim;
|
||
|
||
void Awake()
|
||
{
|
||
if (!buttonRect) buttonRect = GetComponent<RectTransform>();
|
||
if (buttonRect) startPos = buttonRect.anchoredPosition;
|
||
}
|
||
|
||
void OnEnable()
|
||
{
|
||
if (advanceAction != null)
|
||
{
|
||
advanceAction.action.performed += OnAdvance;
|
||
advanceAction.action.Enable();
|
||
}
|
||
}
|
||
|
||
void OnDisable()
|
||
{
|
||
if (advanceAction != null)
|
||
{
|
||
advanceAction.action.performed -= OnAdvance;
|
||
advanceAction.action.Disable();
|
||
}
|
||
}
|
||
|
||
void OnAdvance(InputAction.CallbackContext _)
|
||
{
|
||
if (!buttonRect) return;
|
||
|
||
// Only while textbox is “on”
|
||
if (textboxCanvas && textboxCanvas.alpha < visibleCutoff) return;
|
||
|
||
if (anim != null) StopCoroutine(anim);
|
||
anim = StartCoroutine(PressOnce());
|
||
}
|
||
|
||
IEnumerator PressOnce()
|
||
{
|
||
Vector2 downPos = startPos + new Vector2(0f, offsetY);
|
||
|
||
// down
|
||
float t = 0f;
|
||
while (t < 1f)
|
||
{
|
||
t += Time.unscaledDeltaTime / downTime;
|
||
float k = Mathf.SmoothStep(0f, 1f, Mathf.Clamp01(t));
|
||
buttonRect.anchoredPosition = Vector2.LerpUnclamped(startPos, downPos, k);
|
||
yield return null;
|
||
}
|
||
|
||
// up
|
||
t = 0f;
|
||
while (t < 1f)
|
||
{
|
||
t += Time.unscaledDeltaTime / upTime;
|
||
float k = Mathf.SmoothStep(0f, 1f, Mathf.Clamp01(t));
|
||
buttonRect.anchoredPosition = Vector2.LerpUnclamped(downPos, startPos, k);
|
||
yield return null;
|
||
}
|
||
|
||
anim = null;
|
||
}
|
||
|
||
// If you’d rather trigger from your textbox script:
|
||
public void Pulse()
|
||
{
|
||
if (anim != null) StopCoroutine(anim);
|
||
anim = StartCoroutine(PressOnce());
|
||
}
|
||
}
|