D
D
Daniel24112021-06-26 20:20:26
C++ / C#
Daniel2411, 2021-06-26 20:20:26

How to determine if the ButtonUI button is pressed in Unity or not (via a script)?

Good evening, how to track if the UI button is pressed or not without an Event Trigger? For example: there is a variable of type bool, when the button is pressed, then the variable = true.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GFX Data, 2021-06-26
@ShockWave2048

Class to hang up on the button.

using UnityEngine;
using UnityEngine.EventSystems;

public class BtnHoldDetect : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
    public bool isHold = false;

    public void OnPointerDown(PointerEventData pointerEventData)
    {
        isHold = true;
    }

    public void OnPointerUp(PointerEventData pointerEventData)
    {
        isHold = false;
    }
}
// https://docs.unity3d.com/2018.4/Documentation/ScriptReference/EventSystems.IPointerDownHandler.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question