如何从Unity C中的操纵杆控制器生成器获取数据#

时间:2016-05-03 00:34:17

标签: airconsole

我使用AirConsole控制器生成器创建了一个操纵杆,它说它将它发送到屏幕:

{
    joystick-left: {
        pressed: true|false,
        message: { x: Number, y: Number }
    }
}

现在我不知道如何在Unity中解析它。这是我试过的:

void OnMessage(int receivedID, JToken receivedData)
{
    bool pressed = (bool)receivedData["pressed"];
    float directionX = (float)receivedData["message"]["x"];
    float directionY = (float)receivedData["message"]["y"];
}

当我尝试将press压缩为bool时,它会给我ArgumentNullException:Argument不能为null。我也不知道我应该使用什么语法来获得操纵杆方向。

如何将信息解析为C#Unity?

1 个答案:

答案 0 :(得分:2)

我根本不熟悉C#或Unity,但你必须做s.t.像:

// Sorry, I don't know the type, but assume its 'JToken'
JToken joystick_data = (JToken)receivedData["joystick-left"];

// And then to get the other params:
bool pressed = (bool)joystick_data["pressed"];
float directionX = (float)joystick_data["message"]["x"];
// ...