Пример #1
0
    // used for treating mouse-clicks as touchs
    private void HandleTouch(int touchFingerId, Vector3 touchPosition, TouchPhase touchPhase)
    {
        // deltaTime in seconds -- not the way to handle, need time and position
        bool beginTouch = touchPhase.Equals(TouchPhase.Began);



        if (beginTouch)
        {
            Vector2 touchPosition2D = touchPosition;

            Debug.DrawRay(Vector3.zero, touchPosition, Color.blue, 1.0f);

            // have to check all hits to make sure bounding box isn't the only passed
            RaycastHit2D[] allRayHits = Physics2D.RaycastAll(touchPosition2D, new Vector3(0, 0, -10));


            // TODO: add enemy objects that shouldn't be hit and reduce points
            foreach (RaycastHit2D rayHit in allRayHits)
            {
                // need the tag check to avoid passing the bounding box
                if (rayHit && rayHit.transform.gameObject.tag.Equals("Ball"))
                {
                    // get the ball that was hit
                    GameObject ball = rayHit.transform.gameObject;


                    Splits.split(ball, touchPosition2D, ballsCreated);
                }
            }
        }
    }
Пример #2
0
    void Jump()
    {
#if UNITY_STANDALONE || UNITY_EDITOR || UNITY_WEBPLAYER
        if (Input.GetButtonDown("Jump") || Input.GetMouseButtonDown(0))
        {
            rb.velocity = Vector2.up * jumpForce;
            playerAnimator.SetTrigger("isJump"); //跳跃身体动画
            eyesAnimator.SetTrigger("isJump");   //跳跃眼睛动画
            jumpSound.Play();
        }
#endif

#if UNITY_ANDROID
        if (Input.touchCount > 0)
        {
            Vector2    TouchLoc = Input.GetTouch(0).position;
            TouchPhase TPhase   = Input.GetTouch(0).phase;
            RaycastHit Hitinfo;
            if (TPhase.Equals(TouchPhase.Began))
            {
                Ray ray = Camera.main.ScreenPointToRay(TouchLoc);
                Physics.Raycast(ray, out Hitinfo);
                Hitinfo.collider.gameObject.SendMessage("OnMouseDown");
            }

            if (Time.timeScale != 0)
            {
                rb.velocity = Vector2.up * jumpForce;
                playerAnimator.SetTrigger("isJump"); //跳跃身体动画
                eyesAnimator.SetTrigger("isJump");   //跳跃眼睛动画
                jumpSound.Play();
            }
        }
#endif
    }
Пример #3
0
    private void Update()
    {
#if UNITY_ANDROID
        if (Input.touchCount > 0)
        {
            Vector2    TouchLoc = Input.GetTouch(0).position;
            TouchPhase TPhase   = Input.GetTouch(0).phase;
            RaycastHit Hitinfo;
            if (TPhase.Equals(TouchPhase.Began))
            {
                Ray ray = Camera.main.ScreenPointToRay(TouchLoc);
                Physics.Raycast(ray, out Hitinfo);
                Hitinfo.collider.gameObject.SendMessage("OnMouseDown");
            }
        }
#endif
    }
Пример #4
0
    // Update is called once per frame
    void Update()
    {
        if (Input.touchCount > 0)
        {
            Vector2 TouchPosition = Input.GetTouch(0).position;

            TouchPhase TPhase = Input.GetTouch(0).phase;

            if (TPhase.Equals(TouchPhase.Began))
            {
                Ray R = Camera.main.ScreenPointToRay(TouchPosition);

                RaycastHit HitInfo;

                if (Physics.Raycast(R, out HitInfo))
                {
                    HitInfo.collider.gameObject.SendMessage("Touch area");
                }
            }
        }

        /*foreach(Touch touch in Input.touches)
         * {
         *
         *      ray = Camera.main.ScreenPointToRay(touch.position);
         *      RaycastHit hit;
         *
         *      Pos2 = touch.position;
         *
         *      if(Physics.Raycast(ray, out hit))
         *      {
         *              Debug.Log("Enable Ray");
         *              Pos = Pos2;
         *              Debug.Log(hit.collider.tag);
         *      }
         * }*/
    }
Пример #5
0
    // Update is called once per frame
    void Update()
    {
        if (INPUT_DEBUG)
        {
            if (isdown)
            {
                bool down = Input.GetMouseButton(0);
                if (!down)
                {
                    //Debug.Log ("released!!!");
                    isdown = false;
                    GameEvent releaseEvent = new GameEvent("mouse_release");
                    releaseEvent.addArgument(GetInputPosition());
                    EventManager.Instance().RaiseEvent(releaseEvent);
                    if (tapLengthWatcher.TimeElapsedMillis() < 200)
                    {
                        GameEvent tapEvent = new GameEvent("tap");
                        tapEvent.addArgument(GetInputPosition());
                        EventManager.Instance().RaiseEvent(tapEvent);
                    }
                }
            }
            else
            {
                bool down = Input.GetMouseButton(0);
                if (down)
                {
                    isdown = true;
                    GameEvent clickEvent = new GameEvent("mouse_click");
                    clickEvent.addArgument(GetInputPosition());
                    EventManager.Instance().RaiseEvent(clickEvent);
                    tapLengthWatcher.Restart();
                }
            }
        }
        else
        {
            for (int i = 0; i < Input.touchCount; i++)
            {
                Touch      t  = Input.touches[i];
                TouchPhase tp = lastPhase;
                if (t.fingerId != 0)
                {
                    tp = altPhases[t.fingerId - 1];
                }

                switch (t.phase)
                {
                case TouchPhase.Began:
                    if (tp.Equals(TouchPhase.Began))
                    {
                        break;                         //avoid double touch events
                    }
                    if (t.fingerId == 0)
                    {
                        GameEvent clickEvent = new GameEvent("mouse_click");
                        clickEvent.addArgument(GetInputPosition());
                        EventManager.Instance().RaiseEvent(clickEvent);
                        lastPhase = TouchPhase.Began;
                        //start timer for taps
                        tapLengthWatcher.Restart();
                    }
                    else
                    {
                        GameEvent clickEvent = new GameEvent("alt_click");
                        clickEvent.addArgument(GetInputPosition(t.fingerId));
                        clickEvent.addArgument(t.fingerId);
                        EventManager.Instance().RaiseEvent(clickEvent);
                        altPhases[t.fingerId - 1] = TouchPhase.Began;
                        //start timer for taps
                        altWatchers[t.fingerId - 1].Restart();
                        break;
                    }
                    break;

                case TouchPhase.Ended:
                    if (tp.Equals(TouchPhase.Ended) || tp.Equals(TouchPhase.Canceled))
                    {
                        break;                         //avoid double touch events
                    }
                    if (t.fingerId == 0)
                    {
                        GameEvent clickEvent2 = new GameEvent("mouse_release");
                        clickEvent2.addArgument(GetInputPosition());
                        EventManager.Instance().RaiseEvent(clickEvent2);
                        lastPhase = TouchPhase.Ended;
                        if (tapLengthWatcher.TimeElapsedMillis() < 200)
                        {
                            GameEvent tapEvent = new GameEvent("tap");
                            tapEvent.addArgument(GetInputPosition());
                            EventManager.Instance().RaiseEvent(tapEvent);
                        }
                    }
                    else
                    {
                        GameEvent clickEvent2 = new GameEvent("alt_release");
                        clickEvent2.addArgument(GetInputPosition(t.fingerId));
                        clickEvent2.addArgument(t.fingerId);
                        EventManager.Instance().RaiseEvent(clickEvent2);
                        altPhases[t.fingerId - 1] = TouchPhase.Ended;
                        if (altWatchers[t.fingerId - 1].TimeElapsedMillis() < 200)
                        {
                            GameEvent tapEvent = new GameEvent("alt_tap");
                            tapEvent.addArgument(GetInputPosition(t.fingerId));
                            tapEvent.addArgument(t.fingerId);
                            EventManager.Instance().RaiseEvent(tapEvent);
                        }
                    }
                    break;

                case TouchPhase.Canceled:
                    if (tp.Equals(TouchPhase.Ended) || tp.Equals(TouchPhase.Canceled))
                    {
                        break;                         //avoid double touch events
                    }
                    if (t.fingerId == 0)
                    {
                        GameEvent clickEvent2 = new GameEvent("mouse_release");
                        clickEvent2.addArgument(GetInputPosition());
                        EventManager.Instance().RaiseEvent(clickEvent2);
                        lastPhase = TouchPhase.Ended;
                        if (tapLengthWatcher.TimeElapsedMillis() < 200)
                        {
                            GameEvent tapEvent = new GameEvent("tap");
                            tapEvent.addArgument(GetInputPosition());
                            EventManager.Instance().RaiseEvent(tapEvent);
                        }
                    }
                    else
                    {
                        GameEvent clickEvent2 = new GameEvent("alt_release");
                        clickEvent2.addArgument(GetInputPosition(t.fingerId));
                        clickEvent2.addArgument(t.fingerId);
                        EventManager.Instance().RaiseEvent(clickEvent2);
                        altPhases[t.fingerId - 1] = TouchPhase.Ended;
                        if (altWatchers[t.fingerId - 1].TimeElapsedMillis() < 200)
                        {
                            GameEvent tapEvent = new GameEvent("alt_tap");
                            tapEvent.addArgument(GetInputPosition(t.fingerId));
                            tapEvent.addArgument(t.fingerId);
                            EventManager.Instance().RaiseEvent(tapEvent);
                        }
                    }
                    break;

                default:
                    break;
                }
            }

            /*if (main.fingerId == 0) {
             *      switch(main.phase){
             *              case TouchPhase.Began:
             *              if(lastPhase.Equals(TouchPhase.Began))
             *                      break; //avoid double touch events
             *              GameEvent clickEvent = new GameEvent ("mouse_click");
             *              clickEvent.addArgument (GetInputPosition ());
             *              EventManager.Instance ().RaiseEvent (clickEvent);
             *              lastPhase = TouchPhase.Began;
             *              //start timer for taps
             *              tapLengthWatcher.Restart();
             *              break;
             *              case TouchPhase.Ended:
             *              if(lastPhase.Equals(TouchPhase.Ended) || lastPhase.Equals (TouchPhase.Canceled))
             *                      break; //avoid double touch events
             *              GameEvent clickEvent2 = new GameEvent ("mouse_release");
             *              clickEvent2.addArgument (GetInputPosition ());
             *              EventManager.Instance ().RaiseEvent (clickEvent2);
             *              lastPhase = TouchPhase.Ended;
             *              if(tapLengthWatcher.TimeElapsedMillis() < 200){
             *                      GameEvent tapEvent = new GameEvent("tap");
             *                      tapEvent.addArgument(GetInputPosition());
             *                      EventManager.Instance().RaiseEvent(tapEvent);
             *              }
             *              break;
             *              case TouchPhase.Canceled:
             *              if(lastPhase.Equals(TouchPhase.Ended) || lastPhase.Equals (TouchPhase.Canceled))
             *                      break; //avoid double touch events
             *              GameEvent clickEvent3 = new GameEvent ("mouse_release");
             *              clickEvent3.addArgument (GetInputPosition ());
             *              EventManager.Instance ().RaiseEvent (clickEvent3);
             *              lastPhase = TouchPhase.Canceled;
             *              if(tapLengthWatcher.TimeElapsedMillis() < 200){
             *                      GameEvent tapEvent = new GameEvent("tap");
             *                      tapEvent.addArgument(GetInputPosition());
             *                      EventManager.Instance().RaiseEvent(tapEvent);
             *              }
             *              break;
             *              default:
             *              break;
             *      }
             *
             * }
             * //next handle further touches
             * //Debug.Log ("touchcount: " + Input.touchCount);
             * for(int i = 2; i <= Input.touchCount; i++){
             *
             *      Touch current = Input.GetTouch(i-1);
             *      switch(current.phase){
             *              case TouchPhase.Began:
             *                      if(altPhases[i-2].Equals(TouchPhase.Began))
             *                              break; //avoid double touch events
             *                      GameEvent clickEvent = new GameEvent ("alt_click");
             *                      clickEvent.addArgument (GetInputPosition (i-1));
             *                      clickEvent.addArgument(i-1);
             *                      EventManager.Instance ().RaiseEvent (clickEvent);
             *                      //Debug.Log("alt click generated");
             *                      altPhases[i-2] = TouchPhase.Began;
             *                      //start timer for taps
             *                      altWatchers[i-2].Restart();
             *                      break;
             *              case TouchPhase.Ended:
             *                      if(altPhases[i-2].Equals(TouchPhase.Ended) || altPhases[i-2].Equals (TouchPhase.Canceled))
             *                              break; //avoid double touch events
             *                      GameEvent clickEvent2 = new GameEvent ("alt_release");
             *                      clickEvent2.addArgument (GetInputPosition (i-1));
             *                      clickEvent2.addArgument(i-1);
             *                      EventManager.Instance ().RaiseEvent (clickEvent2);
             *                      altPhases[i-2] = TouchPhase.Ended;
             *                      if(altWatchers[i-2].TimeElapsedMillis() < 200){
             *                              GameEvent tapEvent = new GameEvent("alt_tap");
             *                              tapEvent.addArgument(GetInputPosition(i-1));
             *                              tapEvent.addArgument(i-1);
             *                              EventManager.Instance().RaiseEvent(tapEvent);
             *                      }
             *                      break;
             *              case TouchPhase.Canceled:
             *                      if(altPhases[i-2].Equals(TouchPhase.Ended) || altPhases[i-2].Equals (TouchPhase.Canceled))
             *                              break; //avoid double touch events
             *                      GameEvent clickEvent3 = new GameEvent ("alt_release");
             *                      clickEvent3.addArgument (GetInputPosition (i-1));
             *                      clickEvent3.addArgument(i-1);
             *                      EventManager.Instance ().RaiseEvent (clickEvent3);
             *                      altPhases[i-2] = TouchPhase.Canceled;
             *                      if(altWatchers[i-2].TimeElapsedMillis() < 200){
             *                              GameEvent tapEvent = new GameEvent("alt_tap");
             *                              tapEvent.addArgument(GetInputPosition(i-1));
             *                              tapEvent.addArgument(i-1);
             *                              EventManager.Instance().RaiseEvent(tapEvent);
             *                      }
             *                      break;
             *              default:
             *                      break;
             *      }
             * }*/
        }
    }