I’ve been trying to make my own event trigger since I only need the OnPointerUp event. So I made a class that implements the IPointerUpHandler interface and inherits from UIBehaviour (also tried a normal MonoBehaviour), but my OnPointerUp method doesn’t get called…
My code:
public class SmartText : UIBehaviour, IPointerUpHandler {
public void OnPointerUp(PointerEventData eventData) {
Debug.Log(eventData.position);
}
}
I attached this to an object with also a Text script on it. What am I doing wrong?
@TeotiGraphix_1 I’m actually quite surprised adding the virtual keyword makes it work! Virtual means that the method can be overridden in a derived class using the override keyword. For example:
public class Animal {
public virtual void MakeSound(){
Debug.Log("I'm an animal!");
}
}
public class Cat {
public override void MakeSound(){
Debug.Log("Meow!");
}
}
The construct I used is used to implement interface members, specifically. Unfortunately I don’t work with C# enough to have a lot more knowledge on the subject that I can explain with any decent amount of certainty. I do know that by stating the interface name explicitly you can implement multiple interfaces that contains members with the same name. For example:
interface ITwitter {
void Post(string message);
}
interface IFacebook {
void Post(string message);
}
class SocialMedia : ITwitter, IFacebook {
void ITwitter.Post(string message){
//Twitter API stuff
}
void IFacebook.Post(string message){
//Facebook API stuff
}
}
SocialMedia sm = new SocialMedia();
((ITwitter)sm).Post("Hello world!"); //post to Twitter
((IFacebook)sm).Post("Hello world!"); //post to Facebook
Hope this explained things at least somewhat, though I can’t explain why your method worked as well.
For me nothing works. Making the method virtual doesn’t help, nor adding “IPointerUpHandler.”. It does however show in the inspector that my SmartText component intercepts the OnPointerUp event. It always shows this, regardless of whether or not I make the method virtual or add “IPointerUpHandler.” to it.
I also tried adding a button to it, just to see if my EventSystem object was working properly, and the button works perfectly.
Edit: Also tried it with an Image instead of text, doesn’t work either.
at 25:00, for his OnPointerClick() he has virtual in there and is implementing IPointerClickHandler.
Maybe sloppy code because he was in a hurry? But heh, I saw this yesterday and the memory led to my post today about virtual. But yeah, its kind of like the abstract word in Java. (It didn’t seem right to me).
But in his implementation of the ISubmitHandler, he just uses public void and no qualified interface name like you have!
@The-Oddler Are you using b18? I had some custom event handlers working in b17 that have stopped responding to pointer input, as well, and I believe they may have stopped working as soon as I updated to b18, but I still need to check my scene over a few more times to be sure.
EDIT
Also, if it inherits from UIBehaviour, I’m fairly certain it should be placed inside a Canvas, and you will need to ensure that you have a graphic raycaster, as well.
If you go the MonoBehaviour route, you will need to add a collider to the object you want to receive the events with, and add a 3d or 2d raycaster (depending on what type of collider you use) to one of the cameras in the scene that receives input.
Another thing that could be going wrong is that if another object receives the input first, like say, a collider that sits in front of the object your’e trying to click, your object won’t receive the proper input, so you may want to set up a separate layer and only have the raycaster cast along that particular layer.
@TeotiGraphix_1 Huh, well whaddya know! I honestly don’t know the rationale behind that to be honest. I might do a bit of digging tomorrow. If I end up finding anything I’ll let you know! =)
@The-Oddler Have you tried setting it up with the editor’s trigger system as well? I’m curious if it’s even receiving the event at this point. I’ve tried it myself, but I have a custom EventSystem set up atm, so no conclusive results there.
I just saw this thread by @vexe describing a problem (possible bug) with OnPointer___ events. Could you try changing public void OnPointerUp(PointerEventData eventData) to public void OnPointerUp(BaseEventData eventData) and see if that fixes anything?
@Senshi
I tried the thing with the Test() method and the event trigger. When I do this, my Test method gets called perfectly. The weird thing though, is that now my OnPointerUp also gets called in the SmartText class… I don’t have to add it to the event trigger. If I just add an empty EventTrigger my SmartText component gets the OnPointUp call…
So it seems the EventTrigger does some setup code or something that I don’t know about. Any ideas?
@The-Oddler D’ow! I never realized this, but it seems an EventTrigger Component is indeed needed for it to work! From the docs:
“Receives events from the EventSystem and calls regiostered functions for each event.”
Since I’ve been adding all my triggers through code, my elements already have one by default, so it never occured to me that it might be a prerequisite.
I just found this myself, or at least something very similar : I added a component to my object that implements IPointerClickHandler, and although OnPointerClick does get called when I click the object, the PointerEventData object it gets is not initialised properly and I can’t read it to find out what was clicked. Then I added an EventTrigger component to the object… and now the PointerEventData contains the right data!
I’m going to hazard a guess that there’s something happening here like ‘OnPointerUp doesn’t get called correctly unless the system was able to call OnPointerDown first’ or something… by attaching the EventTrigger component, which has an OnPointerDown handler, the system is ‘satisfied’ and fires things off correctly even though the EventTrigger itself does nothing.
I imagine the dev team would be highly appreciative if someone could make a little project demonstrating the problem and file a bug, then post the bug number here.
To test that hypothesis, I removed the EventTriggers to see what happens, and I am still seeing OnPointerDown and OnPointerClick both fired, but the PointerEventData does not have ‘selectedObject’ set. Add the EventTrigger back on the object, and it sets ‘selectedObject’ correctly. Hope that helps…