• unity 接收和发送Udp消息


    因为需要用到unity和其他的程序交互,其他程序可以提供Udp消息,因此找了合适的相互连接方法。这里直接上代码。

    工具类:

    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.IO;
    5. using System.Net;
    6. using System.Net.Sockets;
    7. using System.Text;
    8. using System.Threading;
    9. using UnityEngine;
    10. using UnityEngine.Video;
    11. public class UdpManager
    12. {
    13. public static string m_receivedMessage;
    14. static IPEndPoint m_IPEndPoint;
    15. static UdpClient m_udpClient;
    16. public static void InitializeUdpClient()
    17. {
    18. m_IPEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8080);
    19. m_udpClient = new UdpClient(m_IPEndPoint);
    20. UdpModel s = new UdpModel(m_udpClient, m_IPEndPoint);
    21. m_udpClient.BeginReceive(EndReceive, s);
    22. Debug.Log("服务器启动");
    23. }
    24. //结束得到的信息
    25. private static void EndReceive(IAsyncResult ar)
    26. {
    27. try
    28. {
    29. UdpModel m_UdpModel = ar.AsyncState as UdpModel;
    30. if (m_UdpModel != null)
    31. {
    32. UdpClient udpClient = m_UdpModel.m_udpclient;
    33. IPEndPoint ip = m_UdpModel.m_ip;
    34. Byte[] receiveBytes = udpClient.EndReceive(ar, ref ip);
    35. string msg = Encoding.UTF8.GetString(receiveBytes);
    36. m_receivedMessage = msg;
    37. udpClient.BeginReceive(EndReceive, m_UdpModel); //开始获取
    38. }
    39. }
    40. catch (Exception ex)
    41. {
    42. //处理异常
    43. }
    44. }
    45. //udp模型
    46. private class UdpModel
    47. {
    48. public UdpClient m_udpclient = null;
    49. public IPEndPoint m_ip;
    50. public UdpModel(UdpClient udpclient, IPEndPoint ip)
    51. {
    52. this.m_udpclient = udpclient;
    53. this.m_ip = ip;
    54. }
    55. }
    56. //关闭
    57. public static void Close()
    58. {
    59. if (m_udpClient != null)
    60. {
    61. m_udpClient.Close();
    62. m_udpClient = null;
    63. }
    64. }
    65. ///
    66. /// 发送数据
    67. ///
    68. ///
    69. public static void SendMessage(string message)
    70. {
    71. UdpClient myUdpClient = new UdpClient();
    72. IPEndPoint endpoint;
    73. //当前服务器ip和端口号
    74. myUdpClient = new UdpClient(new IPEndPoint(IPAddress.Any, 8800));
    75. //要发送给的地址和端口号,255.255.255.255表示在这个局域网的所有ip
    76. endpoint = new IPEndPoint(IPAddress.Parse("192.168.31.174"), 1180);
    77. byte[] bytes = Encoding.UTF8.GetBytes(message);
    78. try
    79. {
    80. myUdpClient.Send(bytes, bytes.Length, endpoint);
    81. myUdpClient.Close();
    82. }
    83. catch (Exception err)
    84. {
    85. Console.Write(err.Message, "发送失败");
    86. }
    87. finally
    88. {
    89. myUdpClient.Close();
    90. }
    91. }
    92. }

    需要挂载运行的脚本:

    1. using System.Collections.Generic;
    2. using UnityEngine.UI;
    3. using UnityEngine;
    4. using UnityEngine.Video;
    5. ///
    6. /// 服务接收生成
    7. ///
    8. public class ServerControl : MonoBehaviour
    9. {
    10. void Start()
    11. {
    12. UdpManager.InitializeUdpClient();
    13. //part1Root.SetActive(true);
    14. //part2Root.SetActive(false);
    15. }
    16. void Update()
    17. {
    18. if (UdpManager.m_receivedMessage != null)
    19. {
    20. string[] array = UdpManager.m_receivedMessage.Split(',');
    21. Debug.Log(UdpManager.m_receivedMessage);
    22. UdpManager.m_receivedMessage = null;
    23. }
    24. }
    25. private void OnDestroy()
    26. {
    27. UdpManager.Close();
    28. }
    29. }

    使用方法很简单,把ServerControl脚本挂载在一个物体上,直接运行即可,接受信息的方法和发送的方法都在两个脚本里。

  • 相关阅读:
    程序环境和预处理
    html 中 title与h1,b与strong,i与em区别?
    【实战】jenkins api 接口
    靠这份 Java 面试宝典,直接跳槽到阿里
    二维下,行政区划借助 geoserver样式配置,实现伪3d效果
    企业如何搭建智能客服系统?
    e签宝,再「进化」
    【BMS软开系列】1、 ISO 26262功能安全标准 (一)
    Myvatis关联关系映射与表对象之间的关系
    8款好用的电脑监控软件分享
  • 原文地址:https://blog.csdn.net/weixin_41573444/article/details/134318368