///Sourced from - http://forum.unity3d.com/threads/rts-style-drag-selection-box.265739/
///Updated Credit BenZed
///Sourced from - http://forum.unity3d.com/threads/color-picker.267043/
/*
* 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)
*
*/
usingSystem.Collections.Generic;
usingUnityEngine.Events;
namespaceUnityEngine.UI.Extensions
{
[RequireComponent(typeof(Canvas))]
[AddComponentMenu("UI/Extensions/Selection Box")]
publicclassSelectionBox:MonoBehaviour
{
// The color of the selection box.
publicColorcolor;
// An optional parameter, but you can add a sprite to the selection box to give it a border or a stylized look.
// It's suggested you use a monochrome sprite so that the selection
// Box color is still relevant.
publicSpriteart;
// Will store the location of wherever we first click before dragging.
privateVector2origin;
// A rectTransform set by the User that can limit which part of the screen is eligible for drag selection
publicRectTransformselectionMask;
//Stores the rectTransform connected to the generated gameObject being used for the selection box visuals
privateRectTransformboxRect;
// Stores all of the selectable game objects
privateIBoxSelectable[]selectables;
// A secondary storage of objects that the user can manually set.
privateMonoBehaviour[]selectableGroup;
//Stores the selectable that was touched when the mouse button was pressed down
privateIBoxSelectableclickedBeforeDrag;
//Stores the selectable that was touched when the mouse button was released
privateIBoxSelectableclickedAfterDrag;
//Custom UnityEvent so we can add Listeners to this instance when Selections are changed.
Debug.LogWarning("SelectionBox component is on a gameObject with a Canvas Scaler component. As of now, Canvas Scalers without the default settings throw off the coordinates of the selection box. Canvas Scaler has been removed.");
}
}
/*
* The user can manually set a group of objects with monoBehaviours to be the pool of objects considered to be selectable. The benefits of this are two fold:
*
* 1) The default behaviour is to check every game object in the scene, which is much slower.
* 2) The user can filter which objects should be selectable, for example units versus menu selections
//If the initial click point is not inside the selection mask, we abort the selection
if(!PointIsValidAgainstSelectionMask(origin)){
ResetBoxRect();
return;
}
// The anchor is set to the same place.
boxRect.anchoredPosition=origin;
MonoBehaviour[]behavioursToGetSelectionsFrom;
// If we do not have a group of selectables already set, we'll just loop through every object that's a monobehaviour, and look for selectable interfaces in them
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.