Placing prefabs on other objects without raycasts?

Whenever building tools to place prefabs quickly in your scene, it's common to use raycasting on some collider to know where the prefab should be created. For example in 3D, we raycast on a terrain to place the prefab directly on top of the terrain.

I wanted to use the same concept for 2D: I double click with my mouse and it places the prefab on a pre-existing prefab if there are any, if not it goes to Z:0.

The problem I have is that I don't want my 2d prefabs to have a 2d colliders as these colliders are not used in my game. So how can I detect if there is a sprite where I'm clicking on without using raycasts (specifically without using colliders on the sprites in my scene).

Without implementing your own mesh or shader raycasting, the best in-built method to pick objects is the appropriately named HandleUtility.PickGameObject.

Click to expand...

Since I would like to know about multiple gameobjects in the cursor's direction, as if a line was cast from the mouse cursor towards infinity in the direction of: from the sceneView camera to the cursor. Is there something else I can use that achieves this?

If you still want to avoid implementing your own mesh raycasting system, you could add temporary colliders with hideFlags to your objects and raycast against them.

However for maximum flexibility you'll probably want to be able to raycast against meshes. I've actually implemented this for my own projects. It's not actually that difficult, mainly just porting to C# some algorithms made by way smarter people than I.