void Update() { if (!EventSystem.current.IsPointerOverGameObject()) { // Click somewhere in the Game View. if (Input.GetMouseButtonDown(0)) { // Get the initial click position of the mouse. No need to convert to GUI space // since we are using the lower left as anchor and pivot. initialClickPosition = new Vector2(Input.mousePosition.x, Input.mousePosition.y); // The anchor is set to the same place. selectionBox.anchoredPosition = initialClickPosition; } // While we are dragging. if (Input.GetMouseButton(0)) { // Store the current mouse position in screen space. Vector2 currentMousePosition = new Vector2(Input.mousePosition.x, Input.mousePosition.y); // How far have we moved the mouse? Vector2 difference = currentMousePosition - initialClickPosition; // Copy the initial click position to a new variable. Using the original variable will cause // the anchor to move around to wherever the current mouse position is, // which isn't desirable. Vector2 startPoint = initialClickPosition; //Debug.Log ("Width: " + difference.x + " Height: " + difference.y); // The following code accounts for dragging in various directions. if (difference.x < 0) { startPoint.x = currentMousePosition.x; difference.x = -difference.x; } if (difference.y < 0) { startPoint.y = currentMousePosition.y; difference.y = -difference.y; } // Set the anchor, width and height every frame. selectionBox.transform.position = startPoint; selectionBox.sizeDelta = difference; } } // After we release the mouse button. if (Input.GetMouseButtonUp(0)) { Rect rect = new Rect(selectionBox.anchoredPosition, selectionBox.sizeDelta); foreach (GameObject unit in WorldHandler.countUnitsAsArray()) { if (rect.Contains((Vector2)Camera.main.WorldToScreenPoint(unit.transform.position))) { /*if (!WorldHandler.isShiftDown ()) { * WorldHandler.deselectUnits (); * WorldHandler.deselectAntHill (); * WorldHandler.hideAnthillInfo (); * }*/ GameObject indicator = unit.transform.FindChild("Indicator").gameObject; if (unit.GetComponent <BasicAnt> () != null) // changed with addition of beatle { BasicAnt unitScript = unit.GetComponent <BasicAnt> (); indicator.SetActive(!indicator.activeSelf); unitScript.isUnitSelected = indicator.activeSelf; if (unitScript.isUnitSelected) { WorldHandler.PlayUnitBattleSound(); } if (unitScript.isUnitSelected) { WorldHandler.unitsSelected.Add(unit); } else { WorldHandler.unitsSelected.Remove(unit); } } if (unit.GetComponent <Beatle> () != null) // changed with addition of beatle. { Beatle unitScript = unit.GetComponent <Beatle> (); indicator.SetActive(!indicator.activeSelf); unitScript.isUnitSelected = indicator.activeSelf; if (unitScript.isUnitSelected) { WorldHandler.PlayUnitBattleSound(); } if (unitScript.isUnitSelected) { WorldHandler.unitsSelected.Add(unit); } else { WorldHandler.unitsSelected.Remove(unit); } } } } // Reset initialClickPosition = Vector2.zero; selectionBox.anchoredPosition = Vector2.zero; selectionBox.sizeDelta = Vector2.zero; } }