본문 바로가기
NOTE/SaveSource

[Source Save] InputTouchManager.cs

by DevAthena 2016. 10. 27.
using UnityEngine;
using System.Collections.Generic;
 
/*  
Rotate
Tap, Double Tap, Press, Drag
*/
 
class InputTouchManager : SingleTon<InputTouchManager>
{
    #region One finger Value
    private     Touch       _initTouch = new Touch();
    private     Vector2     _dragXY = Vector2.zero;        // X,Y 축으로 드래그한 정도
    private     float       _pressTime;                    // Press 판정을 위한 시간
    private     bool        _isDrag;
    private     bool        _isPress;
    private     bool        _isDoubleTap;
    private     GameObject  _rayObject;
    #endregion
 
    #region Two finger Value
    private     float       _zoomDgree;
    private     Touch       _touch0;
    private     Touch       _touch1;
    private     Vector2     _touch0_PrevPos;
    private     Vector2     _touch1_PrevPos;
    private     float       _prevDeltaMag;
    private     float       _deltaMag;
    private     bool        _isZoom;
    #endregion
 
    private InputTouchManager() {   }
 
    #region TouchDetect
    public void TouchDetect()
    {
        CoroutineManager.Instance.Register(TouchDetecting());
    }
    #endregion
 
    #region TouchDetecting~
    private IEnumerator<CoroutinePhase> TouchDetecting()
    {
        while(true)
        {
            int touchCount = Input.touchCount;
 
            if(touchCount != 2)
            {
                _isZoom = false;
            }
            else
            {
                _isZoom = true;
            }
 
            if(touchCount == 1)
            {
                OneFinger();
            }
            else if(touchCount == 2)
            {
                TwoFinger();
            }
            yield return null;
        }
    }
    #endregion
 
    #region OneFinger
    private void OneFinger()
    {
        Touch touch = Input.GetTouch(0);
        switch (touch.phase)
        {
            case TouchPhase.Began:
                _initTouch = touch;
                break;
 
            case TouchPhase.Moved:
                /* x: 오른쪽이동 -값 , y : 위쪽이동 -값 */
                _dragXY.x = _initTouch.position.x - touch.position.x;
                _dragXY.y = _initTouch.position.y - touch.position.y;
                _isDrag = true;
                break;
 
            case TouchPhase.Stationary:
                if (_pressTime >= 1f)
                {
                    _isPress = true;
                }
                _pressTime += RealTimer.DeltaTime;
                _isDrag = false;
                break;
 
            case TouchPhase.Ended:
                if (touch.tapCount == 2)
                {
                    DoubleTap();
                }
                else if (touch.tapCount == 1)
                {
                    if (_isPress)
                    {
                        _isPress = false;
                    }
                    else
                    {
                        OneTap(touch);
                    }
                }
 
                _initTouch  = new Touch();
                _pressTime  = 0;
                _isDrag     = false;
                break;
        }
    }
    #endregion
 
    #region TwoFinger
    private void TwoFinger()
    {
        _touch0 = Input.GetTouch(0);
        _touch1 = Input.GetTouch(1);
 
        _touch0_PrevPos = _touch0.position - _touch0.deltaPosition;
        _touch1_PrevPos = _touch1.position - _touch1.deltaPosition;
 
        _prevDeltaMag = (_touch0_PrevPos - _touch1_PrevPos).magnitude;
        _deltaMag     = (_touch0.position - _touch1.position).magnitude;
 
        _zoomDgree = _prevDeltaMag - _deltaMag;
    }
    #endregion
 
    #region DoubleTap
    private void DoubleTap()
    {
        _isDoubleTap = true;
    }
    public void DoubleTapUsed()
    {
        _isDoubleTap = false;
    }
    #endregion
 
    #region OneTap - Raycast로 탭한 위치의 오브젝트를 _rayObject에 담아줌 (object엔 collider 필요)
    private void OneTap(Touch touch)
    {
        RaycastHit hit;
        Ray ray = Camera.main.ScreenPointToRay(touch.position);
 
        if(Physics.Raycast(ray, out hit, Mathf.Infinity))
        {
            _rayObject = hit.collider.gameObject;
            Log.Desc("Tap Object -> " + _rayObject.name);
        }
    }
    #endregion
 
    #region Property
    public Vector2      GetDragXY               { get { return  _dragXY;            } }
    public bool         GetIsPress              { get { return  _isPress;           } }
    public bool         GetIsDoubleTap          { get { return  _isDoubleTap;       } }
    public bool         GetIsDrag               { get { return  _isDrag;            } }
    public GameObject   GetRayObject            { get { return  _rayObject;         } }
    public float        GetZoomDgree            { get { return  _zoomDgree;         } }
    public bool         GetIsZoom               { get { return  _isZoom;            } }
    #endregion
}
 
 
cs


'NOTE > SaveSource' 카테고리의 다른 글

[Source Save] MobileSensorManager.cs  (0) 2016.11.01
[Source Save] TouchCameraMove.cs  (0) 2016.10.31
[SaveSource] Drag Script  (0) 2016.10.14
[SaveSource] Touch Pinch Zoom  (0) 2016.10.14
[SaveSource] Player Cam & Input.Axis  (0) 2016.10.07