I need to know how to draw a box that starts at Input.GetMouseButtonDown(0) and continues to render and change size until Input.GetMouseButtonUp(0). And based on the mouse position. I cant figure out where to start.
Here’s an example to get you started:
using UnityEngine;
public class MouseBox : MonoBehaviour {
bool _isValid; //
bool _isDown;
Vector2 _mouseDownPos;
Vector2 _mouseLastPos;
// Update is called once per frame
void Update () {
// Detect when mouse button is down
if (Input.GetMouseButtonDown(0)) {
_mouseDownPos = Input.mousePosition;
_isDown = true;
_isValid = true;
}
// Continue tracking mouse position until the button is raised
if (_isDown) {
_mouseLastPos = Input.mousePosition;
if (Input.GetMouseButtonUp(0)) {
_isDown = false;
// If you want the box to vanish now, just set 'isValid' to false.
}
}
}
void OnGUI() {
if (_isValid) {
// Find the corner of the box
Vector2 origin;
origin.x = Mathf.Min(_mouseDownPos.x, _mouseLastPos.x);
// GUI and mouse coordinates are the opposite way around.
origin.y = Mathf.Max(_mouseDownPos.y, _mouseLastPos.y);
origin.y = Screen.height - origin.y;
//Compute size of box
Vector2 size = _mouseDownPos - _mouseLastPos;
size.x = Mathf.Abs(size.x);
size.y = Mathf.Abs(size.y);
// Draw it!
Rect r = new Rect(origin.x, origin.y, size.x, size.y);
GUI.Box(r, "");
}
}
}