public bool DetectClosestFingerTouch(int FingerNum) { //DetectClosestFingerTouch in a certain distance Touch[] allTouches = Input.touches; Touch[] closestTouchs = new Touch[FingerNum]; if (Input.touchCount >= FingerNum) { float[] distance = new float[Input.touchCount]; for (int i = 0; i < Input.touchCount; i++) { Touch touch = Input.touches[i]; //Debug.Log(string.Format("detected the {0} finger", i)); //worldCamera:1.screenSpace-Camera //canvas.GetComponent<Camera>() 1.ScreenSpace -Overlay if (RenderMode.ScreenSpaceCamera == canvas.renderMode) { RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRectTransform, touch.position, canvas.worldCamera, out pos); } else if (RenderMode.ScreenSpaceOverlay == canvas.renderMode) { RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRectTransform, touch.position, _camera, out pos); } else if (RenderMode.WorldSpace == canvas.renderMode) { RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRectTransform, touch.position, Camera.main, out pos); } else { Debug.Log("Please choose right Camera RenderMode!"); } distance[i] = Vector2.Distance(rectTransform.anchoredPosition, pos); //Debug.Log(string.Format("distance of the {0} finger from selected cases {2} is {1}", i + 1, distance[i], this.gameObject.name)); } if (FingerNum == 2) { int[] indexs = new int[FingerNum]; float[] mindistance = FindMinumFromArray(distance, FingerNum, out indexs); for (int i = 0; i < FingerNum; i++) { //Debug.Log(mindistance[i]); if (mindistance[i] > 2f) { return(false); } closestTouchs[i] = Input.touches[indexs[i]]; } DetectClosestTouchMovement.Calculate(closestTouchs); return(true); } } return(false); }
public bool DetectClosestFingerTouch(int FingerNum) { //DetectClosestFingerTouch in a certain distance Touch[] allTouches = Input.touches; Touch[] closestTouchs = new Touch[FingerNum]; if (Input.touchCount >= FingerNum) { float[] distance = new float[Input.touchCount]; for (int i = 0; i < Input.touchCount; i++) { Touch touch = Input.touches[i]; Ray ray = Camera.main.ScreenPointToRay(touch.position); RaycastHit hitInfo; if (Physics.Raycast(ray, out hitInfo)) { Vector2 hitVector2 = new Vector2(hitInfo.point.x, hitInfo.point.z); Vector2 caseVector2 = new Vector2(transform.position.x, transform.position.z); distance[i] = Vector2.Distance(caseVector2, hitVector2); } } if (FingerNum == 2) { int[] indexs = new int[FingerNum]; float[] mindistance = FindMinumFromArray(distance, FingerNum, out indexs); for (int i = 0; i < FingerNum; i++) { //Debug.Log(mindistance[i]); if (mindistance[i] > 0.1f) { return(false); } closestTouchs[i] = Input.touches[indexs[i]]; } DetectClosestTouchMovement.Calculate(closestTouchs); return(true); } } return(false); }