Selecting box

Someone can give me some advice about drawing a selecting box ?

Like Warcraft 3 (or almost any RTS actually), when the user drag the mouse, a line box appears, delimiting the selected area (one corner is the start position and the opposite corner is the actual position). I need some options about how to draw this line box.

I think i now how to detect which units are selected, which not, only friendly units or only enemy units, etc. but i need this line box or some other alternative.

thanks

.ORG

Offhand, two possibilities come to mind:

  1. using GL to draw the lines

or

  1. displaying a GUITexture that shrinks/grows appropriately as you drag the mouse

I would create the selection box with geometry, or use GL if I had Pro.

The best way will be to use one GUITexture for each line in the box. You should use a white texture on the GUITextures and set the color in script. You will have to set the GUITexture’s pixel rects every frame depending on the mouse position when clicked and the current mouse position.

A GUITexture for each line? ouch. Just one GUITexture, with border values set, will do.

Forgot about the border settings. Yeah I think that would work.

Yep, and for best results in this case you probably want to turn off filtering on the GUITexture.

–Eric

Thanks for the advice, ill work on it, i have some other alternatives for this selection box though

.ORG

Tell us your alternatives so everyone benefits! :slight_smile:

Something like this:

Image a semitransparent sphere that is created dynamically with a radius equal to the distance from the initial point to the actual cursor point.

The collider created with the sphere will tell you which units are inside and could be selected.

.ORG

Physics.OverlapSphere() will do that for you.

http://unity3d.com/Documentation/ScriptReference/Physics.OverlapSphere.html

-Jeremy

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);
    }
}