2016-05-03 48 views
2

我創建使用AirConsole控制器生成一個操縱桿和它說,它發送該屏幕:如何獲得在Unity C#從操縱桿控制器生成數據

{ 
    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"]; 
} 

當我嘗試投壓制爲bool,它給了我ArgumentNullException:參數不能爲空。我也不知道我應該用什麼語法來獲取遊戲杆的方向。

如何解析C#Unity中的信息?

回答

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"]; 
// ... 
+0

它的工作,謝謝! –