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;
}
}
}