我正在使用 Unity3D。这是我的异步方法:
private void Receive(IAsyncResult ar)
{
try
{
IPEndPoint ipEndPoint = null;
byte[] data = udpClient.EndReceive(ar, ref ipEndPoint);
string receivedMessage = Encoding.UTF8.GetString(data);
JsonData json = JsonConvert.DeserializeObject<JsonData>(receivedMessage);
string prefix = json.header.Substring(0, 2);
Debug.Log("UDP World: " + receivedMessage);
if (prefix != "3x")
{
Debug.Log("Unknown packet: " + receivedMessage + "\n");
}
else
{
string header = json.header.Substring(2);
int conId = json.connectionId;
switch (header)
{
default:
Debug.Log("Unknown packet: " + receivedMessage + "\n");
break;
case "001":
Debug.Log("Data received: " + receivedMessage + "\n");
break;
case "002":
CharacterPosition position = new CharacterPosition();
position.x = float.Parse(json.data["position.x"].ToString());
position.y = float.Parse(json.data["position.y"].ToString());
}
}
}
我在 Unity 控制台中看到的只是以下输出:
Debug.Log("Position 2: X: " + position.x + " Y: " + position.y + " Z: " + position.z + " Character: " + characterId + " CnnID: " + cnnId);
使用上面显示的 Debug.Log,我可以看到我拥有所需的所有信息。Debug.Log 中的所有内容都在那里。下一行:
Debug.Log("GameObject FIND: " + GameObject.Find("CharactersOnline").name);
似乎是一切刹车的地方。我没有从该行收到任何输出,也没有任何错误。它看起来就像它留在那里没有给出任何输出或错误。
当updateCharacterPosition从另一个非异步方法调用时,内部的所有内容都updateCharacterPosition按缩进方式工作。
知道为什么会发生这种奇怪的行为。我该如何解决这个问题,以便我可以调用updateCharacterPosition哪个ReceiveAsync 函数?
茅侃侃
相关分类