用Qt编写的窗口与Unity3D通过Tcp进行通信,通信过程中难免发送一些中文字符,下面简单介绍一下:
Qt版本:5.9.9 , 建立TcpServer
unity3D 建立TcpSocket , 通过固定端口进行通信
Qt与Unity3D通信发生中文乱码问题,主要在以下方面:
1. 在Qt main.cpp 中添加以下设置
QTextCodec::setCodecForLocale(QTextCodec::codecForName("GBK"));
GBK要与Unity3D中的保持一致,GB2312也是可以的,UTF-8不可以。
2. Qt发送数据
mCommondSocket->write(command.toLocal8Bit());
发送的字符串直接转化为QByteArray发送,转化为char*发送,是会乱码的。
3.示例截图
通过Qt发送一个视频地址,在Unity3D上进行视频播放,也会打印出接收的数据。


videoPath代表视频地址:前面的BG代表标识;MuTcpSocketObject是tcp通信.


好了,都已经介绍完毕,希望能帮到大家。
网友评论