5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

スワイプ角算出手順の備忘録

Posted at

概要

uGUIオブジェクトごとの、スワイプ角算出手順の備忘録。

スワイプイベントを拾う

以下にまとめました。
https://qiita.com/ganessa/items/71cc0293d09ebd3ea980

角度算出

閾値チェックにはmagnitudeではなく、sqrMagnitudeを使用。
Mathf.Atan2(y, x)を使うと、象限ごとに分けなくて済みそう。

SwipeHandler.cs
using UnityEngine;
using UnityEngine.EventSystems;

public class SwipeHandler : MonoBehaviour, IDragHandler, IEndDragHandler {
    public float threshold = 100.0f;

    public void OnDrag(PointerEventData eventData)
    {
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        // eventData.delta の値が期待と異なったので算出
        Vector2 delta = eventData.position - eventData.pressPosition;

        // 閾値チェック(magnitudeは重いのでsqrMagnitudeを使う)
        // if (delta.magnitude < threshold) {
        if (delta.sqrMagnitude < threshold * threshold) {
            return;
        }

        // スワイプ角度算出
        //        90
        //        ↑
        // ±180 ← x → 0
        //        ↓
        //       -90
        float deg = Mathf.Atan2(delta.y, delta.x) * 180 / Mathf.PI;
    }
}

uGUIにアタッチ

あとはこのクラスをInspector上でアタッチするだけでOK

5
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?