DEV Community

KOGA Mitsuhiro
KOGA Mitsuhiro

Posted on • Originally published at qiita.com

UnityでDaydreamコントローラーのGesture C APIを使う

はじめに

DaydreamはGesture C APIがありタッチパッドについて以下のような機能を使えます。
このAPIはAndroid NDKのみに提供されていているのでUnityから扱えるようにしました。

  • スワイプ判定 (開始、スワイプ中、終了、スワイプ)
  • スワイプ方向の判定(上下左右)
  • スワイプ速度(X軸とY軸)
  • スワイプ移動量(X軸とY軸)
  • 長押し判定

使い方

ソース

https://gist.github.com/shiena/d2cbd2bd100cc9ed7db4b4dfd608feca から2つのソースをコピーしてUnityプロジェクトに入れます。

使い方

using UnityEngine;

public class GestureTest
{
    [SerializeField] private GvrGesture gvrGesture;

    void Update()
    {
        var gestures = gvrGesture.GetGesture();
        if (gestures.Count > 0)
        {
            foreach (var g in gestures)
            {
                Debug.Log($"{g.GestureType}, {g.GestureDirection}, {g.Displacement.x}, {g.Displacement.y}, {g.Velocity.x}, {g.Velocity.y}");
            }
        }

        if (gvrGesture.LongPressed())
        {
            Debug.Log("Long pressed");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode
機能 メソッド
スワイプ判定 GvrGesture::GetGesture()
長押し判定 GvrGesture::LongPressed()

このAPIがなくてもUnityで同じ事を実現できるのですがあったらあったで便利です。
せっかくなのでUnity Package Managerに対応しようとしたのですが依存先にGoogle VR SDK for Unityを指定できないので断念しました。残念。

リンク

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay