using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.Text;
using UnityEditor;
public class UDP : MonoBehaviour
{
//Udp
static UdpClient udpClient;
public static int port = 11223;
IPEndPoint remotePoint = new IPEndPoint(IPAddress.Any, 0);
public string ip = "192.168.100.104";
private void Awake()
{
udpClient = new UdpClient(port, AddressFamily.InterNetwork);
}
void Update()
{
Receive();
}
/// <summary>
/// 发送消息
/// </summary>
/// <param name="msg">信息</param>
/// <param name="receivePort">端口</param>
/// <param name="ip"></param>
public static void Send(string msg, int receivePort, string ip)
{
byte[] b_msg = Encoding.UTF8.GetBytes(msg);
udpClient.Send(b_msg, b_msg.Length, ip, receivePort);
}
void Receiv
自用工具 Unity UDP传输
使用UDP进行网络通信的Unity实现
于 2022-08-23 15:37:08 首次发布
该博客展示了如何在Unity中使用C#实现UDP网络通信。代码创建了一个UDP客户端,监听并发送数据到指定IP和端口。在`Awake`函数中初始化UDP客户端,在`Update`中接收数据,并在`Receive`方法中处理接收到的消息。

最低0.47元/天 解锁文章
3448

被折叠的 条评论
为什么被折叠?



