コガネブログ

平日更新を目標に Unity や C#、Visual Studio などのゲーム開発アレコレを書いていきます

【Unity】タッチジェスチャを簡単に実装できる「TouchKit」紹介

はじめに

「TouchKit」を Unity プロジェクトに導入することで
タッチジェスチャを簡単に実装できるようになります

使用例

f:id:baba_s:20181231143216g:plain

使い方

タッチ

var recognizer = new TKTapRecognizer();
recognizer.boundaryFrame = new TKRect( 0, 0, 50f, 50f );
recognizer.gestureRecognizedEvent += r =>
{
    Debug.Log( r );
};
TouchKit.addGestureRecognizer( recognizer );

長押し

var recognizer = new TKLongPressRecognizer();
recognizer.gestureRecognizedEvent += r =>
{
    Debug.Log( r );
};
recognizer.gestureCompleteEvent += r =>
{
    Debug.Log( r );
};
TouchKit.addGestureRecognizer( recognizer );

パン(ドラッグ)

var recognizer = new TKPanRecognizer();
recognizer.gestureRecognizedEvent += r =>
{
    var d = recognizer.deltaTranslation;
    Camera.main.transform.position -= new Vector3( d.x, d.y ) / 100;
    Debug.Log( r );
};
recognizer.gestureCompleteEvent += r =>
{
    Debug.Log( r );
};
TouchKit.addGestureRecognizer( recognizer );

スワイプ

var recognizer = new TKSwipeRecognizer();
recognizer.gestureRecognizedEvent += r =>
{
    Debug.Log( r );
};
TouchKit.addGestureRecognizer( recognizer );

ピンチ

var recognizer = new TKPinchRecognizer();
recognizer.gestureRecognizedEvent += r =>
{
    cube.transform.localScale += Vector3.one * recognizer.deltaScale;
    Debug.Log( r );
};
TouchKit.addGestureRecognizer( recognizer );

回転(2本指)

var recognizer = new TKRotationRecognizer();
recognizer.gestureRecognizedEvent += r =>
{
    cube.Rotate( Vector3.back, recognizer.deltaRotation );
    Debug.Log( r );
};
TouchKit.addGestureRecognizer( recognizer );

回転(1本指)

var recognizer = new TKOneFingerRotationRecognizer();
recognizer.targetPosition = Camera.main.WorldToScreenPoint( cube.position );
recognizer.gestureRecognizedEvent += r =>
{
    cube.Rotate( Vector3.back, recognizer.deltaRotation );
    Debug.Log( r );
};
TouchKit.addGestureRecognizer( recognizer );

イベント解除

TouchKit.removeAllGestureRecognizers();