multiple object selection

How do I create a way for players to select multiple objects through a square selection. For instance, I want to use Input.mousePosition to get the x,y of the mouse on the screen then of course Input.GetMouseButtonDown to know if element 0 (left mouse button) is down, while this is down, draw a square and stretch it as I have EventType.MouseDrag being true, so while it drags, it creates a larger 2d square on the screen, now the depth from camera would be static so that not all elements in an endless depth are choosen, so say from the camera out to 100meters would be the max depth. Now, ever game object that has a tag that I choose would be selectable and highlights with a circle around it. When I have an EventType.MouseUp, my array of gameobjects would have all of the selected game objects in that array. I can't find anything in the docs for drawing a box on the screen in 2d like I am mentioning, I thought about using a Ray but that is a specific point, using Camera.main.ScreenPointToRay only lets me know if a ray has hit a specific item, this will only be good for single object selection, not multiple.

If you want this to be exact then it may be a lot of work. But it may be fairly simple to get an effect which is very close.

First have a look at the answers to the question, how to tell if a GameObject is seen by a camera. That should give you some ideas.

In particular, the suggestion about using Camera.WorldToViewportPoint may be the best approach. Instead of testing the object against the full viewport, which has coordinates (0,0) to (1,1), you'd be checking against the smaller rectangle that is the area selected with the mouse.

Easiest just to check a single point of each object, probably its center. But if this isn't precise enough you'll have to test more points of each object, and if the objects are complex and rotated arbitrarily it could get tricky. But for most purposes testing just the center point, or points representing the outermost corners of the object, will be good enough.

Edit: In the comments you say "I want to limit the selection to a cubed area". If you really want to do it this way the true area isn't a cube, but rather a truncated pyramid called a frustum. (For details, see "viewing frustum".)

If you were to calculate the 6 planes yourself, you could then use the function GeometricUtility.TestPlanesAABB as follows: You can get the camera's frustum using the function GeometricUtility.CalculateFrustumPlanes. Two of the planes you want are the near and far planes of the camera. The remaining planes you would have to calculate by projecting the four edges of the selected rectangle from the screen into the scene. (All you need are three points to construct each Plane: two adjacent corners of the selected rect, and a point on the ray you get by projecting one of those rays into the scene.)

This is all very doable, particularly if you already understand the geometry, but it's much more involved than my first suggestion. And both ways require you to loop through all the objects and test them. So I think projecting one or more points to the viewport for each object is a simpler solution. And if you test all the points that define the object's bounding volume instead of a single point, it'll be pretty good.

for drawing a square you should use a texute and scale it or just create a texture procidurally and then draw it. if you want to see if some gameobject is in selection area. you can use GameObject.FindGameobjectsWithTag function to find all of the tagged gameobjects and then compare their transform.position and see if they are in the selected area or not. also you can create a box collider in the selection area and then define a OnTriggerEnter for those tagged gameobjects that is like this.

void OnTriggerEnter (Collider other)
{
if (other.GameObject.Name = "selectionarea")
{
objlist.add (this.gameobject);
}
}

the code is untested and i wrote it here. you should have a static list that has the type in mouse up you can iterate in list with foreach and then get all gameobjects and clear the list in next Mouse down event.

Hey guys. I’m trying to do something similar but with a 2d line instead of a 3d box.
On mouse down i want to collect the objects found underneath the cursor and compare them on mouse up.

I’m currently using a raycast from the main camera with XY coordinates from the mouse to select individual objects on mouse up, but want to be able to select multiple objects in a single mouse down/up.

Any suggestions on how to do this?

I know, this is an old question, but I had the same problem and I solved it by using colliders.

My selection cube was an empty gameobject with a collider as trigger. The objects that I wanted to select had also colliders.
I took the 2D Mouse position, transformed it into world coordinates an sized my cube accordingly. The cube gave me the triggerenter events so I knew when an object was touched by the selection cube.