Here is the code:
using Mirror;
using UnityEngine;
using UnityEngine.InputSystem;
public class UnitSelectionHandler : MonoBehaviour
{
[SerializeField] private RectTransform unitSelctionArea = null;
[SerializeField] private LayerMask layerMask = new LayerMask();
private Vector2 startPos;
private RTSPlayer player;
private Camera mainCamera;
public List<Unit> SelectedUnits { get; } = new List<Unit>();
private void Start()
{
mainCamera = Camera.main;
}
private void Update()
{
if(player == null)
{
player = NetworkClient.connection.identity.GetComponent<RTSPlayer>();
}
if (Mouse.current.leftButton.wasPressedThisFrame)
{
StartSelectionArea();
}
else if (Mouse.current.leftButton.wasReleasedThisFrame)
{
ClearSelectionArea();
}
else if (Mouse.current.leftButton.isPressed)
{
UpdateSelectionArea();
}
}
private void StartSelectionArea()
{
foreach (Unit selectedUnit in SelectedUnits)
{
selectedUnit.Deselect();
}
SelectedUnits.Clear();
unitSelctionArea.gameObject.SetActive(true);
startPos = Mouse.current.position.ReadValue();
UpdateSelectionArea();
}
private void UpdateSelectionArea()
{
Vector2 mousePos = Mouse.current.position.ReadValue();
float areaWidth = mousePos.x - startPos.x;
float areaHeight = mousePos.y - startPos.y;
unitSelctionArea.sizeDelta = new Vector2(Mathf.Abs(areaWidth), Mathf.Abs(areaHeight));
unitSelctionArea.anchoredPosition = startPos + new Vector2((areaWidth / 2), (areaHeight / 2));
}
private void ClearSelectionArea()
{
unitSelctionArea.gameObject.SetActive(false);
if(unitSelctionArea.sizeDelta.magnitude == 0)
{
Ray ray = mainCamera.ScreenPointToRay(Mouse.current.position.ReadValue());
if (!Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, layerMask)) { return; }
if (!hit.collider.TryGetComponent<Unit>(out Unit unit)) { return; }
if (!unit.hasAuthority) { return; }
SelectedUnits.Add(unit);
foreach (Unit selectedUnit in SelectedUnits)
{
selectedUnit.Select();
}
return;
}
Vector2 min = unitSelctionArea.anchoredPosition - (unitSelctionArea.sizeDelta / 2);
Vector2 max = unitSelctionArea.anchoredPosition + (unitSelctionArea.sizeDelta / 2);
foreach(Unit unit in player.GetMyUnits())
{
Vector3 screenPos = mainCamera.WorldToScreenPoint(unit.transform.position);
if(screenPos.x > min.x && screenPos.x < max.x && screenPos.y > min.y && screenPos.y < max.y)
{
SelectedUnits.Add(unit);
unit.Select();
}
}
}
}
I am inheriting from a monobehaviour and not networkbehaviour, but according to the GitLab for the section so is Nathan I believe so I didn’t think that was an issue. I am currently using 35.1.0 and I see there is an update. I will see if that makes any difference