コード例 #1
0
        void Update()
        {
            if (Input.GetMouseButton(0))
            {
                touching = true;
            }


            Vector2 pos;
            Touch   touch = new Touch();

            if (Input.touchCount > 0)
            {
                touch    = Input.GetTouch(0);
                pos      = touch.position;
                touching = true;
            }
            pos = Input.mousePosition;

            TouchedScreenPosMoved = (touch.phase != TouchPhase.Began || !Input.GetMouseButtonDown(0)) ? (lastTouchedScreenPos - pos) : Vector2.zero;
            lastTouchedScreenPos  = pos;
            Vector3 touchedPos = Camera.main.ScreenToWorldPoint(new Vector3(pos.x, pos.y, 100000));


            if ((Input.touchCount > 0 && touch.phase == TouchPhase.Began) || Input.GetMouseButtonDown(0))
            {
                if (startTouch != null)
                {
                    startTouch.Invoke(pos);
                }

                Ray          touchRay = new Ray(Vector3.forward, touchedPos);
                RaycastHit2D hit      = Physics2D.Raycast(touchedPos, Vector2.zero, Mathf.Infinity, tappableMask);

                if (hit)
                {
                    Transform  checking  = hit.transform;
                    IClickable clickable = checking.GetComponent <IClickable>();;
                    while (clickable == null && checking.parent != null)
                    {
                        checking  = checking.parent;
                        clickable = checking.GetComponent <IClickable>();
                    }


                    if (clickable != null)
                    {
                        clickable.Click();
                    }
                }

                if (dragging && draggingObject == null)
                {
                    hit = Physics2D.Raycast(touchedPos, Vector2.zero, 10, draggableMask);
                    if (hit)
                    {
                        IDraggable draggable = hit.collider.GetComponent <IDraggable>();
                        if (draggable != null)
                        {
                            draggingObject = draggable;
                            draggingObject.Pick();
                        }
                    }
                }
            }


            if ((Input.touchCount > 0 && touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled) || Input.GetMouseButtonUp(0))
            {
                touching = false;
                if (dragging && draggingObject != null)
                {
                    draggingObject.Drop();
                    draggingObject = null;
                }
            }

            if (dragging && draggingObject != null)
            {
                draggingObject.UpdatePos(touchedPos);
            }

            if (Input.touchCount > 0 && (int)Input.GetTouch(0).phase > 2 && (int)Input.GetTouch(1).phase > 2)
            {
                Debug.Log("bep");
                if ((Input.GetTouch(0).position - Input.GetTouch(1).position).magnitude / Screen.width > MinInversePinchDist)
                {
                    Debug.Log("bop");
                    inversePinch.Invoke();
                }
            }
        }