Hi everyone,
I’m working on a project in Unity, and I want to capture button interaction while another button is being pressed. I can see hover being triggered on each button… but that stops while I’m pressing and holding down on another button, and no more events come in while I drag over them.
I’ve don’t this with Unity built in UI, I have it working in a different app.
My goal is to:
Click on a button.
Drag across other buttons while holding the initial press.
Capture hover events for each button I drag over.
Perform an action based on the final button I release on.
Here’s what I’ve tried so far:
using Nova;
using UnityEngine;
public class SimpleButtonDragHandler : MonoBehaviour
{
private UIBlock initialButton;
private UIBlock lastHoveredButton;
private bool isDragging = false;
public int thisHr;
private void OnEnable()
{
GetComponent<UIBlock>().AddGestureHandler<Gesture.OnPress>(OnPress);
GetComponent<UIBlock>().AddGestureHandler<Gesture.OnDrag>(OnDrag);
GetComponent<UIBlock>().AddGestureHandler<Gesture.OnHover>(OnHover);
GetComponent<UIBlock>().AddGestureHandler<Gesture.OnRelease>(OnRelease);
}
private void OnDisable()
{
GetComponent<UIBlock>().RemoveGestureHandler<Gesture.OnPress>(OnPress);
GetComponent<UIBlock>().RemoveGestureHandler<Gesture.OnDrag>(OnDrag);
GetComponent<UIBlock>().RemoveGestureHandler<Gesture.OnHover>(OnHover);
GetComponent<UIBlock>().RemoveGestureHandler<Gesture.OnRelease>(OnRelease);
}
private void OnPress(Gesture.OnPress evt)
{
Debug.Log("Pressed: " + thisHr);
evt.Consume();
if (evt.Target.TryGetComponent<UIBlock>(out UIBlock button))
{
initialButton = button;
isDragging = true;
Debug.Log("Pressed button: " + initialButton.name);
}
}
private void OnDrag(Gesture.OnDrag evt)
{
if (isDragging)
{
Debug.Log("Dragging: " + thisHr);
evt.Consume();
if (evt.Target.TryGetComponent<UIBlock>(out UIBlock button) && button != lastHoveredButton)
{
lastHoveredButton = button;
Debug.Log("Dragging over button: " + button.name);
}
}
}
private void OnHover(Gesture.OnHover evt)
{
Debug.Log("Hovering: " + thisHr);
evt.Consume();
if (isDragging)
{
Debug.Log("Hover event detected during drag.");
if (evt.Target.TryGetComponent<UIBlock>(out UIBlock button) && button != initialButton && button != lastHoveredButton)
{
lastHoveredButton = button;
Debug.Log("Hovered over button: " + button.name);
}
}
}
private void OnRelease(Gesture.OnRelease evt)
{
Debug.Log("Released: " + thisHr);
evt.Consume();
if (isDragging)
{
isDragging = false;
if (lastHoveredButton != null)
{
Debug.Log("Released on button: " + lastHoveredButton.name);
}
}
}
}
Scene Setup:
Each button has both UIBlock2D and Interactable components.
The script is attached to each button, with unique identifiers set in the thisHr field for debugging.
Issue:
Only the OnPress event logs appear, and the OnDrag and OnHover events are not triggering during the drag.
Request:
Can anyone provide insights or suggestions on how to correctly capture and handle hover and drag events in Nova UI during a button press? Any help or examples would be greatly appreciated!
Thanks in advance!