Image raycast without a color fill?

Is there a way to have an image as a raycast, but without having any fill? Currently, I just have the image set to white with a 1 opacity, but it seems kinda hacky. I would assume there would be an option to have an area be a raycast target without using any graphical elements.

“Is there a way to have an image as a raycast, but without having any fill?”

You mean can you have a raycast target that isn’t visible?

UI events work just fine with transparent Images.

“I would assume there would be an option to have an area be a raycast target without using any graphical elements.”

One way to figure out if you are over UI element, is to use RectangleContainsScreenPoint:

If you need to check if your cursor is over UI object (GameObject = UI object…) use this:
EventSystem.current.IsPointerOverGameObject()

“I would assume there would be an option to have an area be a raycast target without using any graphical elements.”

One way is to create an empty UI element which still would work with UnityEngine.EventSystems events. Sorry, I can’t remember the original author (who came up with this idea) but you can probably find similar examples by googling. This would create an empty UI element without vertices, so no alpha / overdraw AFAIK:

public class EmptyGraphic : Graphic, IPointerClickHandler
{
    // creates empty UI mesh
    protected override void OnPopulateMesh(VertexHelper vh) => vh.Clear();

    // test
    public void OnPointerClick(PointerEventData eventData) => Debug.Log("Click!");
}

So you would attach this kind of script instead of Image to your UI GameObject…

1 Like

Unity 2017 Game Optimization - Second Edition
In this book, it is said that you can create an empty Text gameObject to receive raycast. Of course I don’t think this is a good way because the reference font will be packaged when building the Asset Bundle, so you need to be very careful.

Use this to determine if the cursor is over a Graphics element in the Scene. It accepts the mouse pointer event data as a parameter. Make sure you have an EventSystem in your hierarchy. If you don’t, go to Create >UI >Event System. Some common uses of this include: setting up your own custom UI system; telling when you hover over Text or Images which aren’t automatically selectable; UI click and drag operations; and many more.

@kuailemario
“In this book, it is said that you can create an empty Text gameObject to receive raycast.”

That sounds a bit like they were trying to do what the OnPopulateMesh trick I mentioned does… so I guess it would be better to simply create a script instead than use a hack?

@payalzariya07

Graphic raycaster alone won’t do anything… you’ll need uGUI UI elements to raycast against. Can you clarify what are you trying to say with that quote from the docs?