2
votes

I created a new scene to test 'Ignore Raycast' layer functionality in unity 2018.2.6f1. The scene is just a UI button with a log script attached. When I change the button layer to 'Ignore Raycast', the button still works. Do all the buttons behave this way until we manually turn off 'Raycast Target' & 'Interactable' checkboxes in Image and Button components?

I want to know the dependency between the 3 options 'Ignore Raycast', 'Raycast Target' & 'Interactable'.

Button inspector

1

1 Answers

3
votes

Interactable is easy. If the Graphic Raycaster finds an event (mouse over, click, mouse off, etc) on the button, does it react to those events? If Interactable is true, then it will.

The other two need some explanation.

Layers are used to handle collision with Unity's Physics raycasting.

The Ignore Raycast layer is just like any other layer, but is intended to be typically ignored by Physics raycasts. It's up to the code that creates the Physics raycast to decide if colliders on objects in the Ignore Raycast layer are actually ignored. For instance, if you create a Physics raycast with a mask of Physics.IgnoreRaycastLayer, then it will only collide with colliders of objects in the Ignore Raycast layer.

However, again, that setting deals with Physics raycasts

Raycast Target deals with the Unity Graphic Raycaster, which is how Unity handles interaction with UI elements (by default). The Graphic Raycaster is totally independent from Physics raycast, and how it masks with layers. Instead of layers, Graphic Raycaster allows for control over collision in two ways.

The first of these ways is something called UI.GraphicRaycaster.BlockingObjects which makes the raycast stop when it hits 2d objects or 3d objects, both, or neither.

The second of these ways is where Ignore Raycast comes into play.

The Raycast Target of a ui element determines if raycasts that collide with that element stop at that element, so things below it don't also get interacted with. If Raycast Target is enabled, then when you mouse over or click a button or whatever, it will respond however it is configured to do so, but anything below the button won't have that interaction event.

For example, suppose you want to create a popup that can be interacted with in a way where they aren't clicking on buttons underneath the popup accidentally. You would want to enable Raycast Target on the popup and its buttons and so on in that case.

TL;DR:

  • Interactable determines if the button is enabled or disabled on its own.
  • Raycast Target determines if the button blocks buttons covered up by it from doing things.
  • Ignore Raycast determines if Physics raycasts ignore colliders attached to it by default.