LoginSignup
0
0

More than 3 years have passed since last update.

UnityでLEAPのWebSocketサーバーjson受信

Posted at

Unityで複数LEAPの可能性を探していたので、ひとつの再現方法をメモ

内容はjsonデータをUnityで見えるようにしている状態にしただけなので、活用できるかは未検証。。

返り値floatが本当に正しい値?など。。

LEAPの設定は下記

UnityでWEBSocketは下記を参考にしました

動作状態

SnapCrab_NoName_2021-3-16_3-51-15_No-00.png

スクリプト

WsLeap.cs
using UnityEngine;
using WebSocketSharp;

public class WsLeap : MonoBehaviour
{
  public WebSocket ws;
  public string url = "ws://192.168.1.1:6437/";//環境ごとに変更
  public float currentFrameRate;
  public Devices[] devices;
  public Hands[] hands;
  public int id;
  public Pointables[] pointables;
  public int timestamp;
  public void RecvText(string text)
  {
    string json = text.ToString();
    LeapJson jsonData = new LeapJson();
    JsonUtility.FromJsonOverwrite(json, jsonData);

    currentFrameRate = jsonData.currentFrameRate;
    devices = jsonData.devices;
    hands = jsonData.hands;
    id = jsonData.id;
    pointables = jsonData.pointables;
    timestamp = jsonData.timestamp;
  }
  public void RecvClose()
  {
  }

  void Start()
  {
    ws = new WebSocket(url);
    ws.Connect();
    ws.OnMessage += (sender, e) => RecvText(e.Data);
    ws.OnClose += (sender, e) => RecvClose();
  }
}
LeapJson.cs
using System;
using UnityEngine;

[Serializable]
public class LeapJson
{
  public float currentFrameRate;
  public Devices[] devices;
  public Hands[] hands;
  public int id;
  public Pointables[] pointables;
  public int timestamp;
}

[Serializable]
public class Devices
{

}

[Serializable]
public class Hands
{
  public float[] direction;
  public int id;
  public float[] palmNormal;
  public float[] palmPosition;
  public float[] palmVelocity;
  public float timeVisible;
}


[Serializable]
public class Pointables
{
  public string direction;
  public int handId;
  public int id;
  public int length;
  public int timeVisible;
  public float[] tipPosition;
  public float width;
}

参考サイト

0
0
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
0
0