using UnityEngine.EventSystems;
using UnityEngine.Scripting;
using UnityEngine.UIElements;
public class BetterButton : Button
{
#region UXML
[Preserve]
public new class UxmlFactory : UxmlFactory<BetterButton, UxmlTraits>
{
}
[Preserve]
public new class UxmlTraits : VisualElement.UxmlTraits
{
}
#endregion
public UnityEngine.Events.UnityEvent onRightClick = new UnityEngine.Events.UnityEvent();
public UnityEngine.Events.UnityEvent onLeftClick = new UnityEngine.Events.UnityEvent();
public UnityEngine.Events.UnityEvent onMiddleClick = new UnityEngine.Events.UnityEvent();
public override void OnPointerClick(PointerEventData eventData)
{
if (eventData.button == PointerEventData.InputButton.Left)
{
// Invoke the left click event
base.OnPointerClick(eventData);
//onLeftClick.Invoke();
}
else if (eventData.button == PointerEventData.InputButton.Right)
{
// Invoke the right click event
onRightClick.Invoke();
}
else if (eventData.button == PointerEventData.InputButton.Middle)
{
// Invoke the right click event
onMiddleClick.Invoke();
}
}
}
but it says " âOnPointerClick(PointerEventData)â: no suitable method found to override"
I know this must be a simple fix but ive been racking my brain for days now
It looks like you have mixed elements from UGUI and UI Toolkit.
The example you are linking to is for UGUI, UI Toolkit is a different system and the Button class is different. It does not have a OnPointerClick method to override.
Instead, you donât need to create a new button class, just use a normal Button and register for the mouse-down event:
Okay that works for the target but what about the button (left, right or middle mouse button) I still canât access the mousedownevent.button property
Thank you that works, I had the type as EventData not MouseDownEvent.
Although this approach triggers when the button is âpressedâ not âclickedâ is there a simple way to work like the clickable.clicked event where it requires a MouseDownEvent and a mouseupevent on the same object? Or would I have to duplicate this code and have global flags to check if an object had both events triggered?
This is why I wanted to make my own custom button so this functionality was baked in
Iâve tried that too but nothing in the documentation says how to get clickable to call a different function depending on the button pressed, a right click should call func1 and a left click should call func2 ect
Hello there, I just wanted to do sth similar, and found the workflow with adding new activators on another thread. This seems to only work half way though.
Adding the activators caused my button to react visually (with a color transition added in my uss-style)
But the ClickEvent is never propagated to the callback thatâs registered on the button.
_identifierLabelButton.clickable.activators.Add(new ManipulatorActivationFilter() {button = MouseButton.LeftMouse, modifiers = EventModifiers.None});
_identifierLabelButton.clickable.activators.Add(new ManipulatorActivationFilter() {button = MouseButton.LeftMouse, modifiers = EventModifiers.Alt});
_identifierLabelButton.clickable.activators.Add(new ManipulatorActivationFilter() {button = MouseButton.RightMouse, modifiers = EventModifiers.None});
_identifierLabelButton.RegisterCallback<ClickEvent>(LabelButtonClickedHandler);
//...
private void LabelButtonClickedHandler(ClickEvent evt)
{
Log.Info("C"); //<- never printed on right click... But works for left-click & left-click + alt
switch (evt.button)
//...
UnityVersion: 2023.2.20f1
Unity UI Version: 2.0.0
Update:
Assigning a new Clickable to the button with a callback directly being set propagates right clicks, but as you canât cast BaseEvent to ClickEvent (Exception being thrown) and the BaseEvent not delivering any context about what modifier / button combination was being used does make that pretty much useless, as you canât assign multiple âclickablesâ with different callbacks based on their activations. I donât see any way to have one (ui-)button handling different (mouse-)buttons atm.
Correction: I simply overlooked the âAdd Clickable as Manipulatorâ part. But one thing still stands, you only get an EventBase. How do I know which modifier was pressed along my mouse-button, do I really need to register a extra callback for each combination of mouse-button/modifier?!
Why doesnât the default btn.RegisterCallback callback is invoked for additional activators with proper context-info, it should be able to detect right/middle clicks alongside modifiersâŚ
What is the button-Index of ClickEvent for when thereâs no easy way to use it with other buttons than left one? Do I need to construct the click event myself somehow with a mix of UITK / IMGUI code? This is so frustratingly chaotic (+verbose)âŚ
public void InitedPage(VisualElement root)
{
PropertyEx.Panel.RegisterCallback<ClickEvent>(OnPanelClick);
}
//
private void OnPanelClick(ClickEvent evt)
{
Vector2 position = PositionTransfer(evt.position);
switch (evt.button)
{
case 0:
OnPanelClickLeft(position.x, position.y);
break;
case 1:
OnPanelClickRight(position.x, position.y);
break;
case 2:
OnPanelClickMiddle(position.x, position.y);
break;
}
}
Why canât this approach listen for middle and right mouse button events? This is obviously unreasonable, as the click event isnât triggered at all when the middle or right mouse buttons are clicked.