'Cannot MultiSelect Dragging in Unity 3D
I am using the tutorial in this video https://www.youtube.com/watch?v=mNP80694C-o so that I can drag multiple selection, but the dragging for multi selection is not working and it didn't display any error whatsoever. Anyone can help me?
Here's the code
This is for the multiple selection
//MultiSelect
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace LP.FDG.InputManager
{
public static class MultiSelect
{
private static Texture2D _whiteTexture;
public static Texture2D WhiteTexture
{
get
{
if (_whiteTexture == null)
{
_whiteTexture = new Texture2D(1, 1);
_whiteTexture.SetPixel(0, 0, Color.white);
_whiteTexture.Apply();
}
return _whiteTexture;
}
}
public static void DrawScreenRect(Rect rect, Color color)
{
GUI.color = color;
GUI.DrawTexture(rect, WhiteTexture);
}
public static void DrawScreenRectBorder(Rect rect, float thickness, Color color)
{
//Top
DrawScreenRect(new Rect(rect.xMin, rect.yMin, rect.width, thickness), color);
//Bottom
DrawScreenRect(new Rect(rect.xMin, rect.yMax - thickness, rect.width, thickness), color);
//Left
DrawScreenRect(new Rect(rect.xMin, rect.yMin, thickness, rect.height), color);
//Right
DrawScreenRect(new Rect(rect.xMax - thickness, rect.yMin, thickness, rect.height), color);
}
public static Rect GetScreenRect(Vector3 screenPos1, Vector3 screenPos2)
{
screenPos1.y = Screen.height - screenPos1.y;
screenPos2.y = Screen.height - screenPos2.y;
Vector3 bR = Vector3.Max(screenPos1, screenPos2);
Vector3 tL = Vector3.Min(screenPos1, screenPos2);
return Rect.MinMaxRect(tL.x, tL.y, bR.x, bR.y);
}
public static Bounds GetVPBounds(Camera cam, Vector3 screenPos1, Vector3 screenPos2)
{
Vector3 pos1 = cam.ScreenToViewportPoint(screenPos1);
Vector3 pos2 = cam.ScreenToViewportPoint(screenPos2);
Vector3 min = Vector3.Min(pos1, pos2);
Vector3 max = Vector3.Max(pos1, pos2);
min.z = cam.nearClipPlane;
max.z = cam.farClipPlane;
Bounds bounds = new Bounds();
bounds.SetMinMax(min, max);
return bounds;
}
}
}
This is for the input handler
//InputHandler
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace LP.FDG.InputManager
{
public class InputHandler : MonoBehaviour
{
public static InputHandler instance;
private RaycastHit hit;
private List<Transform> selectedUnit = new List<Transform>();
private bool isDragging = false;
private Vector3 mousePos;
void Start()
{
instance = this;
}
private void OnGui()
{
if (isDragging)
{
Rect rect = MultiSelect.GetScreenRect(mousePos, Input.mousePosition);
MultiSelect.DrawScreenRect(rect, new Color(0f, 0f, 0f, 0.25f));
MultiSelect.DrawScreenRectBorder(rect, 3, Color.blue);
}
}
public void HandleUnitMovement()
{
mousePos = Input.mousePosition;
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
LayerMask layerHit = hit.transform.gameObject.layer;
switch (layerHit.value)
{
case 8:
SelectUnit(hit.transform, Input.GetKey(KeyCode.LeftShift));
break;
default:
isDragging = true;
DeselectUnit();
break;
}
}
}
if (!Input.GetMouseButtonUp(0))
{
foreach (Transform child in Player.PlayerManager.instance.playerUnits)
{
foreach (Transform unit in child)
{
if (isWithinSelectionBounds(unit))
{
SelectUnit(unit, true);
}
}
}
isDragging = false;
}
}
private void SelectUnit(Transform unit, bool canMultiselect = false)
{
if(!canMultiselect)
{
DeselectUnit();
}
selectedUnit.Add(unit);
unit.Find("Highlight").gameObject.SetActive(true);
}
private void DeselectUnit()
{
for (int i = 0; i < selectedUnit.Count; i++)
{
selectedUnit[i].Find("Highlight").gameObject.SetActive(false);
}
selectedUnit.Clear();
}
private bool isWithinSelectionBounds(Transform tf)
{
if(isDragging)
{
return false;
}
Camera cam = Camera.main;
Bounds vpBounds = MultiSelect.GetVPBounds(cam, mousePos, Input.mousePosition);
return vpBounds.Contains(cam.WorldToViewportPoint(tf.position));
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
