ī) Customize the sorting order within Unity. One hacky workaround I have used is placing a transparent rectangle behind all the UI to serve as pointer click receiver, and using pointer events received by that instead of querying raw mouse input API. Most annoying part with this is ensuring that clicking on overlay canvas doesn't result in clicks on non canvas objects (whole Raycaster/EventSystem/InputModule setup gives that for free). Call Physics2D.OverlapPointAll yourself and choose correct result based on your own rules. Due to the same reason it will also not take into account sorting layer for noncanvas TextMeshPro text component or any other component which supports sorting layer/order but isn't one of the 3.Īssuming with this knowledge you still can't reorganize the structure of your game to get desired clicking priority I see two major options:Ī) Don't use IPointerDown. Even if you attached physics collider to sorting group instead of sprites within sorting group it would still not work since Physics2DRaycaster only takes sorting layer and sortingOrder only from SpriteRenderer, TilemapRenderer, SpriteShapeRenderer. SortingGroups are also likely to be used in combination with sorting axis (which isn't taken into account). When using SortingGroup result will likely be mess unless you have set sorting layers within sorting group to match with the global sorting order in which case you wouldn't need sorting groups. While these sorting conditions are somewhat similar to conditions for drawing order sorting there are few differences. index -> the order returned by GetRayIntersectionAll, only used if everything else is identical.Will differ between objects only based on 2D distance from camera center Where m_hits.point is Vector2d so the Z position is thrown away. distance -> seems to be somewhat useless since it uses.depth -> doesn't seem to be set by Physics2DRaycaster.raycast module (for stuff like overlay canvas stuff being on cameras and the order between multiple cameras).The ordering established by RaycastComparer is based on many factors: Due to this sorting order returned by Physics2D.GetRayIntersectionAll almost doesn't matter. The order for Physics2D.GetRayIntersectionAll isn't clearly defined in docs although since it is a 3D ray I would expect it to be sorted by Z, just like OverlapPoint, but that doesn't really matter due to stuff that happens afterwards.Īfter gathering all the raycast hits from all the raycaster modules Event system sorts them using RaycastComparer. Physics2DRaycaster is using reflections to call GetRayIntersectionAll instead of doing it directly, but that shouldn't change anything. So the overall Call sequnce is something like this: InputModule -> EventSystem.RaycastAll -> Physics2DRaycaster.Raycast -> Physics2D.GetRayIntersectionAll. If you want to be sure you can view the actual latest code in Library/PackageCache/ folder of your project. The links I will be giving will be to slightly older version in the public repository, but I doubt things have changed too much. Luckily for you both of them are within uGUI for which all the source code is available. The priority for OverlapPoint is described in docs, supposedly it prefers lowest Z value after filtering out by other conditions.īack to IPointerDown and Physics2DRaycaster. Not Physiscs2D "raycasting" methods which there are there are 3x3(normal, All, NonAlloc) (OverlapPoint, Raycast, GetRayIntersection), don't use Physics2D.Raycast with 0 length for pointer checks, that's for rays within 2d plane, not for rays coming from camera, use OverlapPoint or maybe GetRayIntersection instead. I assume you are specifically asking how IPointerDown and Physics2DRaycaster works. Given your setup, it may be necessary for you to do a RaycastAll and decide what to hit, or else an OverlapPoint I forget where the pointer interfaces call from but I assume it's perhaps in the docs, but a good bet might be Update(). There may be more than one solution to try.Īnd SortingGroups can also be extremely helpful in certain circumstances:Īlso, generally physics should be done in FixedUpdate(), although I suppose you could call Raycast anywhere. search online for the combination of things you are doing and how to to achieve what you want. If you find that you need to mix and match items using these different ways of rendering, and have them appear in ways they are not initially designed for, you need to: UI Canvas Renderers draw in linear transform sequence, like a stack of papers SpriteRenderers draw according to their Sorting Layer and Sorting Depth propertiesģ. The default 3D Renderers draw stuff according to Z depth - distance from camera.Ģ. Three (3) ways that Unity draws / stacks / sorts / layers / overlays stuff:ġ. Click to expand.Sprites may all be the same distance.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |