chatroom简易聊天室(Unity3d)

代码:

using UnityEngine;
using System.Collections;

public class chatroom : MonoBehaviour {


	public string remoteIP= "127.0.0.1";
	public int remotePort= 25000;
	public int listenPort= 25000;
	public string remoteGUID= "";
	public bool useNat= false;
	string MyName="";//用户名字
	string Inputword="";//输入的信息
	string Outputword="";//聊天的所有对话

	void  OnGUI (){

		if (Network.peerType == NetworkPeerType.Disconnected)//脱机状态
		{
			useNat = GUILayout.Toggle(useNat, "Use NAT punchthrough");

			GUILayout.BeginVertical();
			if (GUILayout.Button ("Connect"))//连接
			{
				if (useNat)
				{
					if (remoteGUID == null)
						Debug.LogWarning("Invalid GUID given");
					else
						Network.Connect(remoteGUID);
				}
				else
				{
					Network.Connect(remoteIP, remotePort);
				}
				
			}
			if (GUILayout.Button ("Start Server"))//打开服务器
			{
				Network.InitializeServer(32, listenPort, useNat);
			}
			GUILayout.EndVertical();
			if (useNat)
			{
				remoteGUID = GUILayout.TextField(remoteGUID, GUILayout.MinWidth(145));
			}
			else
			{
				remoteIP = GUILayout.TextField(remoteIP, GUILayout.MinWidth(100));
				remotePort = int.Parse(GUILayout.TextField(remotePort.ToString()));
			}
		}
		else
		{
			if(Network.isServer){//服务器显示信息				if (useNat){
					GUI.Label(new Rect(30,0,500,30),"Server GUID: " + Network.player.guid);
				}
				GUI.Label(new Rect(30,12,500,30),"Server Local IP/port: " + Network.player.ipAddress + "/" + Network.player.port);
				GUI.Label(new Rect(30,24,500,30),"Server External IP/port: " + Network.player.externalIP + "/" + Network.player.externalPort);
			}
			if (GUI.Button (new Rect(300,0,100,30),"Disconnect")){
				Network.Disconnect(200);//脱机
			}
			GUI.Label(new Rect(30,50,100,30),"Name:");
			MyName=GUI.TextField( new Rect(70,48,100,30),MyName,10);//输入性名
			Outputword=GUI.TextArea( new Rect(30,80,400,300),Outputword,1000);//显示所有对话
			Inputword=GUI.TextField( new Rect(30,400,300,60),Inputword,200);//输入对话
			if(GUI.Button( new Rect(350,400,70,60),"send")){
				networkView.RPC("SomeoneSay",RPCMode.All,Inputword,MyName);//远程调用SomeoneSay
				Inputword="";//清空
			}
					
	
		}
	}
	
	void  OnServerInitialized (){
		if (useNat)
			Debug.Log("==> GUID is " + Network.player.guid + ". Use this on clients to connect with NAT punchthrough.");
		Debug.Log("==> Local IP/port is " + Network.player.ipAddress + "/" + Network.player.port + ". Use this on clients to connect directly.");
	}
	
	void  OnConnectedToServer (){
		print ("Connected!!");
	}
	
	void  OnDisconnectedFromServer (){
		if (this.enabled != false)
			Application.LoadLevel(Application.loadedLevel);		
	}
	[RPC]
	void  SomeoneSay ( string message ,  string name  ){
	 	Outputword =Outputword+"\n"+ name + ":  " + message;//将收到的字符串message显示到画面上
	}
}

然后创建一个对象:chatroom 将此代码拖到对象上 在inspector窗口为对象添加"Network View"

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值