- Home /
Question by
smirlianos · Aug 30, 2014 at 11:26 AM ·
guiarrayrtsaccess
Select multiple objects
Hello,
I wrote this script that draws a box on the screen with the mouse, like RTS games. How can I select every object that is inside this box?
In other words, how can I access these objects and change their variables?
#pragma strict
var MenuSkin : GUISkin;
private var clickPosX : float;
private var clickPosY : float;
private var CurPosX : float;
private var CurPosY : float;
private var drawing : boolean;
function Update() {
if(Input.GetMouseButtonDown(0))
{
drawing = true;
clickPosX = Input.mousePosition.x;
clickPosY = Input.mousePosition.y;
}
if(Input.GetMouseButton(0))
{
CurPosX = Input.mousePosition.x;
CurPosY = Input.mousePosition.y;
}
if(Input.GetMouseButtonUp(0))
{
drawing = false;
}
}
function OnGUI() {
GUI.skin = MenuSkin;
if(drawing && clickPosX != CurPosX)
{
GUI.Box (Rect (clickPosX, Screen.height - clickPosY, CurPosX - clickPosX, Screen.height - CurPosY - (Screen.height - clickPosY)), "");
}
}
Comment
Answer by superdudeman · Aug 30, 2014 at 12:11 PM
Assuming the objects have colliders on them, a CapsuleCastAll or SphereCastAll that covers the selected area should do the trick! Then loop through the resulting array to find what was hit.
Hmm... I think this method will check areas outside the rectangle as well
You're right. It's a good question, now I want to know how to do it! I hope someone gives a good answer. Edit: Looks like it has answered a few times before.