OnPointer events without Graphic

I’m attempting to do something very straightforward and simple, but after hours of searching I have yet to find a solution.

What I want to do is detect pointer events (such as OnPointerEnter) on an ‘empty’ UI object = one that only has a RectTransform, and no graphical component (such as Image, RawImage etc.)

I simply wish to detect whether the mouse has entered a particular area of the screen with I have defined with a simple object, a child of a Canvas, that has only the RectTransform, but no ‘visible’ component. I would imagine this is easy with the right knowhow, but I can’t find adequate documentation.

Another way to phrase this question: what methods/interfaces need to be implemented in a component for theOnPointer events to be called?

You need something for a RayCaster to interact with. That leaves you a few options.

  • Use a GraphicsRaycaster. This requires a Graphic. An image with the alpha set to zero works fine
  • Use a PhysicsRaycaster. This requires a Collider. It could be messy mixing UI and 3D physics
  • Use a Physics2DRaycaster. This requires a Collider2D. Again it could be messy
  • Implement your own Raycaster that can interact with an arbitrary Rect

I appreciate your reply, but this doesn’t really answer my question.

Explicity, what methods/interfaces need to be implemented? For example, if I add the following trivial component, the the Raycast() and IsRaycastLocationValid() methods are never called.

I am quite happy to implement my own Raycaster, as you suggest, but what methods does the Eventsystem require to be implemented?

public class CanvasSpace: MonoBehaviour,ICanvasRaycastFilter {

public void Start() {

Debug.Log (“Start()”);
}

public bool Raycast(Vector2 sp,Camera eventCamera) {

Debug.Log (“Raycast(”+sp+“,”+eventCamera);

return false;
}

public bool IsRaycastLocationValid(Vector2 sp,Camera eventCamera) {

Debug.Log (“IsRaycastLocationValid()”);

return false;
}
}

Start from here and work your way up.

https://bitbucket.org/Unity-Technologies/ui/src/ccb946ecc23815d1a7099aee0ed77b0cde7ff278/UnityEngine.UI/UI/Core/GraphicRaycaster.cs?at=5.1

Sorry, but again you are not really answering my question.

I don’t need to implement a RayCaster. The Canvas has a GraphicRaycaster that interacts with the components that are a child of the Canvas. I need to write a component that interacts with the Raycaster already provided; in the same way that the existing graphical components (e.g. Image) already do.

If you trace through the hierachy of Image (for example) it doesn’t extend GraphicRaycaster or BaseRaycaster. But it interacts in the right way - which is what I need to do.

So just inherit straight from graphic, or use an image with a zero alpha. The custom raycaster would be the last resort, custom is normally expensive to maintain.

Yes - that’s the solution I’m using at the moment, but I consider it pretty dirty to add a graphic with alpha = 0 just to do the collision detection.

If I simply had the knowledge of what methods/interfaces are required, I would expect it to be trivial to write the required code. Which is why I posted in the first place!

Also, it is always nice to have a better understanding of what’s happening ‘under the hood’ rather than hack something together that works.

If you take a look at the GraphicRaycasteryou’ll see that it in the Raycast function its gets a list of graphics from GraphicRegistry.GetGraphicsForCanvas(canvas);. It is these graphics that are considered for a hit via the GraphicRaycaster. To add something to this list is needs to be a Graphic.

If you’re not looking to write your own Raycaster (or extending the GraphicRaycaster to look at other non-Graphic elements) then the zero alpha approach is likely the best option. It should have no rendering hit as the element will be ignored.