Created
January 29, 2013 02:28
-
-
Save benloong/4661195 to your computer and use it in GitHub Desktop.
RTS Volume selection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
class Selection { | |
List<Unit> static PickUnitsInRect(float left, float top, float right, float bottom, Camera camera = null) { | |
List<Unit> pickedUnits = new List<Unit>(); | |
if(camera == null) camera = Camera.main; | |
Ray bottomLeft = camera.ScreenPointToRay(new Vector3(left, bottom)); | |
Ray topLeft = camera.ScreenPointToRay(new Vector3(left, top)); | |
Ray topRight = camera.ScreenPointToRay(new Vector3(right, top)); | |
Ray bottomRight = camera.ScreenPointToRay(new Vector3(right, bottom)); | |
Plane[] planes = new Plane[5]; | |
planes[0].Set3Points(topLeft.GetPoint(3), bottomLeft.GetPoint(3), bottomRight.GetPoint(3)); // front | |
planes[1].Set3Points(topLeft.origin, topRight.origin, topRight.GetPoint(100)); // top | |
planes[2].Set3Points(topLeft.origin, topLeft.GetPoint(100), bottomLeft.GetPoint(100)); //left | |
planes[3].Set3Points(bottomLeft.origin, bottomLeft.GetPoint(100), bottomRight.GetPoint(100));//bottom | |
planes[4].Set3Points(topRight.origin, bottomRight.GetPoint(100), topRight.GetPoint(100)); //right | |
foreach(Unit unit in Unit.units) { | |
if(GeometryUtility.TestPlanesAABB(planes,unit.bounds)) { | |
pickedUnits.Add(unit); | |
} | |
} | |
return pickedUnits; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment