コード例 #1
0
ファイル: TouchManager.cs プロジェクト: soulhez/TouchManager
    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;
        }
    }
コード例 #2
0
ファイル: TouchManager.cs プロジェクト: soulhez/TouchManager
    /// <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);
    }
コード例 #3
0
 public void RemoveMouseEvent(MouseButtonEventType mouseEventType, MouseButtonEvent eventHandler)
 {
     mouseButtonEvents[mouseEventType] -= eventHandler;
 }
コード例 #4
0
 public void AddMouseEvent(MouseButtonEventType mouseEventType, MouseButtonEvent eventHandler)
 {
     mouseButtonEvents[mouseEventType] += eventHandler;
 }