コード例 #1
0
    void Update()
    {
        if (Input.GetMouseButtonDown((int)MouseButton.RightMouse))
        {
            Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);

            Vector2 mousePos2D = new Vector2(mousePos.x, mousePos.y);

            //RaycastHit2D hit = Physics2D.Raycast(mousePos2D, Vector3.up,1,LayerMask.NameToLayer("Equipment"));
            RaycastHit2D hit = Physics2D.Raycast(mousePos2D, Vector2.zero);

            if (hit.collider != null)
            {
                Clickable e = hit.collider.GetComponent <Clickable>();
                if (e != null)
                {
                    e.Clicked(MouseButton.RightMouse);
                }
            }
        }
        if (Input.GetMouseButtonDown((int)MouseButton.LeftMouse))
        {
            Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);

            Vector2 mousePos2D = new Vector2(mousePos.x, mousePos.y);

            //RaycastHit2D hit = Physics2D.Raycast(mousePos2D, Vector3.up,1,LayerMask.NameToLayer("Equipment"));
            RaycastHit2D hit = Physics2D.Raycast(mousePos2D, Vector2.zero);

            if (hit.collider != null)
            {
                Clickable e = hit.collider.GetComponent <Clickable>();
                if (e != null)
                {
                    e.Clicked(MouseButton.LeftMouse);
                }
            }
        }
    }
コード例 #2
0
ファイル: Click.cs プロジェクト: OnkelDolf/Project6354
    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0) && !EventSystem.current.IsPointerOverGameObject())
        {
            //startPos = new Vector3();
            startPos = Camera.main.ScreenToViewportPoint(Input.mousePosition);

            RaycastHit rayHit;

            if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out rayHit, Mathf.Infinity, clickablesLayer))
            {
                Clickable clickableScript = rayHit.collider.GetComponent <Clickable>();

                if (Input.GetKey("left shift"))
                {
                    if (clickableScript.isSelected == false)
                    {
                        selectedObjects.Add(rayHit.collider.gameObject);
                        clickableScript.isSelected = true;
                        clickableScript.Clicked();
                    }
                    else
                    {
                        selectedObjects.Remove(rayHit.collider.gameObject);
                        clickableScript.isSelected = false;
                        clickableScript.Clicked();
                    }
                }
                else
                {
                    ClearSelected();

                    selectedObjects.Add(rayHit.collider.gameObject);
                    clickableScript.isSelected = true;
                    clickableScript.Clicked();
                }
            }
            else
            {
                foreach (GameObject obj in selectedObjects)
                {
                    obj.GetComponent <Clickable>().isSelected = false;
                    obj.GetComponent <Clickable>().Clicked();
                }

                selectedObjects.Clear();
            }

            if (CheckSelectionForNonNode())
            {
                buildUI.SetActive(false);
                buildManagerUI.SetActive(true);
            }
            else
            {
                buildManagerUI.SetActive(false);
                buildUI.SetActive(true);
            }
        }

        if (Input.GetMouseButtonUp(0) /*&& !EventSystem.current.IsPointerOverGameObject()*/) // TODO: Fix this so the UI doesn't spass out.
        {
            /*
             * if (EventSystem.current.currentSelectedGameObject == box && !EventSystem.current.IsPointerOverGameObject())
             * {
             *  endPos = Camera.main.ScreenToViewportPoint(Input.mousePosition);
             *
             *  if (startPos != endPos)
             *  {
             *      SelectObjects();
             *  }
             * }
             * else if (EventSystem.current.currentSelectedGameObject != box && EventSystem.current.IsPointerOverGameObject())
             * {
             *  endPos = Camera.main.ScreenToViewportPoint(Input.mousePosition);
             *
             *  if (startPos != endPos)
             *  {
             *      SelectObjects();
             *  }
             * }
             */
        }
    }