Hi all, so I was messing a bit with the event system and I noticed a couple of things:
If you want to have a handler for say, a pointer enter event for a button, your handler must take a BaseEventData as an argument as opposed to PointerEventData otherwise the functions list won’t detect it - So say you wanted to fetch the pointerId or anything related, you’d have to cast it down to a PointerEventData. Redundant, and not very intuitive IMHO.
I’m not quite sure how to receive right/middle mouse buttons. The OnPointerClick/Up/Down don’t seem to catch anything but left clicks.
@Tim-C OnClickHandler is just my custom event handler.
I think you miss-understood, I’m talking about the event system via EventTrigger.
Here’s a step by step:
1- Create a UI Button
2- Add EventTrigger component to it
3- Add any kind of pointing event to the event list (OnPointerUp/Down etc)
4- Write a MonoBehaviour with a handler to that pointer event that takes a PointerEventData argument (just like the OnClickHandler ^)
5- Try to add that handler to the EventTrigger’s event list, and it won’t pick it up i.e. It won’t find my handler from my MonoBehaviour.
6- The way to fix that is to change the handler’s argument to take a BaseEventData instead of PointerEventData.
So with the EventTrigger component we are currently keeping things very simple. With this component everything that is sent uses a BaseEventData. There are few reasons for this:
Dramatically increases code required to manage serialisation
We see EventTrigger as a helper class for prototyping (it’s slow as it will always receive all events)
The recommended way of doing what you want is adding the specific interface that you want to the Handler. That being said if you want to do forwarding like you describe it is pretty east to accomplish. Here is a ‘PointerEventTrigger’ that should work like you want:
using System;
using System.Collections.Generic;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine;
public enum PointerEventTriggerType
{
PointerEnter,
PointerExit,
PointerDown,
PointerUp,
PointerClick
}
[AddComponentMenu("Event/Pointer Event Trigger")]
public class PointerEventTrigger :
MonoBehaviour,
IPointerEnterHandler,
IPointerExitHandler,
IPointerDownHandler,
IPointerUpHandler,
IPointerClickHandler
{
[Serializable]
public class PointerTriggerEvent : UnityEvent<PointerEventData>
{
}
[Serializable]
public class Entry
{
public PointerEventTriggerType eventID = PointerEventTriggerType.PointerClick;
public PointerTriggerEvent callback = new PointerTriggerEvent ();
}
public List<Entry> delegates;
protected PointerEventTrigger()
{
}
private void Execute<T>(PointerEventTriggerType id, T eventData) where T : PointerEventData
{
if (delegates != null)
{
for (int i = 0, imax = delegates.Count; i < imax; ++i)
{
Entry ent = delegates[i];
if (ent.eventID == id && ent.callback != null)
ent.callback.Invoke (eventData);
}
}
}
public virtual void OnPointerEnter(PointerEventData eventData)
{
Execute (PointerEventTriggerType.PointerEnter, eventData);
}
public virtual void OnPointerExit(PointerEventData eventData)
{
Execute (PointerEventTriggerType.PointerExit, eventData);
}
public virtual void OnPointerDown(PointerEventData eventData)
{
Execute (PointerEventTriggerType.PointerDown, eventData);
}
public virtual void OnPointerUp(PointerEventData eventData)
{
Execute (PointerEventTriggerType.PointerUp, eventData);
}
public virtual void OnPointerClick(PointerEventData eventData)
{
Execute (PointerEventTriggerType.PointerClick, eventData);
}
}
Hey @Tim-C Thanks for directing Vexe and us to the right way
Though I am really confused on how to handle Keyboard input using the new EventSystem (StandaloneInputModule)…
For mouse input, I am implementing the interfaces I need like IBeginDragHandler, IPointerDownHandler etc… as you described and it went quite smooth.
But I have no clue how to handle keyboard events! Using Update to detect keyboard input (e.g. Input.GetKey and other variations ) is producing problems and requiring lots of maintenance for the goal I am trying to achieve. Especially when trying to do keyrepeat…
With the old GUI I used to implement keyboard events using OnButtonDown or OnButtonUp but as I transitioned to uGUI I got stuck with detecting keyboard input from Update().
My question is:
What interfaces should I implement or what should I do in order to detect keyboard buttons being pressed in a way similar to Old GUI’s OnButtonDown and OnButtomUp?