7
\$\begingroup\$

I'm making a small game for the first time and enjoying it thus far. The only thing that keeps bothering me is that I can't find a human explanation of what the graphics raycaster does. What's the blocking mask, what are the blocking objects etc? Should I mask the things I want clicked or the things I don't want clicked?

As an example I have the following screenshot: http://i.imgur.com/6nkVY7b.png

The overlayCanvas has the pinkish square on the top in it and the button "Button" on the pink square. Nonetheless, the whole bloody canvas is clickable instead of just the button and the pink square. Which means that all interaction seized because the canvas is on a "topLayer" which has its own camera.

The Square+button in the overlayCanvas are on "topLayer" aswell. But I just want the button and the pink square(and what's in it) clickable because the game is relying on clicks on the background image.

\$\endgroup\$
16
\$\begingroup\$

What is a raycast? What are blocking objects and blocking mask?
A ray is a mathematical device that starts at an origin point and continues on in a specific direction forever. With a raycast you're casting a ray, cast being used like the word throw. It's like if you threw a rock and it continued on in that direction forever, it wouldn't stop until it hit something. You're interested as to whether it hit an object and what that object was.

Blocking objects would be the objects that you specifically define your raycast to be able to hit.
A blocking mask allows you to, instead of passing tons of blocking objects, define the layers in Unity you want your raycast to be able to hit in the form of a bitmask. The bitmask is coded so each bit represents a layer. If your bitmask was 0000000000000101 represented as (1 | 1<<2) or 5 then your raycast will only be blocked by layers 1 and 3 and can therefore only hit objects in those layers.

If we look at Unity's Raycasters documentation, we can see there are three different kind of raycasters

Graphic Raycaster - Used for UI elements, lives on a Canvas and. searches within the canvas

Physics 2D Raycaster - Used for 2D physics elements.

Physics Raycaster - Used for 3D physics elements.

Accordingly to Andy Borrell in Oculus blog

the GraphicRaycaster (for Canvases) and the PhysicsRaycaster (for physics objects).

The one we're interested here is Graphic Raycaster and accordingly to Unity

The Graphic Raycaster is used to raycast against a Canvas. The Raycaster looks at all Graphics on the canvas and determines if any of them have been hit.

Graphic Raycaster and Unity's Event System
Unity's GraphicRaycaster was introduced in version 4.6 along with a bunch of other stuff that's all part of Unity's new events system. For that reason, the GraphicRaycaster was originally meant to be used through this system (same with the Physics Raycaster and its 2D counterpart, which are all different from Physics.Raycast which is meant for generic raycasts not necessarily originating from UI events). There are two options for the use of this component in Unity.


If you want to go the full on Unity way using Unity's EventSystem, you're going to need to place one in your scene. You can find this under GameObject > UI > Event System. From here you have a few methods of capturing the event and using it to your will.

  • Use event triggers (found in Component > Event > Event Trigger and use the editor to create all your connections.
  • If you would like to avoid the editor, your MonoBehaviour can implement a given interface depending on the event your want to capture. These are the UnityEngine.EventSystems.I...Handler interfaces like the IBeginDragHandler interface

This video shows the specific implementations of each method.
For more on the event system, triggers, and how they all work together this video might help as well.

You can also use it directly by calling the GraphicRaycasters Raycast function itself instead of creating code as a response to an event. For this we need the following snippet of code where we create a PointerEventData object to send to the Raycast function.

//Code to be place in a MonoBehaviour with a GraphicRaycaster component
GraphicRaycaster gr = this.GetComponent<GraphicRaycaster>();
//Create the PointerEventData with null for the EventSystem
PointerEventData ped = new PointerEventData(null);
//Set required parameters, in this case, mouse position
ped.position = Input.mousePosition;
//Create list to receive all results
List<RaycastResult> results = new List<RaycastResult>();
//Raycast it
gr.Raycast(ped, results);

results will now contain all of your hit canvas objects. This is pretty much the same way that PointerInputModule does raycasting with EventSystem.RaycastAll in Unity (except with more caching).

Your specific problem with the button.
Try setting the OverlayCanvas to not be on a layer. I think the fact that it's on a layer in the first place might be breaking things because it looks like the default it for a Graphic Caster is to be blocked by "Everything" which I'm guessing your BGCanvas has it's blocking mask set to.

\$\endgroup\$

protected by Community Mar 25 at 15:16

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.