- 已编辑
Get wrong order about 2D Raycaster.
I set my scene with the camera having a "Physics 2D Raycaster" component and an "Orthographic" projection.
Then I have a cat of spine and a carpet of sprite, both were added with "Box Collider 2D" component and Event Trigger adding PointerClick callback.
And here comes the strange thing. When I set the spine and the sprite both in "Default" Sorting Layer. I will get the click callback of the cat first. But when I set them both to "Interaction" Sorting Layer(Created in sorting layers manually). I will get the click callback of the carpet first. This makes me confusing. Is this a bug? Or need I to do some extra settings to get the right click callback order with a custom sorting layer.
I add my test project as an attachment. Please help me figure it out. Thanks.
Thanks for sending your Unity reproduction project up front. Unfortunately Unity seems to not properly respect the Sorting Layer
[1] during raycasts between MeshRenderer
and SpriteRenderer
objects. It also seems to matter whether the BoxCollider2D
is attached to the Renderer
GameObject or to its parent, the Sorting Layer
of the renderer only seems to be respected properly when the BoxCollider2D
is at the renderer as well (otherwise it may count as the default sorting layer perhaps?). So we're afraid to say that we can't do anything about it, as it's in Unity's hand to handle the raycast. If you really need to use Sorting Layer
, we would recommend to either create dummy raycast receivers (e.g. empty SpriteRenderers
with a BoxCollider2D
) that forward any events to the intended receiver, or to change raycast handling altogether.
[1] In code it can be set via renderer.sortingLayerID = SortingLayer.NameToID(layerName);
.
Thanks, Harald. Finally, I add an empty Sprite Renderer on spine's parent with BoxCollider2D and it works well.
Glad to hear you've figured it out, thanks for getting back to us. 8)