How do I get (return) all the GUI inside(contained in) a GUI rect ?


If we have a large GUI box and then we drag a button into that box, how can we make the box know what the button is within the bounds of the box ?


I would try something like this:

When you release the element do a raycast and check if it hits the box (if you want to know it the whole button is in the box you can do 4 raycasts from 4 button corners and check if all of them hit the box). If the button is in the box you can either move it to become a child element of the box and/or add it to a list of contained elements for faster reference.

Since OnGUI elements like Box and Button use Rectangles, you can just use

if(yourRect.Contains(new Vector2(myButton.position.xMin, myButton.position.yMin))
|| yourRect.Contains(new Vector2(myButton.position.xMin, myButton.position.yMax))
|| yourRect.Contains(new Vector2(myButton.position.xMax, myButton.position.yMin))
|| yourRect.Contains(new Vector2(myButton.position.xMax, myButton.position.yMax))) {
     Debug.Log("myButton is completely inside yourRect.");

That would ensure the whole button is inside the Rect. You can of course also just check if the Rect contains your mouseposition if you are dragging the button with the mouse.