//Highlight a unit when mouse is above it void HighlightUnit() { //Change material on the latest unit we highlighted if (highlightThisUnit != null) { //But make sure the unit we want to change material on is not selected bool isSelected = false; for (int i = 0; i < selectedUnits.Count; i++) { if (selectedUnits[i] == highlightThisUnit) { isSelected = true; break; } } if (!isSelected) { highlightThisUnit.GetComponent <FriendlyUnitController>().ChangeMaterial(MaterialState.NormalMaterial); } highlightThisUnit = null; } //Fire a ray from the mouse position to get the unit we want to highlight RaycastHit hit; //Fire ray from camera if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 200f)) { //Did we hit a friendly unit? if (hit.collider.gameObject.layer == friendlyLayer) { //Get the object we hit FriendlyUnitController currentObj = hit.collider.gameObject.GetComponent <FriendlyUnitController>(); //Highlight this unit if it's not selected bool isSelected = false; for (int i = 0; i < selectedUnits.Count; i++) { if (selectedUnits[i] == currentObj) { isSelected = true; break; } } if (!isSelected) { highlightThisUnit = currentObj; highlightThisUnit.ChangeMaterial(MaterialState.HighlightMaterial); } } } }
//Select units with click or by draging the mouse void SelectUnits() { //Are we clicking with left mouse or holding down left mouse bool isClicking = false; bool isHoldingDown = false; //Click the mouse button if (Input.GetMouseButtonDown(0)) { clickTime = Time.time; //We dont yet know if we are drawing a square, but we need the first coordinate in case we do draw a square RaycastHit hit; //Fire ray from camera if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 200f, groundLayer)) { //The corner position of the square squareStartPos = hit.point; } } //Release the mouse button if (Input.GetMouseButtonUp(0)) { if (!usable) { return; } //here i need to exclude button presses if (Time.time - clickTime <= delay) { isClicking = true; Debug.Log("Screen Click"); } //Select all units within the square if we have created a square if (hasCreatedSquare) { hasCreatedSquare = false; //Deactivate the square selection image selectionSquareTrans.gameObject.SetActive(false); //Clear the list with selected unit selectedUnits.Clear(); //Debug.Log(" selectedUnits.Clear()"); //Select the units for (int i = 0; i < allUnits.Count; i++) { FriendlyUnitController currentUnit = allUnits[i]; //Is this unit within the square if (IsWithinPolygon(currentUnit.transform.position)) { currentUnit.ChangeMaterial(MaterialState.SelectedMaterial); selectedUnits.Add(currentUnit); } //Otherwise deselect the unit if it's not in the square else { currentUnit.ChangeMaterial(MaterialState.NormalMaterial); } } } } //Holding down the mouse button if (Input.GetMouseButton(0)) { //if (!usable) //return; if (Time.time - clickTime > delay) { isHoldingDown = true; } } //Select one unit with left mouse and deselect all units with left mouse by clicking on what's not a unit if (isClicking) { //Deselect all units for (int i = 0; i < selectedUnits.Count; i++) { selectedUnits[i].ChangeMaterial(MaterialState.NormalMaterial); } //Clear the list with selected units selectedUnits.Clear(); //Try to select a new unit RaycastHit hit; //Fire ray from camera if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 200f)) { //Did we hit a friendly unit? if (hit.collider.gameObject.layer == friendlyLayer) { FriendlyUnitController activeUnit = hit.collider.gameObject.GetComponent <FriendlyUnitController>(); //Set this unit to selected activeUnit.ChangeMaterial(MaterialState.SelectedMaterial); //Add it to the list of selected units, which is now just 1 unit selectedUnits.Add(activeUnit); } } } //Drag the mouse to select all units within the square if (isHoldingDown) { //Activate the square selection image if (!selectionSquareTrans.gameObject.activeInHierarchy) { selectionSquareTrans.gameObject.SetActive(true); } //Get the latest coordinate of the square squareEndPos = Input.mousePosition; //Display the selection with a GUI image DisplaySquare(); //Highlight the units within the selection square, but don't select the units if (hasCreatedSquare) { for (int i = 0; i < allUnits.Count; i++) { FriendlyUnitController currentUnit = allUnits[i]; //Is this unit within the square if (IsWithinPolygon(currentUnit.transform.position)) { currentUnit.ChangeMaterial(MaterialState.HighlightMaterial); } //Otherwise deactivate else { currentUnit.ChangeMaterial(MaterialState.NormalMaterial); } } } } }