Exemple #1
0
    // Update is called once per frame
    void Update()
    {
        swipeDir = Vector2.zero;                  //フレーム毎にリセット

#if !UNITY_EDITOR && (UNITY_ANDROID || UNITY_IOS) //タッチで取得したいプラットフォームのみ
        if (Input.touchCount == 1)                //複数の指は不可とする(※2つ以上の指の場合はピンチの可能性もあるため)
#endif
        {
            if (!pressing && Input.GetMouseButtonDown(0))   //押したとき(左クリック/タッチが取得できる)
            {
                pressing  = true;
                startPos  = Input.mousePosition;
                limitTime = Time.time + timeout;
            }
            else if (pressing && Input.GetMouseButtonUp(0))  //既に押されているときのみ(※この関数は2つ以上タッチの場合、どの指か判別できないので注意)
            {
                pressing = false;

                if (Time.time < limitTime)  //時間制限前なら認識
                {
                    endPos = Input.mousePosition;

                    Vector2 dist       = endPos - startPos;
                    float   dx         = Mathf.Abs(dist.x);
                    float   dy         = Mathf.Abs(dist.y);
                    float   requiredPx = widthReference ? Screen.width * validWidth : Screen.height * validWidth;

                    if (dy < dx)             //横方向として認識
                    {
                        if (requiredPx < dx) //長さを超えていたら認識
                        {
                            swipeDir = Mathf.Sign(dist.x) < 0 ? Vector2.left : Vector2.right;
                        }
                    }
                    else                     //縦方向として認識
                    {
                        if (requiredPx < dy) //長さを超えていたら認識
                        {
                            swipeDir = Mathf.Sign(dist.y) < 0 ? Vector2.down : Vector2.up;
                        }
                    }

                    //コールバックイベント
                    if (swipeDir != Vector2.zero)
                    {
                        if (OnSwipe != null)
                        {
                            OnSwipe.Invoke(swipeDir);   //UnityEvent
                        }
                    }
                }
            }
        }
#if !UNITY_EDITOR && (UNITY_ANDROID || UNITY_IOS)   //タッチで取得したいプラットフォームのみ
        else  //タッチが1つでないときは無効にする(※2つ以上の指の場合はピンチの可能性もあるため)
        {
            pressing = false;
        }
#endif
    }
        public void OnPointerUp(PointerEventData eventData)
        {
#if !UNITY_EDITOR && (UNITY_ANDROID || UNITY_IOS) //Only platforms you want to obtain with touch.     //タッチで取得したいプラットフォームのみ
            if (Input.touchCount == 1)            //Multiple fingers are impossible (because there is a possibility of pinching in case of two or more fingers).  //複数の指は不可とする(※2つ以上の指の場合はピンチの可能性もあるため)
#endif
            {
                if (pressing)
                {
                    pressing = false;

                    if (Time.time < limitTime)  //Recognize before time limit   //時間制限前なら認識
                    {
                        endPos = Input.mousePosition;
                        Vector2 dist       = endPos - startPos;
                        float   dx         = Mathf.Abs(dist.x);
                        float   dy         = Mathf.Abs(dist.y);
                        float   requiredPx = widthReference ? Screen.width * validWidth : Screen.height * validWidth;

                        if (dy < dx)             //Recognized as horizontal direction            //横方向として認識
                        {
                            if (requiredPx < dx) //Recognize if it exceeds length     //長さを超えていたら認識
                            {
                                swipeDir = Mathf.Sign(dist.x) < 0 ? Vector2.left : Vector2.right;
                            }
                        }
                        else                     //Recognized as vertical direction                      //縦方向として認識
                        {
                            if (requiredPx < dy) //Recognize if it exceeds length     //長さを超えていたら認識
                            {
                                swipeDir = Mathf.Sign(dist.y) < 0 ? Vector2.down : Vector2.up;
                            }
                        }

                        if (swipeDir != Vector2.zero)
                        {
                            if (OnSwipe != null)
                            {
                                OnSwipe.Invoke(swipeDir);
                            }
                        }
                    }
                }
            }
#if !UNITY_EDITOR && (UNITY_ANDROID || UNITY_IOS)   //タッチで取得したいプラットフォームのみ
            else  //Invalid it when there is not one touch (since there is also a possibility of pinching in case of two or more fingers).  //タッチが1つでないときは無効にする(※2つ以上の指の場合はピンチの可能性もあるため)
            {
                pressing = false;
            }
#endif
        }
        // Update is called once per frame
        void Update()
        {
            Direction = Vector2.zero;             //Reset per frame   //フレーム毎にリセット

#if !UNITY_EDITOR && (UNITY_ANDROID || UNITY_IOS) //Only platforms you want to obtain with touch.     //タッチで取得したいプラットフォームのみ
            if (Input.touchCount == 1)            //Multiple fingers are impossible (because there is a possibility of pinching in case of two or more fingers).  //複数の指は不可とする(※2つ以上の指の場合はピンチの可能性もあるため)
#endif
            {
                if (!pressing && Input.GetMouseButtonDown(0))   //When pressed (left click / touch can be acquired).    //押したとき(左クリック/タッチが取得できる)
                {
                    startPos = Input.mousePosition;
                    if (validArea.xMin * Screen.width <= startPos.x && startPos.x <= validArea.xMax * Screen.width &&
                        validArea.yMin * Screen.height <= startPos.y && startPos.y <= validArea.yMax * Screen.height)   //Within recognition area   //認識エリア内
                    {
                        pressing  = true;
                        limitTime = Time.time + timeout;
                    }
                }
                else if (pressing && Input.GetMouseButtonUp(0))  //Only when it is already pressed (Note that this function can not distinguish which finger when touching 2 or more).   //既に押されているときのみ(※この関数は2つ以上タッチの場合、どの指か判別できないので注意)
                {
                    pressing = false;

                    if (Time.time < limitTime)  //Recognize before time limit   //時間制限前なら認識
                    {
                        endPos = Input.mousePosition;
                        Vector2 dist       = endPos - startPos;
                        float   dx         = Mathf.Abs(dist.x);
                        float   dy         = Mathf.Abs(dist.y);
                        float   requiredPx = widthReference ? Screen.width * validWidth : Screen.height * validWidth;

                        if (dy < dx)             //Recognized as horizontal direction            //横方向として認識
                        {
                            if (requiredPx < dx) //Recognize if it exceeds length     //長さを超えていたら認識
                            {
                                Direction = Mathf.Sign(dist.x) < 0 ? Vector2.left : Vector2.right;
                            }
                        }
                        else                     //Recognized as vertical direction                      //縦方向として認識
                        {
                            if (requiredPx < dy) //Recognize if it exceeds length     //長さを超えていたら認識
                            {
                                Direction = Mathf.Sign(dist.y) < 0 ? Vector2.down : Vector2.up;
                            }
                        }

                        if (Direction != Vector2.zero)
                        {
                            if (OnSwipe != null)
                            {
                                OnSwipe.Invoke(Direction);
                            }
                        }
                    }
                }
            }
#if !UNITY_EDITOR && (UNITY_ANDROID || UNITY_IOS)   //タッチで取得したいプラットフォームのみ
            else  //Invalid it when there is not one touch (since there is also a possibility of pinching in case of two or more fingers).  //タッチが1つでないときは無効にする(※2つ以上の指の場合はピンチの可能性もあるため)
            {
                pressing = false;
            }
#endif
        }
Exemple #4
0
        // Update is called once per frame
        void Update()
        {
            swipeDir = Vector2.zero;              //Reset per frame

#if !UNITY_EDITOR && (UNITY_ANDROID || UNITY_IOS) //Only platforms you want to obtain with touch
            if (Input.touchCount == 1)
#endif
            {
                if (!pressing && Input.GetMouseButtonDown(0))
                {
                    startPos = Input.mousePosition;
                    if (validArea.xMin * Screen.width <= startPos.x && startPos.x <= validArea.xMax * Screen.width &&
                        validArea.yMin * Screen.height <= startPos.y && startPos.y <= validArea.yMax * Screen.height)
                    {
                        pressing  = true;
                        limitTime = Time.time + timeout;
                    }
                }
                else if (pressing && Input.GetMouseButtonUp(0))
                {
                    pressing = false;

                    if (Time.time < limitTime)
                    {
                        endPos = Input.mousePosition;
                        Vector2 dist       = endPos - startPos;
                        float   dx         = Mathf.Abs(dist.x);
                        float   dy         = Mathf.Abs(dist.y);
                        float   requiredPx = widthReference ? Screen.width * validWidth : Screen.height * validWidth;

                        if (dy < dx)
                        {
                            if (requiredPx < dx)
                            {
                                swipeDir = Mathf.Sign(dist.x) < 0 ? Vector2.left : Vector2.right;
                            }
                        }
                        else
                        {
                            if (requiredPx < dy)
                            {
                                swipeDir = Mathf.Sign(dist.y) < 0 ? Vector2.down : Vector2.up;
                            }
                        }

                        if (swipeDir != Vector2.zero)
                        {
                            if (OnSwipe != null)
                            {
                                OnSwipe.Invoke(swipeDir);
                            }
                        }
                    }
                }
            }
#if !UNITY_EDITOR && (UNITY_ANDROID || UNITY_IOS)   //Only platforms you want to obtain with touch
            else
            {
                pressing = false;
            }
#endif
        }