Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

Discussion in 'UGUI & TextMesh Pro' started by AidanofVT, May 29, 2020.

  1. AidanofVT

    AidanofVT

    Joined:
    Nov 10, 2019
    Posts:
    104
    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:

    Code (CSharp):
    1. public class UnitSelection : MonoBehaviour {
    2.     public GameState gameState;
    3.     float downTime = 1000000;
    4.     enum relativeMousePlaces {upRight, downRight, downLeft, upleft};
    5.     relativeMousePlaces mouseVsStart;
    6.     Vector2 mousePosLastFrame;
    7.     Vector2 mouseDownLocation;
    8.     public Transform quadrangle;
    9.     Transform selectorSquare = null;
    10.  
    11.     void Awake() {
    12.         gameState = gameObject.GetComponent<GameState>();
    13.         quadrangle.gameObject.SetActive(false);
    14.     }
    15.  
    16.     void Update() {
    17.         if (Input.GetKeyDown(KeyCode.Mouse0)) {
    18.             downTime = Time.time;
    19.             mouseDownLocation = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    20.             mousePosLastFrame = mouseDownLocation;
    21.         }
    22.         else if (Input.GetKeyUp(KeyCode.Mouse0)) {
    23.             downTime = 1000000;
    24.             selectorSquare.gameObject.SetActive(false);
    25.             selectorSquare = null;
    26.         }
    27.         else if (Time.time - downTime >= 0.1) {
    28.             if (selectorSquare == null) {
    29.                 selectorSquare = quadrangle;
    30.                 selectorSquare.gameObject.SetActive(true);
    31.                 Debug.Log(selectorSquare.gameObject.activeSelf);
    32.             }
    33.             else {
    34.                 float xDelta = (Input.mousePosition).x - (mousePosLastFrame).x;
    35.                 float yDelta = (Input.mousePosition).y - (mousePosLastFrame).y;
    36.                 selectorSquare.localScale += new Vector3 (xDelta, yDelta, 0);
    37.                 if (Input.mousePosition.x > mouseDownLocation.x) {
    38.                     if (Input.mousePosition.y > mouseDownLocation.y) {
    39.                         mouseVsStart = relativeMousePlaces.upRight;
    40.                     }
    41.                     else {
    42.                         mouseVsStart = relativeMousePlaces.downRight;
    43.                     }
    44.                 }
    45.                 else if (Input.mousePosition.y < mouseDownLocation.y) {
    46.                     mouseVsStart = relativeMousePlaces.downLeft;
    47.                 }
    48.                 else {
    49.                     mouseVsStart = relativeMousePlaces.upleft;
    50.                 }
    51.                 Vector3 changeMag = Camera.main.ScreenToWorldPoint(Input.mousePosition) - Camera.main.ScreenToWorldPoint((Vector3)mousePosLastFrame);
    52.                 selectorSquare.position += changeMag;
    53.             }
    54.             mousePosLastFrame = Input.mousePosition;
    55.         }
    56.     }
     
  2. blu3drag0n

    blu3drag0n

    Joined:
    Nov 9, 2018
    Posts:
    94
    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:
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. /// <summary>
    4. /// Draws a selection rectangle on the left mouse button down & dragging
    5. ///
    6. /// You only need an InputReceiverManager that basically tracks
    7. /// - if the left mouse button is currently down and saves it in "LeftMouseButtonDown"
    8. /// - saves the initial click position when mouse button was clicked  and saves it in "InitialMousePositionOnLeftClick"
    9. /// - updates the current mouse position and saves it in "CurrentMousePosition"
    10. ///
    11. /// </summary>
    12. public class SelectionRectangleDrawer : MonoBehaviour
    13. {
    14.     //set color via inspector for the selection rectangles filler color
    15.     public Color SelectionRectangleFillerColor;
    16.     //set color via inspector for the selection rectangles border color
    17.     public Color SelectionRectangleBorderColor;
    18.     //set selection rectangles  border thickness
    19.     public int SelectionRectangleBorderThickness = 2;
    20.     private Texture2D _selectionRectangleFiller;
    21.     private Texture2D _selectionRectangleBorder;
    22.     private bool _drawSelectionRectangle;
    23.     private float _x1, _x2, _y1, _y2;
    24.     private Vector2 pos1, pos2;
    25.     void Start()
    26.     {
    27.         _selectionRectangleFiller = new Texture2D(1, 1);
    28.         _selectionRectangleFiller.SetPixel(0, 0, SelectionRectangleFillerColor);
    29.         _selectionRectangleFiller.Apply();
    30.         _selectionRectangleFiller.wrapMode = TextureWrapMode.Clamp;
    31.         _selectionRectangleFiller.filterMode = FilterMode.Point;
    32.         _selectionRectangleBorder = new Texture2D(1, 1);
    33.         _selectionRectangleBorder.SetPixel(0, 0, SelectionRectangleBorderColor);
    34.         _selectionRectangleBorder.Apply();
    35.         _selectionRectangleBorder.wrapMode = TextureWrapMode.Clamp;
    36.         _selectionRectangleBorder.filterMode = FilterMode.Point;
    37.     }
    38.     void Update()
    39.     {
    40.         if (InputReceiverManager.Instance.LeftMouseButtonDown && !Mathf.Approximately(Vector2.Distance(InputReceiverManager.Instance.CurrentMousePosition,
    41.                                                                                                        InputReceiverManager.Instance.InitialMousePositionOnLeftClick), 0f))
    42.             _drawSelectionRectangle = true;
    43.         else if (!InputReceiverManager.Instance.LeftMouseButtonDown && _drawSelectionRectangle)
    44.             _drawSelectionRectangle = false;
    45.     }
    46.     private void OnGUI()
    47.     {
    48.         if (_drawSelectionRectangle)
    49.             drawSelectionRectangle();
    50.     }
    51.     private void drawSelectionRectangle()
    52.     {
    53.         pos1 = InputReceiverManager.Instance.InitialMousePositionOnLeftClick;
    54.         pos2 = InputReceiverManager.Instance.CurrentMousePosition;
    55.         //check initial mouse position on X axis versus dragging mouse position
    56.         if (pos1.x < pos2.x)
    57.         {
    58.             _x1 = pos1.x;
    59.             _x2 = pos2.x;
    60.         }
    61.         else
    62.         {
    63.             _x1 = pos2.x;
    64.             _x2 = pos1.x;
    65.         }
    66.         //check initial mouse position on Y axis versus dragging mouse position
    67.         if (pos1.y < pos2.y)
    68.         {
    69.             _y1 = pos1.y;
    70.             _y2 = pos2.y;
    71.         }
    72.         else
    73.         {
    74.             _y1 = pos2.y;
    75.             _y2 = pos1.y;
    76.         }
    77.         //filler
    78.         GUI.DrawTexture(new Rect(_x1, Screen.height - _y1, _x2 - _x1, _y1 - _y2), _selectionRectangleFiller, ScaleMode.StretchToFill);
    79.         //top line
    80.         GUI.DrawTexture(new Rect(_x1, Screen.height - _y1, _x2 - _x1, -SelectionRectangleBorderThickness), _selectionRectangleBorder, ScaleMode.StretchToFill);
    81.         //bottom line
    82.         GUI.DrawTexture(new Rect(_x1, Screen.height - _y2, _x2 - _x1, SelectionRectangleBorderThickness), _selectionRectangleBorder, ScaleMode.StretchToFill);
    83.         //left line
    84.         GUI.DrawTexture(new Rect(_x1, Screen.height - _y1, SelectionRectangleBorderThickness, _y1 - _y2), _selectionRectangleBorder, ScaleMode.StretchToFill);
    85.         //right line
    86.         GUI.DrawTexture(new Rect(_x2, Screen.height - _y1, -SelectionRectangleBorderThickness, _y1 - _y2), _selectionRectangleBorder, ScaleMode.StretchToFill);
    87.     }
    88. }
    89.  
    90.  
    91.