我正在使用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(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());
position.z = float.Parse(json.data["position.z"].ToString());
Character.updateCharacterPosition(position, Convert.ToInt32(json.data["characterId"].ToString()), conId);
break;
case "003":
ClientWorldServer.character.updateCharacterRotation(json.data, conId);
break;
}
}
} catch (SocketException e)
{
Debug.Log("UDP Socket Exception: " + e);
}
udpClient.BeginReceive(Receive, null);
}
以下是当从调用时不能正常工作的函数
Receive()
:
public static void updateCharacterPosition(CharacterPosition position, int characterId, int cnnId)
{
if (Character.SceneLoaded != true)
return;
Debug.Log("Position 2: X: " + position.x + " Y: " + position.y + " Z: " + position.z);
Debug.Log("GameObject FIND: " + GameObject.Find("CharactersOnline").name);
if (cnnId != ClientWorldServer.connectionId)
{
GameObject startPoint = GameObject.Find("CharactersOnline/" + characterId.ToString());
GameObject endPoint = new GameObject();
endPoint.transform.position = new Vector3(position.x, position.y, position.z);
GameObject.Find("CharactersOnline/" + characterId.ToString()).transform.position = Vector3.Lerp(startPoint.transform.position, endPoint.transform.position, Time.deltaTime);
//Updating Clients ram of the character's position
Character.characterDetails[int.Parse(characterId.ToString())].characterPosition.x = position.x;
Character.characterDetails[int.Parse(characterId.ToString())].characterPosition.y = position.y;
Character.characterDetails[int.Parse(characterId.ToString())].characterPosition.z = position.z;
//Destroy(endPoint);
}
}
在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
从另一个方法调用,该方法不是异步的
更新字符位置
以缩进方式工作。
知道为什么会发生这种奇怪的行为吗?我该怎么修才能打电话
更新字符位置
从
Receive
哪个是异步函数?