private void Update() { if (Input.touchCount > 0) { // Debug.Log("Touching"); Touch touch = Input.GetTouch(0); if (touch.phase == TouchPhase.Began) { fingerDown = touch; fingerUp = touch; onFingerDownTouchables = GetTouchablesFromPoint(fingerDown.position); onFingerDownTime = Time.time; } else if (touch.phase == TouchPhase.Ended) { fingerUp = touch; onFingerUpTime = Time.time; if (touchSettings.requireLiftInCollider) { HashSet <ITouchable> onFingerUpTouchables = GetTouchablesFromPoint(fingerUp.position); onFingerDownTouchables.IntersectWith(onFingerUpTouchables); } foreach (ITouchable touchable in onFingerDownTouchables) { if (IsTap(fingerDown.position, fingerUp.position)) { ITappable tap = (touchable as ITappable); if (tap != null) { tap.OnTap(); } } else if (IsSwipe(fingerDown.position, fingerUp.position)) { ISwipeable swipe = (touchable as ISwipeable); if (swipe != null) { swipe.OnSwipe(new SwipeInfo(fingerDown, fingerUp, onFingerUpTime - onFingerDownTime)); } } } } } }
} // ends CheckTouch() // #endif /// <summary> /// Sends the OnClick message to anything under the input position. /// </summary> /// <param name="inputPosition">It's the screen position to check for input and send a message to.</param> private void CheckInput(Vector2 inputPosition) { //convert the mouse position to the world space Ray mouseCursorRay = my_camera.ScreenPointToRay(inputPosition); //declare a RaycastHit object to recieve our results RaycastHit hitInfo; //performs the actual raycast using the ray information and outputting hitInfo if (Physics.Raycast(mouseCursorRay, out hitInfo) == true) { //find out what we hit Collider objectWeHit = hitInfo.collider; ITappable tappableScript = objectWeHit.GetComponent <ITappable>(); if (tappableScript != null) { tappableScript.OnTap(hitInfo.point); Debug.Log("tap heard"); } // return objectWeHit.name; // log the name of the object that was touched // Debug.Log(objectWeHit.name); // if-else-ifs to check which object was clicked and act accordingly // use a switch statement for the same thing // attach a script to the touchable object and call out to it //calls the function hideCard() on every script on the object if it exists // objectWeHit.SendMessage("OnClick", SendMessageOptions.DontRequireReceiver); // objectWeHit.SendMessage("AddToPile", SendMessageOptions.DontRequireReceiver); } }
private void Update() { if (!allowInputs) { return; } #if UNITY_EDITOR || UNITY_STANDALONE if (Input.GetMouseButtonDown(0)) { initialInputScreenPos = Input.mousePosition; currentInputScreenPos = initialInputScreenPos; touchedCol = null; justTouched = true; touching = true; } else if (Input.GetMouseButton(0)) { currentInputScreenPos = Input.mousePosition; } if (Input.GetMouseButtonUp(0)) { touching = false; justReleased = true; } #elif UNITY_ANDROID || UNITY_IOS if (Input.touchCount > 0) { initialTouch = Input.touches[0]; } if (initialTouch.phase == TouchPhase.Began) { initialInputScreenPos = initialTouch.position; currentInputScreenPos = initialInputScreenPos; touchedCol = null; justTouched = true; touching = true; } else if (initialTouch.phase == TouchPhase.Moved) { currentInputScreenPos = initialTouch.position; } if (initialTouch.phase == TouchPhase.Ended && touching) { touching = false; justReleased = true; } #endif if (justTouched) { justTouched = false; Ray ray = MainCamera.ScreenPointToRay(initialInputScreenPos); RaycastHit2D hit = Physics2D.GetRayIntersection(ray, MainCamera.farClipPlane, raycastMask); if (hit.collider != null) { touchedCol = hit.collider; tappable = touchedCol.GetComponent <ITappable>(); if (tappable != null) { tappable.OnTouch(); } } } if (justReleased) { justReleased = false; if (touchedCol != null) { Ray ray = MainCamera.ScreenPointToRay(currentInputScreenPos); compareCol = Physics2D.GetRayIntersection(ray, MainCamera.farClipPlane, raycastMask).collider; bool inBounds = false; if (compareCol != null) { if (touchedCol == compareCol) { inBounds = true; } } if (tappable != null) { tappable.OnTap(inBounds); } } } if (touching) { cameraControls.UpdateTarget(MainCamera.ScreenToWorldPoint(initialInputScreenPos) - MainCamera.ScreenToWorldPoint(currentInputScreenPos)); } }
// #endif private void CheckTouch(Touch touchInfo) { // Extract the position of that finger's touch. Vector2 touchPosition = touchInfo.position; // Convert the mouse position to world space Ray mouseCursorRay = my_camera.ScreenPointToRay(touchPosition); // Declare a RaycastHit object to recieve our results RaycastHit hitInfo; // perform the actual raycast if (Physics.Raycast(mouseCursorRay, out hitInfo) == true) { Collider objectWeHit = hitInfo.collider; Debug.Log(objectWeHit); if (objectWeHit.tag == "circle") { Debug.Log("we hit the circle"); objectWeHit.SendMessage("OnClick", SendMessageOptions.DontRequireReceiver); objectWeHit.SendMessage("AddToPile", SendMessageOptions.DontRequireReceiver); } // extract the touch phase TouchPhase touchPhase = touchInfo.phase; switch (touchPhase) { case TouchPhase.Began: // Debug.Log("touch began"); break; case TouchPhase.Moved: // search the object we hit for any script that implements IDraggable IDraggable draggableScript = objectWeHit.GetComponent <IDraggable>(); // NOTE: GetComponent returns null if no matches if (draggableScript != null) { // Debug.Log("touch phase moved heard"); // call OnDrag() on whatever script we found draggableScript.OnDrag(hitInfo.point, touchInfo.deltaPosition); } break; case TouchPhase.Stationary: { // Debug.Log("touch phase stationary"); // search the object we hit for any script that implements ITappable ITappable tappableScript = objectWeHit.GetComponent <ITappable>(); if (tappableScript != null) { tappableScript.OnTap(hitInfo.point); } break; } case TouchPhase.Ended: // Debug.Log("Touch phase ended"); break; } // ends switch } // ends if RaycastHit } // ends CheckTouch()