Beispiel #1
0
        private void Update()
        {
            if (collectInput)
            {
                //If running game in editor
#if UNITY_EDITOR
                //If mouse button 0 is down
                if (Input.GetMouseButton(0))
                {
                    Vector2 mouseCache = Input.mousePosition;
                    if (!dragging)
                    {
                        dragBeginPos = mouseCache;
                        dragging     = true;
                    }

                    //claculate the length of the current drag
                    dragLength = (dragBeginPos.x - mouseCache.x) * -1;

                    //Drag is long enough to switch to the next level
                    if (Mathf.Abs(dragLength) >= screenWidth * minScreenWidthPercentToSwitch)
                    {
                        //drag to the right
                        if (dragLength > 0)
                        {
                            if (!UILevelselectionManager.LastLevel())
                            {
                                StartCoroutine(lerpBackToOrigin());
                                SoundManager.PlayUnvalidSound();
                                UILevelselectionManager.StarShake();
                            }
                            else
                            {
                                onBounceBack.Invoke();
                            }
                        }
                        //drag to the left
                        else
                        {
                            if (!UILevelselectionManager.NextLevel())
                            {
                                StartCoroutine(lerpBackToOrigin());
                                SoundManager.PlayUnvalidSound();
                                UILevelselectionManager.StarShake();
                            }
                            else
                            {
                                onBounceBack.Invoke();
                            }
                        }
                    }
                    newDragObjectPos = new Vector3(defaultPosition.x + dragLength, defaultPosition.y, defaultPosition.z);
                    //Debug.Log(dragLength + " " + originDragObjectPos.x);
                    touched = true;
                }
                else if (Input.GetMouseButtonUp(0))
                {
                    Vector2 mouseCache = Input.mousePosition;
                    dragging = false;

                    if (dragLength <= screenWidth * minScreenWidthPercentToSwitch)
                    {
                        StartCoroutine(lerpBackToOrigin());
                        if (dragLength > 20 || dragLength < -20)
                        {
                            onBounceBack.Invoke();
                        }
                    }
                }
#endif
                //If a touch is detected
                if (Input.touchCount >= 1)
                {
                    Touch   touch      = Input.touches[0];
                    Vector2 touchCache = touch.position;

                    //finger is on screen
                    if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
                    {
                        if (!dragging)
                        {
                            dragBeginPos = touchCache;
                            dragging     = true;
                        }

                        //claculate the length of the current drag
                        dragLength = (dragBeginPos.x - touchCache.x) * -1;

                        //Drag is long enough to switch to the next level
                        if (Mathf.Abs(dragLength) >= screenWidth * minScreenWidthPercentToSwitch)
                        {
                            //drag to the right
                            if (dragLength > 0)
                            {
                                if (!UILevelselectionManager.LastLevel())
                                {
                                    StartCoroutine(lerpBackToOrigin());
                                    SoundManager.PlayUnvalidSound();
                                    UILevelselectionManager.StarShake();
                                }
                                else
                                {
                                    onBounceBack.Invoke();
                                }
                            }
                            //drag to the left
                            else
                            {
                                if (!UILevelselectionManager.NextLevel())
                                {
                                    StartCoroutine(lerpBackToOrigin());
                                    SoundManager.PlayUnvalidSound();
                                    UILevelselectionManager.StarShake();
                                }
                                else
                                {
                                    onBounceBack.Invoke();
                                }
                            }
                        }
                        newDragObjectPos = new Vector3(defaultPosition.x + dragLength, defaultPosition.y, defaultPosition.z);
                        touched          = true;
                    }

                    //finger got released
                    else if (touch.phase == TouchPhase.Ended)
                    {
                        dragging = false;

                        if (dragLength <= screenWidth * minScreenWidthPercentToSwitch)
                        {
                            StartCoroutine(lerpBackToOrigin());
                            if (dragLength > 20 || dragLength < -20)
                            {
                                onBounceBack.Invoke();
                            }
                        }
                    }
                }
            }
        }