unity 调用c语言,C/Unity-未能从异步方法调用Unity方法

我正在使用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

哪个是异步函数?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值