许可优化
产品
解决方案
服务支持
关于
软件库
当前位置:服务支持 >  软件文章 >  WinForm內嵌Unity(Socket通信)

WinForm內嵌Unity(Socket通信)

阅读数 4
点赞 0
article_banner

最近有需求要实现WinForm和Unity交互,也就是通信,查过几个方案有用UnityWebPlayer Control组件的(由于版本问题所以我没尝试),也有把Winform打包成dll动态链接库然后unity内引入的,还有打包Unity.exe然后Winform内嵌入的,后面两种都可以。

一.Winform打包成dll动态链接库然后unity内引入

        1.总之先把界面画出来(大概有个样子)

        2.后台代码(我这里是winform充当服务器,unity充当客户端来连接实现socket通信)

                2.1 Winform:建立SocketServer

  1. public class SocketServer
  2. {
  3. public Socket serverSocket;
  4. public Socket clientSocket;
  5. private string _ip = string.Empty;
  6. private int _port = 12345;
  7. /// <summary>
  8. /// 构造函数
  9. /// </summary>
  10. /// <param name="ip">监听的IP</param>
  11. /// <param name="port">监听的端口</param>
  12. public SocketServer(string ip, int port)
  13. {
  14. this._ip = ip;
  15. this._port = port;
  16. }
  17. public SocketServer(int port)
  18. {
  19. this._ip = "0.0.0.0";
  20. this._port = port;
  21. }
  22. static List<Socket> userOnline = new List<Socket>();
  23. private static readonly object textsLock;
  24. public Queue<string> texts = new Queue<string>();
  25. public void StartListen()
  26. {
  27. try
  28. {
  29. //1.0 实例化套接字(IP4寻找协议,流式协议,TCP协议)
  30. serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  31. //2.0 创建IP对象
  32. IPAddress address = IPAddress.Parse(_ip);
  33. //3.0 创建网络端口,包括ip和端口
  34. IPEndPoint endPoint = new IPEndPoint(address, _port);
  35. //4.0 绑定套接字
  36. serverSocket.Bind(endPoint);
  37. //5.0 设置最大连接数量
  38. serverSocket.Listen(int.MaxValue);
  39. //MessageBox.Show(serverSocket.LocalEndPoint.ToString());
  40. //6.0 开始监听
  41. Thread thread = new Thread(ListenClientConnect);
  42. thread.IsBackground = true;
  43. thread.Start();
  44. }
  45. catch (Exception ex)
  46. {
  47. }
  48. }
  49. /// <summary>
  50. /// 监听客户端连接
  51. /// </summary>
  52. private void ListenClientConnect()
  53. {
  54. try
  55. {
  56. while (true)
  57. {
  58. //阻塞当前的线程直到某个客户端连接,连接上则返回一个新的Socket(即客户端)
  59. clientSocket = serverSocket.Accept();
  60. userOnline.Add(clientSocket);//每连接上一个客户端,就将该客户端添加至客户端列表
  61. Thread thread = new Thread(ReceiveMessage);//每连接上一个客户端,启动一个线程(用于接受客户端信息)
  62. thread.Start(clientSocket);
  63. }
  64. }
  65. catch (Exception)
  66. {
  67. }
  68. }
  69. /// <summary>
  70. /// 接收客户端消息
  71. /// </summary>
  72. /// <param name="socket">来自客户端的socket</param>
  73. private void ReceiveMessage(object socket)
  74. {
  75. Socket clientSocket = (Socket)socket;
  76. byte[] buffer = new byte[1024 * 1024 * 2];
  77. while (true)
  78. {
  79. try
  80. {
  81. //获取从客户端发来的数据
  82. int length = clientSocket.Receive(buffer);
  83. lock (textsLock)//如果textsLock没有被锁上,则可执行内部代码并主动锁上(PS:有序的执行,避免资源冲突)
  84. {
  85. texts.Enqueue(Encoding.UTF8.GetString(buffer, 0, length));//接收到消息则存入texts
  86. }
  87. //Console.WriteLine("接收客户端{0},消息{1}", clientSocket.RemoteEndPoint.ToString(), Encoding.UTF8.GetString(buffer, 0, length));
  88. }
  89. catch (Exception ex)
  90. {
  91. Console.WriteLine(ex.Message);
  92. clientSocket.Shutdown(SocketShutdown.Both);
  93. clientSocket.Close();
  94. break;
  95. }
  96. }
  97. }
  98. }

                2.2 Winform:在Form加载时开启服务,线程监听客户端(unity)连接 

        p

免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删
相关文章
QR Code
微信扫一扫,欢迎咨询~

联系我们
武汉格发信息技术有限公司
湖北省武汉市经开区科技园西路6号103孵化器
电话:155-2731-8020 座机:027-59821821
邮件:tanzw@gofarlic.com
Copyright © 2023 Gofarsoft Co.,Ltd. 保留所有权利
遇到许可问题?该如何解决!?
评估许可证实际采购量? 
不清楚软件许可证使用数据? 
收到软件厂商律师函!?  
想要少购买点许可证,节省费用? 
收到软件厂商侵权通告!?  
有正版license,但许可证不够用,需要新购? 
联系方式 155-2731-8020
预留信息,一起解决您的问题
* 姓名:
* 手机:

* 公司名称:

姓名不为空

手机不正确

公司不为空