我将Unity 3D用作使用TCP和UDP套接字的C#应用程序的客户端。
这是一个功能:
public void updateCharacterPosition(CharacterPosition position, int characterId, int cnnId)
{
if (Character.SceneLoaded != true)
return;
if (cnnId != ClientWorldServer.connectionId)
{
Debug.Log("HERE!");
GameObject startPoint = GameObject.Find("CharactersOnline/" + characterId.ToString());
Debug.Log("HERE 1: " + GameObject.Find("CharactersOnline").transform.name);
Debug.Log("HERE 2: " + startPoint.transform.name);
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[characterId].characterPosition.x = position.x;
Character.characterDetails[characterId].characterPosition.y = position.y;
Character.characterDetails[characterId].characterPosition.z = position.z;
//Destroy(endPoint);
}
}
这里有两个函数正在调用此函数:
接收TCP数据的函数:
private void OnIncomingData(string data)
{
JsonData json = JsonConvert.DeserializeObject<JsonData>(data);
string prefix = json.header.Substring(0, 2);
if (prefix != "0x")
{
Debug.Log("Unknown packet: " + data + "\n");
}
else
{
string header = json.header.Substring(2);
int conId = json.connectionId;
//Debug.Log("Header:" + header);
switch (header)
{
default:
Debug.Log("Unknown packet: " + data + "\n");
break;
case "000":
Debug.Log("Empty header received: " + json.header + "\n");
break;
case "001":
if (connectionId == 0)
{
connectionId = conId;
Debug.Log("Connection ID: " + connectionId + "\n");
//After we have connected to TCP Stream now let's connect to UDP.
ConnectUDP();
}
break;
case "004":
DisplayCharacterSelects(json.data, connectionId);
break;
case "005":
CharacterData charData = new CharacterData();
string jsonData = JsonConvert.SerializeObject(json.data, Formatting.None);
charData = JsonConvert.DeserializeObject<CharacterData>(jsonData);
playerSpawner.SpawnPlayer(playerSpawner,charData);
break;
case "006":
playerSpawner.SpawnOnlinePlayers(playerSpawner,json.data);
break;
case "007":
CharacterLogout(json.data);
break;
case "008":
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 "009":
character.updateCharacterRotation(json.data, conId);
break;
}
Debug.Log("World Server: " + data + " TIME: " + Timer);
}
}
这是接收UDP数据的函数:
private void UDPReceive()
{
_socket.BeginReceiveFrom(state.buffer, 0, bufSize, SocketFlags.None, ref epFrom, recv = (ar) =>
{
State so = (State)ar.AsyncState;
int bytes = _socket.EndReceiveFrom(ar, ref epFrom);
_socket.BeginReceiveFrom(so.buffer, 0, bufSize, SocketFlags.None, ref epFrom, recv, so);
Debug.Log("UDP World: " + Encoding.ASCII.GetString(so.buffer, 0, bytes));
string receivedData = Encoding.ASCII.GetString(so.buffer, 0, bytes);
JsonData json = JsonConvert.DeserializeObject<JsonData>(receivedData);
string prefix = json.header.Substring(0, 2);
if (prefix != "3x")
{
Debug.Log("Unknown packet: " + receivedData + "\n");
}
else
{
string header = json.header.Substring(2);
int conId = json.connectionId;
//Debug.Log("Header:" + header);
switch (header)
{
default:
Debug.Log("Unknown packet: " + receivedData + "\n");
break;
case "001":
Debug.Log("Data received: " + receivedData + "\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":
character.updateCharacterRotation(json.data, conId);
break;
}
}
}, state);
}
发生了什么事。当从updateCharacterPosition()
(即TCP侦听器)中调用OnIncomingData
时,该功能将按预期工作100%。
但是,从updateCharacterPosition()
调用函数UDPReceive()
时,函数updateCharacterPosition()
似乎正在GameObject startPoint = GameObject.Find("CharactersOnline/" + characterId.ToString());
处停止执行
行:
Debug.Log("HERE 1: " + GameObject.Find("CharactersOnline").transform.name);
Debug.Log("HERE 2: " + startPoint.transform.name);
未显示任何结果。好像他们不在那里。 Unity控制台中未显示任何错误,就像找不到对象或其他东西一样,而Debug.Log
仍未显示任何内容。您在控制台中唯一看到的是以下行:
Debug.Log("HERE!");
此行下面的所有内容似乎都无法正常工作,也不会引发任何错误。您是否知道为什么会发生此问题以及如何解决?
函数OnIncomingData
和UDPReceive
属于同一类。
答案 0 :(得分:0)
我不确定为什么UDP版本不起作用,但是我可以阐明为什么某些代码部分没有运行。
Unity具有可以使用的“有用”功能,该功能在每次执行遇到错误时都会终止程序,它将结束当前指令集并在下一个可能的点继续执行,而不会向程序提供任何错误。安慰。至于为什么他们没有提供错误,我无法告诉您,但他们没有提供。
因此,就您的程序而言,代码可能会在以下行崩溃:
GameObject startPoint = GameObject.Find("CharactersOnline/" + characterId.ToString());
当尝试获取GameObject时。在这种情况下,您的代码崩溃,Unity结束执行,然后继续运行它可以从Monobehavior类运行的下一个可能的事物,同时假装没有任何问题。
我的修正建议是将其放入try / catch块中,并在失败时打印。假设我是对的,那您应该能够看到此时打印的消息,并根据当时可以访问的数据开始调试。