Author Topic: Rts selection box in Unity2018  (Read 1189 times)


  • Playmaker Newbie
  • *
  • Posts: 21
    • View Profile
Rts selection box in Unity2018
« on: August 04, 2018, 11:28:49 PM »

How to select multiple objects on a scene by "drawing" a selection box on screen with mouse?

This exemple is not working in Unity2018/Playmaker 1.9

If possible without using the ArrayMaker addon but only with Array actions Inside Playmaker, please.


  • Administrator
  • Hero Member
  • *****
  • Posts: 5071
    • View Profile
    • jinxtergames
Re: Rts selection box in Unity2018
« Reply #1 on: August 06, 2018, 03:00:02 AM »
Maybe this video can help, i still need to make a proper tutorial for this  ::)


  • Playmaker Newbie
  • *
  • Posts: 21
    • View Profile
Re: Rts selection box in Unity2018
« Reply #2 on: August 06, 2018, 05:09:24 AM »

In fact, I'm looking for something like that :

I guess, the solution consists to find the objects (in the 3D world) which are Inside or behind a 2d area (the rectangle) with a simple click and drag…

Broken Stylus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
Re: Rts selection box in Unity2018
« Reply #3 on: August 06, 2018, 12:16:22 PM »
Try the Unity UI Extensions for uGUI. It's a little known set of small extensions with both game objets and components.
In your case, use Controls/Selection Box. It will add a new canvas because the drawing and selection needs to be done on a separate canvas.

Here's the description of the central script that covers this function:

 * What the SelectionBox component does is allow the game player to select objects using an RTS style click and drag interface:
 * We want to be able to select Game Objects of any type,
 * We want to be able to drag select a group of game objects to select them,
 * We want to be able to hold the shift key and drag select a group of game objects to add them to the current selection,
 * We want to be able to single click a game object to select it,
 * We want to be able to hold the shift key and single click a game object to add it to the current selection,
 * We want to be able to hold the shift key and single click an already selected game object to remove it from the current selection.
 * Most importantly, we want this behaviour to work with UI, 2D or 3D gameObjects, so it has to be smart about considering their respective screen spaces.
 * Add this component to a Gameobject with a Canvas with RenderMode.ScreenSpaceOverlay
 * And implement the IBoxSelectable interface on any MonoBehaviour to make it selectable.
 * Improvements that could be made:
 * Control clicking a game object to select all objects of that type or tag.
 * Compatibility with Canvas Scaling
 * Filtering single click selections of objects occupying the same space. (So that, for example, you're only click selecting the game object found closest to the camera)

SelectionBox component must be placed on a canvas in Screen Space Overlay mode.

From there you'll have to paste as a MonoBehaviour component, a script similar to the ExampleSelectable (inside it the IBoxSectable class is implemented, which is necessary to receive info from the selection script).
You could tweak that example script, it's very small and rather simple to understand even for a non-coder; you may add Playmaker-specific function like stuff that sends events in the void Update() phase.
Check for help on this forum as to how send events to Playmaker from "normal" scripts.

Otherwise, iIf you want to do it from scratch, since I haven't done such a thing yet, I presume you'd need to start from a UI image with a slice-9 graphic inside and constantly update the size of the UI selection zone by stretching the image between the initial click position and the current cursor position.
You may want to be careful too to avoid the red cross thing that means you're seeing the back of the image and the front is not facing the camera.
This would happen if you first started dragging rightwards and then decided to actually go leftwards and crossing over the coordinates of the your first mouse click.
You'd probably need to have several branches, maybe four, in your Fsm to check in which direction you're drawing the rectangle from the first click position, and check that constantly.
The way I'd do that is first check in which direction you're going even before displaying the rectangle, and then set the anchor in the opposite corner. So if you click then drag to the bottom right, put the anchor at the top left of the selection rectangle.
And if you actually drag as much as to actually cross the anchor's position on either x or y, then redo the new opposite-anchoring process based on the new direction you're moving the mouse's cursor. This should allow you to quickly increase the size (width or/and height) of your UI element in the RectTransform without having to fiddle with anything else.

As for what to do next...
Taking the min and max offsets from that UI image object that is your rectangle selection, you'd have to cast an invisible 3D rectangle (trigger?) which depth axis would be parallelal to the camera's direction it's pointing at.
Using the distance between the offsets's x and y mentioned before, you would set the size of the rectangle on x and y once translated into camera space (I suppose you'd have to multiply values at this point regarding the rectangle's size in screen space and the area you're selecting over in the game's world space). This rectangle would therefore be used to check if your units' game objects are inside the trigger (make the rectangle deep enough) and you may even build it so that you can do raycast and test for collisions and not just the units' respective pivots, but that may eat more CPU.
I'm just thinking out loud tho', take that with a grain of sand.

There's perhaps an action on Ecosystem that covers the detection of game objects based on a rectangle drawn on screen?