/// <summary> /// タッチを管理する関数。 /// </summary> /// <param name="touch">Touch.</param> private void ManageTouch(CustomTouch touch) { List <CustomTouch> list; if (_touchListDict != null && _touchListDict.ContainsKey(touch.fingerID)) { list = _touchListDict[touch.fingerID]; } else { if (_touchListDict == null) { _touchListDict = new Dictionary <int, List <CustomTouch> >(); } list = new List <CustomTouch>(); } list.Add(touch); if (_touchListDict.ContainsKey(touch.fingerID)) { _touchListDict[touch.fingerID] = list; } else { _touchListDict.Add(touch.fingerID, list); } }
/// <summary> /// 過去のタッチ状況から現在のタッチイベントを判定して返す関数。 /// </summary> /// <returns>The touch state.</returns> /// <param name="touch">Touch.</param> private TouchEventType GetTouchEventType(CustomTouch touch) { List <CustomTouch> list = _touchListDict[touch.fingerID]; if (list == null || list.Count == 0) { return(TouchEventType.NONE); } if (list.Count == 1) { return(TouchEventType.TOUCH); } else if (touch.phase == TouchPhase.Stationary) { int index = list.FindIndex(m => m.phase == TouchPhase.Moved); if (index != -1) { return(TouchEventType.SWIPE); } else { return(TouchEventType.PRESS); } } else if (touch.phase == TouchPhase.Moved) { return(TouchEventType.SWIPE); } else if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled) { return(TouchEventType.END); } return(TouchEventType.NONE); }
void Update() { _isTouchThisFrame = false; if (0 < Input.touchCount) { ProcessTouches(TouchesToCustomTouches(Input.touches)); } //マウスイベント bool isMouse = false; bool isMultiMouse = Input.GetKey(KeyCode.LeftShift); MouseButtonEventType mouseEvent = MouseButtonEventType.NONE; if (Input.GetMouseButtonDown(0)) { //特定のマウスの情報を取得する。押された瞬間、trueを返す。 isMouse = true; mouseEvent = MouseButtonEventType.CLICK; } else if (Input.GetMouseButton(0)) { //特定のマウスの情報を取得する。押している間、trueを返す。 isMouse = true; mouseEvent = MouseButtonEventType.DOWN; } else if (Input.GetMouseButtonUp(0)) { //特定のマウスの情報を取得する。ボタンから指を話したとき、trueを返す isMouse = true; mouseEvent = MouseButtonEventType.UP; } if (isMouse) { CustomTouch touch = MouseToCustomTouch(Input.mousePosition, mouseEvent); CustomTouch[] touches; if (isMultiMouse) { CustomTouch rTouch = MouseToCustomTouch(Input.mousePosition, mouseEvent); rTouch.position = new Vector2(Screen.width / 2 + (Screen.width / 2 - touch.position.x), Screen.height / 2 + (Screen.height / 2 - touch.position.y)); rTouch.fingerID = touch.fingerID * -1; touches = new CustomTouch[] { touch, rTouch }; } else { touches = new CustomTouch[] { touch }; } ProcessTouches(touches); } if (!_isTouchThisFrame) { // タッチイベントが無かった場合、前フレームのタッチイベントの保持を解放する。 _prevTouchEventDict = null; } }
/// <summary> /// Touch配列をCustomTouch配列に変換する関数。 /// </summary> /// <returns>The to CustomTouch Array</returns> /// <param name="touches">Touches.</param> private CustomTouch[] TouchesToCustomTouches(Touch[] touches) { CustomTouch[] customTouches = new CustomTouch[touches.Length]; for (int i = 0; i < touches.Length; i++) { customTouches[i] = TouchToCustomTouch(touches[i]); } return(customTouches); }
/// <summary> /// Touch moved callback. /// </summary> /// <param name="touch">Touch info.</param> /// <param name="deltaPosition">Touch position difference.</param> void OnTouchMove(CustomTouch touch, Vector2 deltaPosition) { if (touch.fingerID == leftTouchfingerID) { Vector3 direction = new Vector3(deltaPosition.x, 0, deltaPosition.y).normalized; playerManager.Move(direction, true); } if (touch.fingerID == rightTouchfingerID) { this.RotateMeAndShoot(deltaPosition); } }
/// <summary> /// Touch moved callback. /// </summary> /// <param name="touch">Touch info.</param> /// <param name="deltaPosition">Touch position difference.</param> void OnTouchMove(CustomTouch touch, Vector2 deltaPosition) { if (touch.fingerID == leftTouchfingerID) { Vector3 direction = new Vector3(deltaPosition.x, 0, deltaPosition.y).normalized; playerManager.Move(direction,true); } if (touch.fingerID == rightTouchfingerID) { this.RotateMeAndShoot(deltaPosition); } }
/// <summary> /// Touch began callback. /// </summary> /// <param name="touch">Touch info.</param> void OnTouchBegan(CustomTouch touch) { switch (touch.side) { case TouchSides.Left: leftTouchfingerID = touch.fingerID; break; case TouchSides.Right: playerManager.StartShoot(); rightTouchfingerID = touch.fingerID; break; } }
/// <summary> /// Touch began callback. /// </summary> /// <param name="touch">Touch info.</param> void OnTouchBegan(CustomTouch touch) { switch (touch.side) { case TouchSides.Left: leftTouchfingerID = touch.fingerID; break; case TouchSides.Right: playerManager.StartShoot(); rightTouchfingerID = touch.fingerID; break; } }
/// <summary> /// Touch ended callback. /// </summary> /// <param name="touch">Touch info.</param> void OnTouchEnd(CustomTouch touch) { switch (touch.side) { case TouchSides.Left: leftTouchfingerID = -1; playerManager.StopMoving(); break; case TouchSides.Right: rightTouchfingerID = -1; playerManager.StopShooting(); break; } }
/// <summary> /// Touch ended callback. /// </summary> /// <param name="touch">Touch info.</param> void OnTouchEnd(CustomTouch touch) { switch (touch.side) { case TouchSides.Left: leftTouchfingerID = -1; playerManager.StopMoving(); break; case TouchSides.Right: rightTouchfingerID = -1; playerManager.StopShooting(); break; } }
/// <summary> /// Creates a new touch from parameters, and sends it to the listeners. /// </summary> /// <param name="ID">Touch ID.</param> /// <param name="position">Touch position on screen.</param> void CreateNewTouch(int ID, Vector2 position) { TouchSides side; side = position.x < Screen.width / 2f ? TouchSides.Left : TouchSides.Right; if (this.ContainsTouchInSide(side) || this.ContainsTouchWithFinger(ID)) { return; } CustomTouch touch = new CustomTouch(); touch.fingerID = ID; touch.touchPosition = position; touch.actualPhase = TouchPhase.Began; touch.side = side; touches.Add(touch); touchesCount++; this.ReorderTouches(); switch (side) { case TouchSides.Left: leftTouchImage.gameObject.SetActive(true); Vector3 touchPosition = touch.touchPosition; touchPosition /= screenScale; RectTransform rect = (RectTransform)leftTouchImage.transform; rect.anchoredPosition = touchPosition; break; case TouchSides.Right: rightTouchImage.gameObject.SetActive(true); touchPosition = touch.touchPosition; touchPosition /= screenScale; rect = (RectTransform)rightTouchImage.transform; rect.anchoredPosition = touchPosition; break; } this.OnTouchStart(touch); }
/// <summary> /// Creates a new touch from parameters, and sends it to the listeners. /// </summary> /// <param name="ID">Touch ID.</param> /// <param name="position">Touch position on screen.</param> void CreateNewTouch(int ID, Vector2 position) { TouchSides side; side = position.x < Screen.width / 2f ? TouchSides.Left : TouchSides.Right; if (this.ContainsTouchInSide(side) || this.ContainsTouchWithFinger(ID)) { return; } CustomTouch touch = new CustomTouch(); touch.fingerID = ID; touch.touchPosition = position; touch.actualPhase = TouchPhase.Began; touch.side = side; touches.Add(touch); touchesCount++; this.ReorderTouches(); switch (side) { case TouchSides.Left: leftTouchImage.gameObject.SetActive(true); Vector3 touchPosition = touch.touchPosition; touchPosition /= screenScale; RectTransform rect = (RectTransform)leftTouchImage.transform; rect.anchoredPosition = touchPosition; break; case TouchSides.Right: rightTouchImage.gameObject.SetActive(true); touchPosition = touch.touchPosition; touchPosition /= screenScale; rect = (RectTransform)rightTouchImage.transform; rect.anchoredPosition = touchPosition; break; } this.OnTouchStart(touch); }
/// <summary> /// マウス入力をCustomTouchクラスに置き換える関数。 /// </summary> /// <returns>CustomTouchクラス</returns> /// <param name="mousePosition">マウス座標</param> /// <param name="type">入力タイプ</param> private CustomTouch MouseToCustomTouch(Vector2 mousePosition, MouseButtonEventType type) { TouchPhase phase = TouchPhase.Began; switch (type) { case MouseButtonEventType.CLICK: phase = TouchPhase.Began; break; case MouseButtonEventType.DOWN: if (_isPrevMouseInput) { //前のフレームもマウスのボタンを押していた場合。 if (_prevMousePosition == mousePosition) { //前のフレームとマウスの位置座標が同じ場合:プレス phase = TouchPhase.Stationary; } else { //前のフレームからマウスの位置座標が移動している場合:ドラッグ phase = TouchPhase.Moved; } } else { phase = TouchPhase.Began; } break; case MouseButtonEventType.UP: if (_isPrevMouseInput) { //前のフレームもマウスボタンを押していた場合。 phase = TouchPhase.Ended; } else { //普通だったらこの分岐は絶対にしない。 phase = TouchPhase.Ended; } break; } CustomTouch touch = new CustomTouch(); touch.deltaPosition = mousePosition - _prevMousePosition; touch.deltaTime = _isPrevMouseInput ? Time.deltaTime - _prevMouseTime : 0f; touch.fingerID = _mouseClickCount; touch.phase = phase; touch.position = mousePosition; touch.tapCount = 1; touch.time = Time.time; if (phase == TouchPhase.Ended || phase == TouchPhase.Canceled) { _isPrevMouseInput = false; _mouseClickCount++; } else { _isPrevMouseInput = true; _prevMousePosition = mousePosition; _prevMouseTime = Time.deltaTime; } return(touch); }
private TouchEvent GetTouchEvent(CustomTouch touch) { return(new TouchEvent(touch, GetTouchEventType(touch))); }