gpt4 book ai didi

c# - 在 Unity 插件内的 C# 脚本中不触发回调

转载 作者:行者123 更新时间:2023-11-30 15:52:16 30 4
gpt4 key购买 nike

我有一个 C# 脚本,我正试图将其放入 Unity 插件中。

该脚本本质上只是创建一个套接字连接、接收数据并解析它。在 Unity 中用作“独立”脚本时,它可以完美运行。

这是脚本的代码:

using UnityEngine;
using System.Collections;
using System;
using System.Net;
using System.Net.Sockets;
using System.Linq;

public class ClientSocket
{
private Socket _clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
private byte[] _recieveBuffer = new byte[8142];

void Start()
{
SetupServer();
}

void Update()
{

}

void OnApplicationQuit()
{
_clientSocket.Close();
}

private void SetupServer()
{
try
{
_clientSocket.Connect("127.0.0.1", 12345);

}
catch (SocketException ex)
{
Debug.Log(ex.Message);
}

_clientSocket.BeginReceive(_recieveBuffer, 0, _recieveBuffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);

}

private void ReceiveCallback(IAsyncResult AR)
{
//Check how much bytes are recieved and call EndRecieve to finalize handshake
int recieved = _clientSocket.EndReceive(AR);

if (recieved <= 0)
return;

//Copy the recieved data into new buffer, to avoid null bytes
byte[] recData = new byte[recieved];
Buffer.BlockCopy(_recieveBuffer, 0, recData, 0, recieved);

string incomingData = System.Text.Encoding.Default.GetString(_recieveBuffer);
Debug.Log(incomingData);
}
}

正如我所提到的,这在作为脚本时效果很好 --- 创建连接,我的 ReceiveCallback 触发,并从套接字连接接收数据。

现在,当我尝试在 Unity 插件中使用基本相同的代码时,会创建连接;但是回调永远不会触发......对于为什么这可能有所不同有点不知所措。任何想法或指示表示赞赏!

这是用于插件的略微修改版本:

using UnityEngine;
using System.Collections;
using System;
using System.Net;
using System.Net.Sockets;
using System.Linq;

namespace InputsPlugin
{
public class ClientSocket
{
private Socket _clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
private byte[] _recieveBuffer = new byte[8142];

public void StartConnection()
{
SetupServer();
}

private void SetupServer()
{
try
{
_clientSocket.Connect("127.0.0.1", 12345);
Debug.Log("Connected!!!!");
}
catch (SocketException ex)
{
Debug.Log(ex.Message);
}

Debug.Log("Callback?");
_clientSocket.BeginReceive(_recieveBuffer, 0, _recieveBuffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
Debug.Log("Callback?");
}

private void ReceiveCallback(IAsyncResult AR)
{
Debug.Log("Callback!!!!");

//Check how much bytes are recieved and call EndRecieve to finalize handshake
int recieved = _clientSocket.EndReceive(AR);
Debug.Log(recieved);

if (recieved <= 0)
return;

//Copy the recieved data into new buffer, to avoid null bytes
byte[] recData = new byte[recieved];
Buffer.BlockCopy(_recieveBuffer, 0, recData, 0, recieved);

string incomingData = System.Text.Encoding.Default.GetString(_recieveBuffer);
Debug.Log(incomingData);

//Start receiving again
_clientSocket.BeginReceive(_recieveBuffer, 0, _recieveBuffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
}

private void SendData(byte[] data)
{
SocketAsyncEventArgs socketAsyncData = new SocketAsyncEventArgs();
socketAsyncData.SetBuffer(data, 0, data.Length);
_clientSocket.SendAsync(socketAsyncData);
}
}
}

最佳答案

您的 Start 方法很可能应该转换为构造函数,因为引擎不再调用它。作为私有(private)方法,无法从外部调用。

因此,您的 SetupServer 可能从未被调用过。

您也可以删除 Update 和 OnApplicationQuit 应该是析构函数或一些清理方法。

关于c# - 在 Unity 插件内的 C# 脚本中不触发回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55016901/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com