So, I’ve got a script that creates a GUI.Box when I drag the Mouse.
Although, I’m trying to figure out how to select all of the units inside of that GUI.Box, and I’m having a problem figuring it out.
I know this has been asked before, but the answers just didn’t really explain it for me.
Here’s the code.
using UnityEngine;
using System.Collections;
public class SelectionTool : MonoBehaviour {
Vector2 lmbDown;
Vector2 lmbUp;
bool lmbDrag;
float raycastLength = 200f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetButtonDown ("Fire1")) {
lmbDown = Input.mousePosition;
}
if(Input.GetButtonUp ("Fire1")) {
lmbUp = Input.mousePosition;
lmbDrag = false;
}
if(Input.GetButton("Fire1")) {
selectionBox (Input.mousePosition);
}
}
void selectionBox(Vector2 screenPosition ) {
if (screenPosition != lmbDown) {
lmbDrag = true;
lmbUp = screenPosition;
}
}
void OnGUI() {
if (lmbDrag) {
float width = lmbUp.x - lmbDown.x;
float height = (Screen.height - lmbUp.y) - (Screen.height - lmbDown.y);
Rect rect = new Rect(lmbDown.x, Screen.height - lmbDown.y, width, height);
GUI.Box (rect, "");
}
}
}