概要
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