标签:
using UnityEngine; using System.Collections; public class Global : MonoBehaviour { public static string ipAddress = "127.0.0.1"; //ip地址 public static int port = 41254; //端口号 public static int connectCount = 10; //最大连接数 }
然后,我们保存,回到Unity中,在创建一个脚本,命名为ServerUIManager ,让后,将其挂在UIRoot上,脚本,我们用来处理服务器端的事物。包括,管理UI,点击事件等。脚本如下:
using UnityEngine; using System.Collections; public class ServerUIManager : MonoBehaviour { public GameObject createServer; //创建服务器的管理对象 public GameObject showInfo; //显示客户端信息的管理对象 private UITextList clientInfo; //用来显示客服端信息的Label private bool isCreateServer = false; //能否创建服务器 private string message = ""; //信息 void Start() { clientInfo = showInfo.transform.FindChild("ClientInfo").GetComponent<UITextList>(); createServer.SetActive(true); //显示 showInfo.SetActive(false); //隐藏 } void Update() { if (Network.peerType == NetworkPeerType.Disconnected) { //创建服务器 isCreateServer = true; } else if (Network.peerType == NetworkPeerType.Server) { //服务 isCreateServer = false; OnServer(); } } //当点击了创建服务器按钮时执行, public void OnCreateServerButtonClick() { if (isCreateServer) { //可以创建,创建服务器 NetworkConnectionError error = Network.InitializeServer(Global.connectCount, Global.port, false); if (error == NetworkConnectionError.NoError) isCreateServer = false; //创建成功 else Debug.Log("创建失败" + error); } } //当服务器,初始化完成 void OnServerInitialized() { Debug.Log("Server initialized and ready"); createServer.SetActive(false); //显示 showInfo.SetActive(true); //隐藏 clientInfo.Clear(); //清空下textList } public void OnCloseServerButtonClick() { createServer.SetActive(true); //显示 showInfo.SetActive(false); //隐藏 Network.Disconnect(); //断开连接 isCreateServer = true; //重置 } //服务脚本,用来获取客户端接入信息,可以收取全部客户端发的信息 void OnServer() { int length = Network.connections.Length; //获得链接的客户端数 //遍历增加信息 for (int i = 0; i < length; i++) { clientInfo.Add("客户端:" + i ); clientInfo.Add("Ip:" + Network.connections[i].ipAddress ); clientInfo.Add("端口:" + Network.connections[i].port + "\r\n"); clientInfo.Add(message); } } [RPC] void ReciveMessage(string msg, NetworkMessageInfo info) { //刚从网络接收的数据的相关信息,会被保存到NetworkMessageInfo这个结构中 message = "发送端" + info.sender + "消息: " + msg + "\n" + info.timestamp + "\n"; //+"时间"+info.timestamp +"网络视图"+info.networkView } }
using UnityEngine; using System.Collections; public class ClientUIManager : MonoBehaviour { public GameObject connectToServer; //连接到服务器管理对象 public GameObject showMessage; //消息管理对象 private UILabel ipLabel; //用户输入的Ip private UITextList messageList; //用来展示信息 private UILabel messageLabel; //用来获取用户输入的信息 private string message = ""; //信息 private bool canConnect = false; //能否连接 private bool canSendMessage = false; //能否发送 private NetworkView nv; //NetworkView组件引用 void Start() { //获得各个引用 ipLabel = connectToServer.transform.FindChild("IpInput/Label").GetComponent<UILabel>(); messageList = showMessage.transform.FindChild("Message").GetComponent<UITextList>(); messageLabel = showMessage.transform.FindChild("MessageInput/Label").GetComponent<UILabel>(); nv = GetComponent<NetworkView>(); connectToServer.SetActive(true); showMessage.SetActive(false); } void Update() { //没有连接时,连接 if (Network.peerType == NetworkPeerType.Disconnected) { canConnect = true; } } //用户点击了连接按钮 public void OnConnectToServerButtonClick() { //检测输入是否为空,并且利用连接 if (ipLabel.text != "" && canConnect) { Global.ipAddress = ipLabel.text; //连接到对应的服务器端 NetworkConnectionError error = Network.Connect(Global.ipAddress, Global.port); if (error == NetworkConnectionError.NoError) canConnect = false; else Debug.Log("连接出错" + error); } } //连接到了服务器 void OnConnectedToServer() { Debug.Log("Connected to server"); canConnect = false; canSendMessage = true; connectToServer.SetActive(false); showMessage.SetActive(true); messageList.Clear(); //清空消息列表 } //用户点击了发送,或者按下Enter键时调用 public void OnSendMessageButtonClick() { if (canSendMessage) { message = messageLabel.text; //获得用户输入的文本 //使用RPC来同步信息 nv.RPC("ReciveMessage", RPCMode.AllBuffered,message); } } [RPC] void ReciveMessage(string msg, NetworkMessageInfo info) { //刚从网络接收的数据的相关信息,会被保存到NetworkMessageInfo这个结构中 //+"时间"+info.timestam00p +"网络视图"+info.networkView message = "发送端" + info.sender + "消息: " + msg + "\n" + info.timestamp +"\n"; messageList.Add(message); } }
使用Unity自带的NetWorkView实现简单的聊天系统
标签:
原文地址:http://www.cnblogs.com/AhrenLi/p/4896216.html