- Home /
How do I get (return) all the GUI inside(contained in) a GUI rect ?
Hi,
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 ?
Answer by Cherno · Mar 06, 2015 at 03:15 PM
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.
Answer by AdamAz · Mar 06, 2015 at 11:25 AM
Hi
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.
that seems like a rather taxing process . we can actually raycast from inside OnGUI and hit GUI elements ?
I was under the impression that you are using normal game objects, sorry about that. Not sure if it is possible to do this with OnGUI elements.
no problem. I think that it is possible , when i find the answer ,ironically; I will post it here myself.