private void GetInput() { // for Touch if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android) { if (Input.touchCount == 1) { if (Input.GetTouch(0).phase == TouchPhase.Moved) { moveVector = (Vector3)(Input.GetTouch(0).deltaPosition / 10.0f); isClicked = true; } else { ResetInput(); } } else if (Input.touchCount == 0) { ResetInput(); } } #if UNITY_STANDALONE_WIN else if (Application.platform == RuntimePlatform.WindowsPlayer && winTouch) { if (TouchScript.TouchManager.Instance.PressedPointersCount == 1) { TouchScript.Pointers.Pointer tp = TouchScript.TouchManager.Instance.PressedPointers[0]; if (tp.Position != tp.PreviousPosition) { moveVector = (Vector3)((tp.PreviousPosition - tp.Position) / 10.0f); isClicked = true; } else { ResetInput(); } } else if (TouchScript.TouchManager.Instance.PressedPointersCount == 0) { ResetInput(); } } #endif // for Mouse else { if (Input.GetMouseButton(0)) { moveVector = new Vector3(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"), 0.0f); isClicked = true; } else { ResetInput(); } } }
private void InputByDevice() { isTouch = true; touchCount = 0; if (inputType == INPUT_TYPE.TOUCH) { #if UNITY_STANDALONE_WIN // for Windows Player #if !USE_TOUCH_SCRIPT Debug.LogWarning("Input.Touch is not work in windows. please use TouchScript. and then enable the define macro on the first line of this script."); #else // as TouchScript touchCount = TouchScript.TouchManager.Instance.PressedPointersCount; if (touchCount >= 1) { TouchScript.Pointers.Pointer tp = TouchScript.TouchManager.Instance.PressedPointers[0]; Rect wrect = WindowsUtil.GetApplicationWindowRect(); // not work in Editor. touchPosition = new Vector2( (int)(((tp.Position.x / Screen.width) * Screen.currentResolution.width) - wrect.x), Screen.height + (int)(((tp.Position.y / Screen.height) * Screen.currentResolution.height) - wrect.y)); if (tp.Position == tp.PreviousPosition) { if (isPressed) { phase = INPUT_PHASE.Moved; } else { phase = INPUT_PHASE.Began; } } else { phase = INPUT_PHASE.Moved; } } else #endif { // Pressされた後にここにきたらReleaseとする if (isPressed) { phase = INPUT_PHASE.ENDED; } else { phase = INPUT_PHASE.NONE; touchPosition = Vector3.zero; isTouch = false; } } #elif UNITY_IOS || UNITY_ANDROID // for Mobile touchCount = Input.touchCount; if (touchCount >= 1) { touchPosition = Input.GetTouch(0).position; TouchPhase tphase = Input.GetTouch(0).phase; if (tphase == TouchPhase.Began) { phase = INPUT_PHASE.BEGAN; } else if (tphase == TouchPhase.Ended || tphase == TouchPhase.Canceled) { phase = INPUT_PHASE.ENDED; } else if (tphase == TouchPhase.Moved || tphase == TouchPhase.Stationary) { phase = INPUT_PHASE.MOVED; } } else { phase = INPUT_PHASE.NONE; touchPosition = Vector3.zero; isTouch = false; } #endif } // for Mouse else if (inputType == INPUT_TYPE.MOUSE) { touchPosition = Input.mousePosition; if (Input.GetMouseButton(0)) { if (Input.GetMouseButtonDown(0)) { phase = INPUT_PHASE.BEGAN; } else { phase = INPUT_PHASE.MOVED; } touchCount = 1; } else if (Input.GetMouseButtonUp(0)) { phase = INPUT_PHASE.ENDED; } else { phase = INPUT_PHASE.NONE; } } if (touchCount < 0) { touchCount = 0; } }
private void GetInput() { // for Touch if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer) { if (Input.touchCount >= grabTouchNum) { // カメラ操作のロック if (flyThroughCamera != null) { flyThroughCamera.LockInput(this.gameObject); } if (pinchZoomCamera != null) { pinchZoomCamera.LockInput(this.gameObject); } if (orbitCamera != null) { orbitCamera.LockInput(this.gameObject); } // コンポーネントをOFF foreach (MonoBehaviour component in disableComponents) { if (component != null) { component.enabled = false; } } // ドラッグ量を計算 float touchesPosX = (Input.GetTouch(0).position.x + Input.GetTouch(1).position.x + Input.GetTouch(2).position.x) / 3.0f; float touchesPosY = (Input.GetTouch(0).position.y + Input.GetTouch(1).position.y + Input.GetTouch(2).position.y) / 3.0f; Vector3 currentWorldTouchPos = renderCamera.ScreenToWorldPoint(new Vector3(touchesPosX, touchesPosY, 1000.0f)); if (isFirstTouch) { oldWorldTouchPos = currentWorldTouchPos; isFirstTouch = false; return; } dragDelta = currentWorldTouchPos - oldWorldTouchPos; oldWorldTouchPos = currentWorldTouchPos; } else { ResetInput(); } } #if UNITY_STANDALONE_WIN else if (Application.platform == RuntimePlatform.WindowsPlayer && winTouch) { if (TouchScript.TouchManager.Instance.PressedPointersCount >= grabTouchNum) { // カメラ操作のロック if (flyThroughCamera != null) { flyThroughCamera.LockInput(this.gameObject); } if (pinchZoomCamera != null) { pinchZoomCamera.LockInput(this.gameObject); } if (orbitCamera != null) { orbitCamera.LockInput(this.gameObject); } // コンポーネントをOFF foreach (MonoBehaviour component in disableComponents) { if (component != null) { component.enabled = false; } } // ドラッグ量を計算 TouchScript.Pointers.Pointer tp0 = TouchScript.TouchManager.Instance.PressedPointers[0]; TouchScript.Pointers.Pointer tp1 = TouchScript.TouchManager.Instance.PressedPointers[1]; TouchScript.Pointers.Pointer tp2 = TouchScript.TouchManager.Instance.PressedPointers[2]; float touchesPosX = (tp0.Position.x + tp1.Position.x + tp2.Position.x) / 3.0f; float touchesPosY = (tp0.Position.y + tp1.Position.y + tp2.Position.y) / 3.0f; Vector3 currentWorldTouchPos = renderCamera.ScreenToWorldPoint(new Vector3(touchesPosX, touchesPosY, 1000.0f)); if (isFirstTouch) { oldWorldTouchPos = currentWorldTouchPos; isFirstTouch = false; return; } dragDelta = currentWorldTouchPos - oldWorldTouchPos; oldWorldTouchPos = currentWorldTouchPos; } else { ResetInput(); } } #endif // for Mouse else { if (Input.GetMouseButton(0) && (Input.GetKey(KeyCode.LeftAlt) || Input.GetKey(KeyCode.RightAlt)) && (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))) { // カメラ操作のロック if (flyThroughCamera != null) { flyThroughCamera.LockInput(this.gameObject); } if (pinchZoomCamera != null) { pinchZoomCamera.LockInput(this.gameObject); } if (orbitCamera != null) { orbitCamera.LockInput(this.gameObject); } // コンポーネントをOFF foreach (MonoBehaviour component in disableComponents) { if (component != null) { component.enabled = false; } } // ドラッグ量を計算 Vector3 currentWorldTouchPos = renderCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 1000.0f)); if (isFirstTouch) { oldWorldTouchPos = currentWorldTouchPos; isFirstTouch = false; return; } dragDelta = currentWorldTouchPos - oldWorldTouchPos; oldWorldTouchPos = currentWorldTouchPos; } else { ResetInput(); } } }
private void GetInput2() { // for Touch if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android) { if (Input.touchCount == 2) { if (lastFingerVec == Vector3.zero) { lastFingerVec = (Input.GetTouch(0).position - Input.GetTouch(1).position).normalized; lastFingerPos = (Input.GetTouch(0).position + Input.GetTouch(1).position) / 2.0f; } else { moveVector = Vector3.zero; // 回転 Vector3 now_vec = (Input.GetTouch(0).position - Input.GetTouch(1).position).normalized; moveVector.x += Mathf.Rad2Deg * (Mathf.Atan2(lastFingerVec.y, lastFingerVec.x) - Mathf.Atan2(now_vec.y, now_vec.x)); float rot_x_max = 10.0f; if (moveVector.x < rot_x_max) { moveVector.x = -rot_x_max; } if (rot_x_max < moveVector.x) { moveVector.x = rot_x_max; } lastFingerVec = now_vec; // 傾き Vector3 now_pos = (Input.GetTouch(0).position + Input.GetTouch(1).position) / 2.0f; moveVector.y += (-100.0f * ((now_pos - lastFingerPos).y / Screen.height)); lastFingerPos = now_pos; } } else { ResetInput(); } } #if UNITY_STANDALONE_WIN else if (Application.platform == RuntimePlatform.WindowsPlayer && winTouch) { #if !USE_TOUCH_SCRIPT if (Input.touchCount == 2) { if (lastFingerVec == Vector3.zero) { lastFingerVec = (Input.touches[0].position - Input.touches[1].position).normalized; lastFingerPos = (Input.touches[0].position + Input.touches[1].position) / 2.0f; } #else if (TouchScript.TouchManager.Instance.PressedPointersCount == 2) { TouchScript.Pointers.Pointer pt0 = TouchScript.TouchManager.Instance.PressedPointers[0]; TouchScript.Pointers.Pointer pt1 = TouchScript.TouchManager.Instance.PressedPointers[1]; if (lastFingerVec == Vector3.zero) { lastFingerVec = (pt0.Position - pt1.Position).normalized; lastFingerPos = (pt0.Position + pt1.Position) / 2.0f; } #endif else { moveVector = Vector3.zero; // 回転 #if !USE_TOUCH_SCRIPT Vector3 now_vec = (Input.touches[0].position - Input.touches[1].position).normalized; #else Vector3 now_vec = (pt0.Position - pt1.Position).normalized; #endif moveVector.x += Mathf.Rad2Deg * (Mathf.Atan2(lastFingerVec.y, lastFingerVec.x) - Mathf.Atan2(now_vec.y, now_vec.x)); float rot_x_max = 10.0f; if (moveVector.x < rot_x_max) { moveVector.x = -rot_x_max; } if (rot_x_max < moveVector.x) { moveVector.x = rot_x_max; } lastFingerVec = now_vec; // 傾き #if !USE_TOUCH_SCRIPT Vector3 now_pos = (Input.touches[0].position + Input.touches[1].position) / 2.0f; #else Vector3 now_pos = (pt0.Position + pt1.Position) / 2.0f; #endif moveVector.y += (-100.0f * ((now_pos - lastFingerPos).y / Screen.height)); lastFingerPos = now_pos; } } else { ResetInput(); } } #endif // for Mouse else { moveVector = Vector3.zero; if (Input.GetMouseButton(1)) { // 回転 moveVector.x = (oldMousePosition.x - Input.mousePosition.x) * ratioForMouse; // 傾き moveVector.y = (oldMousePosition.y - Input.mousePosition.y) * ratioForMouse; } else { ResetInput(); } oldMousePosition = Input.mousePosition; } }
/// <summary> /// 入力デバイスにより分岐する /// </summary> private void InputDevice() { isTouch = true; touchCount = 0; // for WinTouch if (inputType == INPUT_TYPE.WTOUCH) { #if UNITY_STANDALONE_WIN #if !USE_TOUCH_SCRIPT touchCount = Input.touchCount; // 1点目を入力として受付 if (touchCount >= 1) { Rect wrect = WindowsUtil.GetApplicationWindowRect(); // not work in Editor. touchPosition = new Vector2( (int)(((Input.touches[0].position.x / Screen.width) * Screen.currentResolution.width) - wrect.x), Screen.height + (int)(((Input.touches[0].position.x / Screen.height) * Screen.currentResolution.height) - wrect.y)); if (Input.touches[0].deltaPosition != Vector2.zero) { if (isPressed) { phase = INPUT_PHASE.Moved; } else { phase = INPUT_PHASE.Began; } } else { phase = INPUT_PHASE.Moved; } #else touchCount = TouchScript.TouchManager.Instance.PressedPointersCount; // 1点目を入力として受付 if (touchCount >= 1) { TouchScript.Pointers.Pointer tp = TouchScript.TouchManager.Instance.PressedPointers[0]; Rect wrect = WindowsUtil.GetApplicationWindowRect(); // not work in Editor. touchPosition = new Vector2( (int)(((tp.Position.x / Screen.width) * Screen.currentResolution.width) - wrect.x), Screen.height + (int)(((tp.Position.y / Screen.height) * Screen.currentResolution.height) - wrect.y)); if (tp.Position == tp.PreviousPosition) { if (isPressed) { phase = INPUT_PHASE.Moved; } else { phase = INPUT_PHASE.Began; } } else { phase = INPUT_PHASE.Moved; } #endif } else { // Pressされた後にここにきたらReleaseとする if (isPressed) { phase = INPUT_PHASE.Ended; } else { phase = INPUT_PHASE.NONE; touchPosition = Vector3.zero; isTouch = false; } } #endif } // for Mobile else if (inputType == INPUT_TYPE.INPUTTOUCH) { touchCount = Input.touchCount; if (touchCount >= 1) { touchPosition = Input.GetTouch(0).position; TouchPhase tphase = Input.GetTouch(0).phase; if (tphase == TouchPhase.Began) { phase = INPUT_PHASE.Began; } else if (tphase == TouchPhase.Ended || tphase == TouchPhase.Canceled) { phase = INPUT_PHASE.Ended; } else if (tphase == TouchPhase.Moved || tphase == TouchPhase.Stationary) { phase = INPUT_PHASE.Moved; } } else { phase = INPUT_PHASE.NONE; touchPosition = Vector3.zero; isTouch = false; } } // for Mouse else if (inputType == INPUT_TYPE.MOUSE) { touchPosition = Input.mousePosition; if (Input.GetMouseButton(0)) { if (Input.GetMouseButtonDown(0)) { phase = INPUT_PHASE.Began; } else { phase = INPUT_PHASE.Moved; } touchCount = 1; } else if (Input.GetMouseButtonUp(0)) { phase = INPUT_PHASE.Ended; } else { phase = INPUT_PHASE.NONE; } } if (touchCount < 0) { touchCount = 0; } }