示例#1
0
    private void ClickOrDrag()
    {
        Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;

        if (Physics.Raycast(ray, out hit, Mathf.Infinity) && /** !EventSystem.current.IsPointerOverGameObject()  && **/ !UnitManager.instance.buildMode)
        {
            currentMousePoint = hit.point;
            if (Input.GetMouseButtonDown(0) && !Input.GetKey(KeyCode.LeftShift))
            {
                mouseDownPoint = hit.point;

                mouseDragStartPosition = Input.mousePosition;

                //click to select unit, or click the ground to deselect all
                if (hit.collider.gameObject.tag == "Unit")
                {
                    selectedUnit = hit.collider.gameObject;
                    selectFSM    = SelectFSM.clickSelect;
                }
                else if (hit.collider.gameObject.tag == "Ground")
                {
                    selectedTable.deselectAll();
                }
                //selectFSM = SelectFSM.clickDeselect;
            }
            else if (Input.GetMouseButtonDown(0) && Input.GetKey(KeyCode.LeftShift))
            {
                //holding shift, click to select units or click selected units to deselect
                if (hit.collider.gameObject.tag == "Unit" && !selectedTable.getDictionary().ContainsKey(hit.collider.gameObject.GetInstanceID()))
                {
                    //AddToCurrentlySelectedUnits(hit.collider.gameObject);
                    selectedTable.addSelected(hit.collider.gameObject);
                }
                else if (hit.collider.gameObject.tag == "Unit" && selectedTable.getDictionary().ContainsKey(hit.collider.gameObject.GetInstanceID()))
                {
                    //RemoveFromCurrentlySelectedUnits(hit.collider.gameObject);
                    selectedTable.deselect(hit.collider.gameObject.GetInstanceID());
                }
            }
            else if (Input.GetMouseButton(0) && !Input.GetKey(KeyCode.LeftShift))
            {
                if (UserDraggingByPosition(mouseDragStartPosition, Input.mousePosition))
                {
                    mouseDragging = true;
                    DrawDragBox();
                    SelectUnitsInDrag();
                }
            }
            else if (Input.GetMouseButtonUp(0) && !Input.GetKey(KeyCode.LeftShift))
            {
                mouseDragging = false;
            }
        }
    }
    void HandlesMouseInput()
    {
        //Right Mouse Button Click
        if (Input.GetMouseButtonDown(1))
        {
            startPosition = Input.mousePosition;
        }

        //Right Mouse Button Held
        if (Input.GetMouseButton(1))
        {
            if ((startPosition - Input.mousePosition).magnitude > dragArea)
            {
                dragSelect = true;
            }
        }

        //Right Mouse Button Released
        if (Input.GetMouseButtonUp(1))
        {
            //Selects a single unit
            if (dragSelect == false)
            {
                Ray ray = Camera.main.ScreenPointToRay(startPosition);

                //I set it to an extra large number just incase
                if (Physics.Raycast(ray, out hit, 50000.0f))
                {
                    //Left shift will allow the selection of multiple units
                    //Inclusive Select
                    if (Input.GetKey(KeyCode.LeftShift))
                    {
                        selectedTable.addSelected(hit.transform.gameObject);
                    }
                    //Exclusive Selected
                    else
                    {
                        selectedTable.deselectAll();
                        selectedTable.addSelected(hit.transform.gameObject);
                    }
                }
                //If we didn't hit something
                else
                {
                    if (Input.GetKey(KeyCode.LeftShift))
                    {
                    }
                    else
                    {
                        selectedTable.deselectAll();
                    }
                }
            }
            //If hit by the selection box
            else
            {
                verts = new Vector3[4];
                vecs  = new Vector3[4];
                int i = 0;
                currentPosition = Input.mousePosition;
                corners         = getBoundingBox(startPosition, currentPosition);

                //After getting the position of the boundingBox it will cast a ray from those positions
                foreach (Vector2 corner in corners)
                {
                    Ray ray = Camera.main.ScreenPointToRay(corner);

                    //Layermask is set to ground (1 << 8, layer 8 = ground)
                    //Should be set by Unity default
                    if (Physics.Raycast(ray, out hit, 50000.0f, (1 << 8)))
                    {
                        verts[i] = new Vector3(hit.point.x, hit.point.y, hit.point.z);
                        vecs[i]  = ray.origin - hit.point;
                        //Draws a line in the inspector
                        Debug.DrawLine(Camera.main.ScreenToWorldPoint(corner), hit.point, Color.red, 1.0f);
                    }
                    i++;
                }

                //Genrates the box collider mesh
                selectionMesh = generateSelectionMesh(verts, vecs);
                //Adds a mesh collider to the componment
                selectionBox            = gameObject.AddComponent <MeshCollider>();
                selectionBox.sharedMesh = selectionMesh;
                selectionBox.convex     = true;
                selectionBox.isTrigger  = true;

                if (!Input.GetKey(KeyCode.LeftShift))
                {
                    selectedTable.deselectAll();
                }

                Destroy(selectionBox, 0.02f);
            }
            dragSelect = false;
        }
    }