Closed ViktorijaSml closed 8 months ago
Evo vam skripta za detekciju klikova (Double click / long press / wasPressed / wasRelesed) Također sadži UnityEvent što znači da možete u sve to dodati koje klase, objekti itd će se pokretati kad se pokrene neki od tih evenata.
NPR:
public UnityEvent wasPressedEvent, wasReleasedEvent, longPressEvent, wasDoublePressedEvent;
private bool isPressed = false;
private float pressTime = 0f, longPressDuration = 1f, doublePressDelay = 0.3f;
void Start()
{
Button button = GetComponent<Button>();
if (button != null)
{
button.onClick.AddListener(OnButtonClicked);
}
else
{
Debug.LogError("Button component not found on GameObject.");
}
}
void OnButtonClicked()
{
if (!isPressed)
{
isPressed = true;
pressTime = Time.time;
wasPressedEvent.Invoke();
}
else if (Time.time - pressTime < doublePressDelay)
{
wasDoublePressedEvent.Invoke();
isPressed = false;
}
}
void Update()
{
if (isPressed)
{
// Do something
}
}
void OnButtonReleased()
{
isPressed = false;
wasReleasedEvent.Invoke();
if (Time.time - pressTime > longPressDuration)
{
longPressEvent.Invoke();
}
}