Search code examples
c#unity-game-engineunity3d-2dtools

Continuous input from eventTrigger OnPointerDown


Just like Input.GetKey() for keyboard is there any way to take continuous input from UI button event trigger OnPointerDown ? Because for movement controls with animation its giving very weird output? I have to click it again and again which I don't want to!

And if answer is manually tracking then how to do that ?


Solution

  • You can do this with the OnPointerDown and OnPointerUp functions then use a simple boolean variable to keep the states. You can then check of the press in the Update function.

    public class YourCode : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
    {
        bool pointerDown = false;
    
        public void OnPointerDown(PointerEventData eventData)
        {
            pointerDown = true;
        }
    
        public void OnPointerUp(PointerEventData eventData)
        {
            pointerDown = false;
        }
    
        void Update()
        {
            if (pointerDown)
            {
                //Your Code
            }
        }
    }