如何用Unity实现局域网聊天室功能
发表于:2025-11-12 作者:千家信息网编辑
千家信息网最后更新 2025年11月12日,本篇内容介绍了"如何用Unity实现局域网聊天室功能"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!整
千家信息网最后更新 2025年11月12日如何用Unity实现局域网聊天室功能
本篇内容介绍了"如何用Unity实现局域网聊天室功能"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
整体过程分为两部分:构建服务端、构建客户端。
服务端:
大概思路:
1. 声明Socket连接以及绑定IP和端口,这里面使用
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Net.Sockets;using System.Net;namespace ServerApplication{ class Program { public static string IP; public static int Port; static List clientList = new List(); static Socket serverSocket; static void Main(string[] args) { //绑定IP和端口 BindIPAndPort(); // while (true) { Socket clientSocket = serverSocket.Accept(); Client client = new Client(clientSocket); clientList.Add(client); Console.WriteLine("一台主机进入连接"); } } /// /// 广播数据 /// public static void BroadcostMSG(string s) { List NotConnectedList = new List(); foreach (var item in clientList) { if(item.IsConnected) { item.SendMSG(s); } else { NotConnectedList.Add(item); } } foreach (var item in NotConnectedList) { clientList.Remove(item); } } /// /// 绑定IP和端口 /// public static void BindIPAndPort() { //创建一个serverSocket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //声明IP和端口 Console.WriteLine("输入IP地址:"); IP = Console.ReadLine(); string ipStr = IP; Console.WriteLine("请输入端口:"); Port = int.Parse(Console.ReadLine()); int port = Port; IPAddress serverIp = IPAddress.Parse(ipStr); EndPoint serverPoint = new IPEndPoint(serverIp, port); //socket和ip进行绑定 serverSocket.Bind(serverPoint); //监听最大数为100 serverSocket.Listen(100); } }} using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Net.Sockets;using System.Threading;namespace ServerApplication{ class Client { public Socket clientSocket; //声明一个线程用于接收信息 Thread t; //接收信息所用容器 byte[] data = new byte[1024]; //构造函数 public Client(Socket s) { clientSocket = s; t = new Thread(ReceiveMSG); t.Start(); } /// /// 接收数据 /// void ReceiveMSG() { while(true) { if (clientSocket.Poll(10,SelectMode.SelectRead)) { break; } data = new byte[1024]; int length = clientSocket.Receive(data); string message = Encoding.UTF8.GetString(data, 0, length); Program.BroadcostMSG(message); Console.WriteLine("收到消息:" + message); } } /// /// 发送数据 /// /// public void SendMSG(string message) { byte[] data = Encoding.UTF8.GetBytes(message); clientSocket.Send(data); } //判断此Client对象是否在连接状态 public bool IsConnected { get { return clientSocket.Connected; } } }}客户端:
a.UI界面
UI界面是使用UGUI实现的
登录用户可以自己取名进行登录(发言时用于显示),使用时需要输入服务端的IP地址和端口号

下面是聊天室的页面,在输入框内输入要发送的消息,点击Send,将信息发送出去

这是服务端的信息
b.关于客户端的脚本
(1)这是ClientManager,负责与服务端进行连接,通信
using System.Collections;using System.Collections.Generic;using UnityEngine;using System.Net.Sockets;using System.Net;using System.Text;using UnityEngine.UI;using System.Threading;public class ClientManager : MonoBehaviour{ //ip:192.168.1.7 public string ipAddressstr; public int port; public Text ipTextToShow; //Socket private Socket ClientServer; //文本输入框 public InputField inputTxt; public string inputMSGStr; //接收 Thread t; public Text receiveTextCom; public string message; // Use this for initialization void Start() { ipTextToShow.text = ipAddressstr; // ConnectedToServer(); } // Update is called once per frame void Update() { if (message != null && message != "") { receiveTextCom.text = receiveTextCom.text + "\n" + message; message = ""; } } /// /// 连接服务器 /// public void ConnectedToServer() { ClientServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //声明IP地址和端口 IPAddress ServerAddress = IPAddress.Parse(ipAddressstr); EndPoint ServerPoint = new IPEndPoint(ServerAddress, port); ipAddressstr = IpInfo.ipStr; port = IpInfo.portStr; //开始连接 ClientServer.Connect(ServerPoint); t = new Thread(ReceiveMSG); t.Start(); } /// /// 接收消息 /// /// "string" void ReceiveMSG() { while (true) { if (ClientServer.Connected == false) { break; } byte[] data = new byte[1024]; int length = ClientServer.Receive(data); message = Encoding.UTF8.GetString(data, 0, length); //Debug.Log("有消息进来"); } } /// /// 发送string类型数据 /// /// public void SendMSG() { Debug.Log("button Clicked"); //message = "我:" + inputTxt.text; inputMSGStr = inputTxt.text; byte[] data = Encoding.UTF8.GetBytes(IpInfo.name+":"+inputMSGStr); ClientServer.Send(data); } private void OnDestroy() { ClientServer.Shutdown(SocketShutdown.Both); ClientServer.Close(); } private void OnApplicationQuit() { OnDestroy(); }}(2)SceneManager,用于场景切换,这里只是利用GameObject进行SetActive()来实现,并不是创建了单独的Scene进行管理。
using System.Collections;using System.Collections.Generic;using UnityEngine;public class SceneManager : MonoBehaviour { public GameObject loginPanel; public GameObject communicatingPanel; // Use this for initialization public void OnSwitch() { loginPanel.SetActive(false); communicatingPanel.SetActive(true); }}(3)LogInPanel和IPInfo,一个挂载在登录界面上,一个是数据模型,用于存储数据。
using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.UI;public class LogInPanel : MonoBehaviour { public Text nameInputTxt; public Text ipInputTxt; public Text portInputTxt; //private string name; //private string ipStr; //private string portStr; public void OnLogInClick() { IpInfo.name = nameInputTxt.text; IpInfo.ipStr = ipInputTxt.text; IpInfo.portStr = int.Parse(portInputTxt.text); }}public static class IpInfo { public static string name; public static string ipStr; public static int portStr;}"如何用Unity实现局域网聊天室功能"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!
数据
端口
服务
输入
信息
消息
聊天室
地址
客户
界面
端的
登录
功能
局域
局域网
内容
客户端
更多
知识
过程
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
数据库系统结构可以分为几层
梦幻诛仙手游服务器
鸣蚕(上海)网络技术有限公司
拍图解题软件开发
有什么好的数据库管理软件
合肥口碑好的网络技术开发概况
北京华腾大厦软件开发
连接数据库的四个步骤
游戏麻将软件开发
java服务器端开发前景
辽宁费用管理软件开发
java数据库备份
网络技术综合题第一题
软件开发基本设计原则
数据库系统分析与设计
服务器 加显卡
中伟网络技术有限公司
洛宁软件开发选哪家
五行山服务器什么时候更新
开展中学网络技术类课程
航空网络安全保险
导出数据库对象
qq网络安全概念
社区开展网络安全感调查
计算机网络技术大专薪资待遇
计算机数据库版本
21世纪互联网科技成就
文物信息大数据库
服务器里文件能直接查看吗
五行山服务器什么时候更新