ゲーム開発奮闘記

Unity・エフェクト・アプリ開発に関係した記事を書いています

Unity Swipe機能

Swipe機能の実装の為のメモ

参考サイト http://forum.unity3d.com/threads/48601-Swipe-help-please

var startTime: float;
var startPos: Vector2;
var couldBeSwipe: boolean;
var comfortZone: float;
var minSwipeDist: float;
var maxSwipeTime: float;
 
function Update() {
    if (iPhoneInput.touchCount > 0) {
        var touch = iPhoneInput.touches[0];

        switch (touch.phase) {
            case iPhoneTouchPhase.Began:
                couldBeSwipe = true;
                startPos = touch.position;
                startTime = Time.time;
                break;
            
            case iPhoneTouchPhase.Moved:
                if (Mathf.Abs(touch.position.y - startPos.y) > comfortZone) {
                    couldBeSwipe = false;
                }
                break;
            
            case iPhoneTouchPhase.Stationary:
                couldBeSwipe = false;
                break;
            
            case iPhoneTouchPhase.Ended:
                var swipeTime = Time.time - startTime;
                var swipeDist = (touch.position - startPos).magnitude;
                
                if (couldBeSwipe && (swipeTime < maxSwipeTime) && (swipeDist > minSwipeDist)) {
                    // It's a swiiiiiiiiiiiipe!
                    var swipeDirection = Mathf.Sign(touch.position.y - startPos.y);
                    
                    // Do something here in reaction to the swipe.
                }
                break;
        }
    }
}

明日もまた一日ガンバロー!


人気ブログランキングへ