I want to make an easter egg where, if you’ve been holding on a certain button long enough, you would have unlocked something.
How do I achieve that considering that a regular button only has
1 Answer
1Something like this should get you started
using UnityEngine;
using UnityEngine.EventSystems;
public class ButtonPressTimer: MonoBehaviour, IPointerEnterHandler, IPointerExitHandler , IPointerDownHandler, IPointerUpHandler
{
private bool isInside;
private float timer;
//Detect when Cursor enters the GameObject
public void OnPointerEnter(PointerEventData pointerEventData)
{
isInside = true;
}
//Detect when Cursor leaves the GameObject
public void OnPointerExit(PointerEventData pointerEventData)
{
isInside = false;
}
public void OnPointerDown(PointerEventData pointerEventData)
{
timer = Time.time;
}
//Detect if clicks are no longer registering
public void OnPointerUp(PointerEventData pointerEventData)
{
if(!isInside)
return;
float pressingTime = Time.time - timer;
Debug.Log("Pressed for " + pressingTime + "s");
}
}
Thank you so much. I actually ended up using the button as the base class
– defapyt@defapyt glad it helped , i also use this kind of components to stack effects of buttons as well (for example one component for hover SFX , one for for button animation , etc ...) , maybe that idea can help you make your buttons more composable/editable
– PixelEyesDev