(novice) I'm trying to replicate the desktop click-and-drag selection rectangle...

And man, is it HARD. There’s no ‘corner’ value that can be modified to lead the shape of the rectangle. Does anyone have any quick tips for doing this? I’ve seen some online tutorials, but they’re all for 3d, and I’m using 2d. Here’s what I’ve got so far. It’s a bit of a mess. It does create a rectangle which vaguely follows the cursor, but the starting corner definitely doesn’t stay put, and corner opposite it definitely doesn’t stick to the mouse. I get the feeling that this is completely the wrong approach:

public class UnitSelection : MonoBehaviour {
    public GameState gameState;
    float downTime = 1000000;
    enum relativeMousePlaces {upRight, downRight, downLeft, upleft};
    relativeMousePlaces mouseVsStart;
    Vector2 mousePosLastFrame;
    Vector2 mouseDownLocation;
    public Transform quadrangle;
    Transform selectorSquare = null;

    void Awake() {
        gameState = gameObject.GetComponent<GameState>();
        quadrangle.gameObject.SetActive(false);
    }

    void Update() {
        if (Input.GetKeyDown(KeyCode.Mouse0)) {
            downTime = Time.time;
            mouseDownLocation = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            mousePosLastFrame = mouseDownLocation;
        }
        else if (Input.GetKeyUp(KeyCode.Mouse0)) {
            downTime = 1000000;
            selectorSquare.gameObject.SetActive(false);
            selectorSquare = null;
        }
        else if (Time.time - downTime >= 0.1) {
            if (selectorSquare == null) {
                selectorSquare = quadrangle;
                selectorSquare.gameObject.SetActive(true);
                Debug.Log(selectorSquare.gameObject.activeSelf);
            }
            else {
                float xDelta = (Input.mousePosition).x - (mousePosLastFrame).x;
                float yDelta = (Input.mousePosition).y - (mousePosLastFrame).y;
                selectorSquare.localScale += new Vector3 (xDelta, yDelta, 0);
                if (Input.mousePosition.x > mouseDownLocation.x) {
                    if (Input.mousePosition.y > mouseDownLocation.y) {
                        mouseVsStart = relativeMousePlaces.upRight;
                    }
                    else {
                        mouseVsStart = relativeMousePlaces.downRight;
                    }
                }
                else if (Input.mousePosition.y < mouseDownLocation.y) {
                    mouseVsStart = relativeMousePlaces.downLeft;
                }
                else {
                    mouseVsStart = relativeMousePlaces.upleft;
                }
                Vector3 changeMag = Camera.main.ScreenToWorldPoint(Input.mousePosition) - Camera.main.ScreenToWorldPoint((Vector3)mousePosLastFrame);
                selectorSquare.position += changeMag;
            }
            mousePosLastFrame = Input.mousePosition;
        }
    }

This is an old post, but when I was looking for a solution to get a selection rectangle drawing in game mode, also in a build version, then there were not really satisfying answers. Most tried via some assets packages or either Line Renderer or Shaders, which I found to be overloaded for what I was trying to achieve.

I finally managed to build it myself with very few code and want to share for everyone else googling in the future, enjoy:

using UnityEngine;
/// <summary>
/// Draws a selection rectangle on the left mouse button down & dragging
///
/// You only need an InputReceiverManager that basically tracks
/// - if the left mouse button is currently down and saves it in "LeftMouseButtonDown"
/// - saves the initial click position when mouse button was clicked  and saves it in "InitialMousePositionOnLeftClick"
/// - updates the current mouse position and saves it in "CurrentMousePosition"
///
/// </summary>
public class SelectionRectangleDrawer : MonoBehaviour
{
    //set color via inspector for the selection rectangles filler color
    public Color SelectionRectangleFillerColor;
    //set color via inspector for the selection rectangles border color
    public Color SelectionRectangleBorderColor;
    //set selection rectangles  border thickness
    public int SelectionRectangleBorderThickness = 2;
    private Texture2D _selectionRectangleFiller;
    private Texture2D _selectionRectangleBorder;
    private bool _drawSelectionRectangle;
    private float _x1, _x2, _y1, _y2;
    private Vector2 pos1, pos2;
    void Start()
    {
        _selectionRectangleFiller = new Texture2D(1, 1);
        _selectionRectangleFiller.SetPixel(0, 0, SelectionRectangleFillerColor);
        _selectionRectangleFiller.Apply();
        _selectionRectangleFiller.wrapMode = TextureWrapMode.Clamp;
        _selectionRectangleFiller.filterMode = FilterMode.Point;
        _selectionRectangleBorder = new Texture2D(1, 1);
        _selectionRectangleBorder.SetPixel(0, 0, SelectionRectangleBorderColor);
        _selectionRectangleBorder.Apply();
        _selectionRectangleBorder.wrapMode = TextureWrapMode.Clamp;
        _selectionRectangleBorder.filterMode = FilterMode.Point;
    }
    void Update()
    {
        if (InputReceiverManager.Instance.LeftMouseButtonDown && !Mathf.Approximately(Vector2.Distance(InputReceiverManager.Instance.CurrentMousePosition,
                                                                                                       InputReceiverManager.Instance.InitialMousePositionOnLeftClick), 0f))
            _drawSelectionRectangle = true;
        else if (!InputReceiverManager.Instance.LeftMouseButtonDown && _drawSelectionRectangle)
            _drawSelectionRectangle = false;
    }
    private void OnGUI()
    {
        if (_drawSelectionRectangle)
            drawSelectionRectangle();
    }
    private void drawSelectionRectangle()
    {
        pos1 = InputReceiverManager.Instance.InitialMousePositionOnLeftClick;
        pos2 = InputReceiverManager.Instance.CurrentMousePosition;
        //check initial mouse position on X axis versus dragging mouse position
        if (pos1.x < pos2.x)
        {
            _x1 = pos1.x;
            _x2 = pos2.x;
        }
        else
        {
            _x1 = pos2.x;
            _x2 = pos1.x;
        }
        //check initial mouse position on Y axis versus dragging mouse position
        if (pos1.y < pos2.y)
        {
            _y1 = pos1.y;
            _y2 = pos2.y;
        }
        else
        {
            _y1 = pos2.y;
            _y2 = pos1.y;
        }
        //filler
        GUI.DrawTexture(new Rect(_x1, Screen.height - _y1, _x2 - _x1, _y1 - _y2), _selectionRectangleFiller, ScaleMode.StretchToFill);
        //top line
        GUI.DrawTexture(new Rect(_x1, Screen.height - _y1, _x2 - _x1, -SelectionRectangleBorderThickness), _selectionRectangleBorder, ScaleMode.StretchToFill);
        //bottom line
        GUI.DrawTexture(new Rect(_x1, Screen.height - _y2, _x2 - _x1, SelectionRectangleBorderThickness), _selectionRectangleBorder, ScaleMode.StretchToFill);
        //left line
        GUI.DrawTexture(new Rect(_x1, Screen.height - _y1, SelectionRectangleBorderThickness, _y1 - _y2), _selectionRectangleBorder, ScaleMode.StretchToFill);
        //right line
        GUI.DrawTexture(new Rect(_x2, Screen.height - _y1, -SelectionRectangleBorderThickness, _y1 - _y2), _selectionRectangleBorder, ScaleMode.StretchToFill);
    }
}