private void ClearSelectionArea() { unitSelectionArea.gameObject.SetActive(false); if (unitSelectionArea.sizeDelta.magnitude == 0) { SelectSingleUnit(); return; } Vector2 min = unitSelectionArea.anchoredPosition - (unitSelectionArea.sizeDelta / 2); Vector2 max = unitSelectionArea.anchoredPosition + (unitSelectionArea.sizeDelta / 2); foreach (Unit unit in rtsPlayer.GetMyUnits()) { if (SelectedUnits.Contains(unit)) { continue; } Vector3 screenPosition = mainCamera.WorldToScreenPoint(unit.transform.position); //getting screen position of a unit if (screenPosition.x > min.x && //checking if unit is inside box of selector screenPosition.x < max.x && screenPosition.y > min.y && screenPosition.y < max.y) { SelectedUnits.Add(unit); unit.Select(); } } }
private void HandleMultipleSelction() { Vector2 min = unitSelectionArea.anchoredPosition - (unitSelectionArea.sizeDelta / 2); Vector2 max = unitSelectionArea.anchoredPosition + (unitSelectionArea.sizeDelta / 2); foreach (Unit unit in rtsPlayer.GetMyUnits()) { if (SelectedUnits.Contains(unit)) { continue; } Vector3 screenPosition = mainCamera.WorldToScreenPoint(unit.transform.position); if (screenPosition.x < min.x) { return; } if (screenPosition.x > max.x) { return; } if (screenPosition.y < min.y) { return; } if (screenPosition.y > max.y) { return; } SelectedUnits.Add(unit); unit.SelectUnit(); } }