playMaker

Author Topic: UI button on click "hold"  (Read 1483 times)

arminiuspp

  • Playmaker Newbie
  • *
  • Posts: 37
UI button on click "hold"
« on: May 02, 2023, 10:30:03 PM »
So I was trying to make an action where the player has to hold the button and when released it goes back to another state. I could not achieve this with UI on button click event
I used chatgpt here by the way and this is for mobile screen and am trying to do a runner game and am trying to imitate this from the Unity runner template where the player taps holds it while swiping left and right to move the player but I couldn't replicate that so I did this:

using UnityEngine;
using UnityEngine.EventSystems;
using HutongGames.PlayMaker;

namespace PlayMakerCustomActions
{
   [ActionCategory(ActionCategory.UI)]
   [UnityEngine.Tooltip("Sends an event after holding a UI button for a specified time.")]
   public class HoldUIButton : FsmStateAction, IPointerDownHandler, IPointerUpHandler
   {
      [RequiredField]
      [CheckForComponent(typeof(UnityEngine.UI.Button))]
      [UnityEngine.Tooltip("The GameObject that owns the Button component.")]
      public FsmOwnerDefault gameObject;

      [RequiredField]
      [UnityEngine.Tooltip("The event to send when the button is released.")]
      public FsmEventTarget eventTarget;

      [RequiredField]
      [UnityEngine.Tooltip("The event to send when the button is released.")]
      public FsmEvent eventName;

      [UnityEngine.Tooltip("How long the button must be held down before the event is sent.")]
      public FsmFloat holdTime;

      private bool isHeld;
      private float timer;

      public override void OnEnter()
      {
         isHeld = false;
         timer = 0f;
      }

      public void OnPointerDown(PointerEventData eventData)
      {
         isHeld = true;
      }

      public void OnPointerUp(PointerEventData eventData)
      {
         isHeld = false;
         timer = 0f;
      }

      public override void OnUpdate()
      {
         if (isHeld)
         {
            timer += Time.deltaTime;

            if (timer >= holdTime.Value)
            {
               SendEvent();
               Finish();
            }
         }
      }

      private void SendEvent()
      {
         GameObject target = Fsm.GetOwnerDefaultTarget(gameObject);

         if (target != null)
         {
            eventTarget.target = FsmEventTarget.EventTarget.GameObject;
            eventTarget.gameObject = new FsmOwnerDefault();
            eventTarget.gameObject.GameObject = target;
            Fsm fsm = target.GetComponent<Fsm>();
            fsm.Event(eventTarget, eventName);
         }
      }

      public override void Reset()
      {
         gameObject = null;
         eventTarget = null;
         eventName = null;
         holdTime = 1f;
      }
   }
}

---------------------
and this
_______________

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

namespace HutongGames.PlayMaker.Actions
{
   [ActionCategory(ActionCategory.UI)]
   [Tooltip("Sends an event when a UI Button is pressed and released.")]
   public class UiButtonPressedReleasedEvent : ComponentAction<UnityEngine.UI.Button>, IPointerDownHandler, IPointerUpHandler
   {
      [RequiredField]
      [CheckForComponent(typeof(UnityEngine.UI.Button))]
      [Tooltip("The GameObject with the UI Button component.")]
      public FsmOwnerDefault gameObject;

      [Tooltip("Where to send the pressed event.")]
      public FsmEventTarget pressedEventTarget;

      [Tooltip("Send this event when button is pressed.")]
      public FsmEvent buttonPressedEvent;

      [Tooltip("Where to send the released event.")]
      public FsmEventTarget releasedEventTarget;

      [Tooltip("Send this event when button is released.")]
      public FsmEvent buttonReleasedEvent;

      private bool buttonPressed;

      public override void Reset()
      {
         gameObject = null;
         pressedEventTarget = null;
         buttonPressedEvent = null;
         releasedEventTarget = null;
         buttonReleasedEvent = null;
         buttonPressed = false;
      }

      public override void OnEnter()
      {
         buttonPressed = false;

         var go = Fsm.GetOwnerDefaultTarget(gameObject);
         if (UpdateCache(go))
         {
            var button = cachedComponent;
            if (button == null)
            {
               LogError("Missing UI.Button on " + go.name);
               Finish();
               return;
            }

            button.onClick.AddListener(OnButtonDown);
         }
         else
         {
            LogError("Missing GameObject");
            Finish();
            return;
         }
      }

      public override void OnExit()
      {
         var go = Fsm.GetOwnerDefaultTarget(gameObject);
         if (UpdateCache(go))
         {
            var button = cachedComponent;
            if (button == null)
            {
               return;
            }

            button.onClick.RemoveListener(OnButtonDown);
         }
      }

      public void OnPointerDown(PointerEventData eventData)
      {
         buttonPressed = true;
      }

      public void OnPointerUp(PointerEventData eventData)
      {
         if (buttonPressed)
         {
            SendEvent(pressedEventTarget, buttonPressedEvent);
            buttonPressed = false;
         }
         else
         {
            SendEvent(releasedEventTarget, buttonReleasedEvent);
         }
      }

      private void OnButtonDown()
      {
         buttonPressed = true;
      }
   }
}

Captaincrud

  • Junior Playmaker
  • **
  • Posts: 72
Re: UI button on click "hold"
« Reply #1 on: May 02, 2023, 10:55:40 PM »
wouldn't it be easier to do on mouse down and and mouse up and make a int to count if you need to know how long its been down?

arminiuspp

  • Playmaker Newbie
  • *
  • Posts: 37
Re: UI button on click "hold"
« Reply #2 on: May 03, 2023, 02:06:27 AM »
Will it work for mobile? like when I touch the screen and move my touch across the screen, making it change direction?

arminiuspp

  • Playmaker Newbie
  • *
  • Posts: 37
Re: UI button on click "hold"
« Reply #3 on: May 03, 2023, 11:02:27 PM »
I still need help