2

我使用 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"];
}

当我尝试将pressed转换为布尔值时,它给了我ArgumentNullException:Argument cannot be null。我也不知道应该使用什么语法来获取操纵杆方向。

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

4

1 回答 1

2

我根本不熟悉 C# 或 Unity,但你必须这样做:

// 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"];
// ...
于 2016-05-03T08:06:13.043 回答