C# tcp/ip接收字节并转换为纹理
我有一个小问题。我有另一个程序/软件,可以将图像转换为字节并发送到计算机上。所以我现在需要做的是在Unity中捕获这些字节,并将它们转换回图像,并将其设置为纹理 我已经通过TCP/IP系统与其他软件建立了连接,连接正在工作,其他软件正在发送数据,但我不知道如何将这些字节转换为imgC# tcp/ip接收字节并转换为纹理,c#,unity3d,C#,Unity3d,我有一个小问题。我有另一个程序/软件,可以将图像转换为字节并发送到计算机上。所以我现在需要做的是在Unity中捕获这些字节,并将它们转换回图像,并将其设置为纹理 我已经通过TCP/IP系统与其他软件建立了连接,连接正在工作,其他软件正在发送数据,但我不知道如何将这些字节转换为img Debug.Log(“接收的客户端消息为:”+clientMessage) 这只是一个测试,所以我可以看到数据正在通过 这是我的密码 img.LoadRawTextureData(Loader); img.App
Debug.Log(“接收的客户端消息为:”+clientMessage)代码>
这只是一个测试,所以我可以看到数据正在通过
这是我的密码
img.LoadRawTextureData(Loader);
img.Apply();
GameObject.Find("Plane").GetComponent<Renderer>().material.mainTexture = img;
您是否尝试将字节传递到LoadRawTextureData
private byte[] ListenForIncommingRequests()
{
try
{
tcpListener = new TcpListener(IPAddress.Parse("127.0.0.1"), 35800);
tcpListener.Start();
Debug.Log("Server is listening");
Byte[] bytes = new Byte[1024];
while (true)
{
using (connectedTcpClient = tcpListener.AcceptTcpClient())
{
using (NetworkStream stream = connectedTcpClient.GetStream())
{
int length;
while ((length = stream.Read(bytes, 0, bytes.Length)) != 0)
{
var incommingData = new byte[length];
Array.Copy(bytes, 0, incommingData, 0, length);
Loader = incommingData;
string clientMessage = Encoding.ASCII.GetString(incommingData);
Debug.Log("client message received as: " + clientMessage);
}
}
}
}
}
catch (SocketException socketException)
{
Debug.Log("SocketException " + socketException.ToString());
}
return bytes;
}
这样称呼它
var result = ListenForIncommingRequests();
img.LoadRawTextureData(result);
img.Apply();
GameObject.Find("Plane").GetComponent<Renderer>().material.mainTexture = img;
var result=listenforincomingrequests();
img.LoadRawTextureData(结果);
img.Apply();
GameObject.Find(“平面”).GetComponent().material.mainTexture=img;
也许您可以提供更多信息:isimg.LoadRawTextureData(Loader)代码>或img.Apply()代码>抛出错误?什么是tpyeimg
<代码>纹理2D
?在哪里调用img.Apply()
?img.LoadRawTextureData(loader);img.Apply()代码>它们都不会抛出错误。我在Update()的内部调用这两个函数。img是纹理2我认为问题在于您试图将图像
加载为纹理2D
。在Unity 5.x中有一个函数Texture2D.LoadImage(bytes)
。我在当前的文档中找不到这个函数……是的,我试图将字节传递给LoadRawTextureData,但没有成功。而ListenForIncomingRequests()
是TcpClient请求的处理程序,因此这不起作用
var result = ListenForIncommingRequests();
img.LoadRawTextureData(result);
img.Apply();
GameObject.Find("Plane").GetComponent<Renderer>().material.mainTexture = img;