using UnityEngine;
public class SelectionRect : MonoBehaviour
{
public GUIStyle style;
private Vector2 startPos;
private Vector2 endPos;
private Rect rect;
private bool drawRect;
private readonly GUIContent cont = new GUIContent();
void OnGUI ()
{
if (Input.GetMouseButtonDown(0))
{
startPos = Input.mousePosition;
drawRect = true;
}
if (Input.GetMouseButtonUp(0))
drawRect = false;
if (drawRect)
{
endPos = Input.mousePosition;
if(startPos==endPos)return;
rect = new Rect(Mathf.Min(endPos.x, startPos.x),
Screen.height - Mathf.Max(endPos.y, startPos.y),
Mathf.Max(endPos.x, startPos.x) - Mathf.Min(endPos.x, startPos.x),
Mathf.Max(endPos.y, startPos.y) - Mathf.Min(endPos.y, startPos.y)
);
GUI.Box(rect,cont,style);
}
}
}