Unity网络通信(三)同步物体

     在上两篇的基础上,这次我们要做物体同步。使两个物体在两个机器上显示同样的移动效果。这里,使用W、S、A、D四个键位实现前后左右的移动。

注意:如果您复制粘贴代码,千万注意文件编码!最好为ANSI,否则会报出各种各样奇怪的错误!

 

步骤:

1,首先建立一个新工程。

2,添加两个cube,分别重命名为Cube01,和Cube02。这两个cube就是我们要同步的对象。

3,添加一个平行光源,Directional light。否则你在漆黑的场景中,真的很难找到你的cube。

4,调整摄像机、cube、Directional light 的相对位置,使他们在视野内,可以看着它们运行。

5,create一个C#脚本cube_move,如下:

using UnityEngine;
using System.Collections;

public class cube_move : MonoBehaviour {
	
	string IP = "192.168.1.6";
   	int Port = 10100;
	
	int moveSpeed = 16;
	
	GameObject cube01 = null;
	GameObject cube02 = null;
	
	GameObject myself = null;
	GameObject another = null;
	
	// Use this for initialization
	void Start () {
		cube01 = GameObject.Find("Cube01");
		cube02 = GameObject.Find("Cube02");
	}
	
	//OnGUI方法,所有GUI的绘制都需要在这个方法中实现
	void OnGUI(){
		//Network.peerType是端类型的状态:
		//即disconnected, connecting, server 或 client四种
		switch(Network.peerType){
			//禁止客户端连接运行, 服务器未初始化
			case NetworkPeerType.Disconnected:
				StartServer();
			    break;
			//运行于服务器端
			case NetworkPeerType.Server:
				OnServer();
			    break;
			//运行于客户端
			case NetworkPeerType.Client:
			    break;
			//正在尝试连接到服务器
			case NetworkPeerType.Connecting:
				break;
		}
	}
	
	void StartServer(){
		GUILayout.Label("同步测试:");
		//当用户点击按钮的时候为true
		if (GUILayout.Button("创建服务器")) {
			//初始化本机服务器端口,第一个参数就是本机接收多少连接
			NetworkConnectionError error = Network.InitializeServer(12,Port,false);
			//连接状态
			switch(error){
				case NetworkConnectionError.NoError:
				    break;
			    default:
			        Debug.Log("服务端错误"+error);
				    break;
			}
		}
		if (GUILayout.Button("连接服务器")){
			NetworkConnectionError error = Network.Connect(IP,Port);
			//连接状态
			switch(error){
				case NetworkConnectionError.NoError:
				    break;
			    default:
			        Debug.Log("客户端错误"+error);
				    break;
			}
		}
	}
	
	void OnServer(){
		GUILayout.Label("服务端已经运行,等待客户端连接");
		//Network.connections是所有连接的玩家, 数组[]
		//取客户端连接数. 
		int length = Network.connections.Length;
		//按数组下标输出每个客户端的IP,Port
		for (int i=0; i<length; i++)
		{
			GUILayout.Label("客户端"+i);
			GUILayout.Label("客户端ip"+Network.connections[i].ipAddress);
			GUILayout.Label("客户端端口"+Network.connections[i].port);
			GUILayout.Label("-------------------------------");
		}
		//当用户点击按钮的时候为true
		if (GUILayout.Button("断开服务器")){
			Network.Disconnect();
		}
	}
	
	//接收请求的方法. 注意要在上面添加[RPC]
	[RPC]
	void ProcessMove(string msg, NetworkMessageInfo info){
		//刚从网络接收的数据的相关信息,会被保存到NetworkMessageInfo这个结构中
		string sender = info.sender.ToString();
		//看脚本运行在什么状态下
		NetworkPeerType status = Network.peerType;
		if (status == NetworkPeerType.Server) 
		{
			myself = cube01;  //假如运行在server状态下, 那么自己就是cube1
			another = cube02;
			
		}
		else
		{
			myself = cube02;  //假如运行在client状态下, 那么自己就是cube2
			another = cube01;
		}
		//假如是自己发送的信息
		if (sender == "-1")
		{
			if (msg == "W")
			{
				myself.transform.Translate(Vector3.forward * Time.deltaTime * moveSpeed);
			}
			if (msg == "S")
			{
				myself.transform.Translate(Vector3.back * Time.deltaTime * moveSpeed);
			}
			if (msg == "A")
			{
				myself.transform.Translate(Vector3.left * Time.deltaTime * moveSpeed);
			}
			if (msg == "D")
			{
				myself.transform.Translate(Vector3.right * Time.deltaTime * moveSpeed);
			}
			
		}
		//假如是别人发送的信息
	    else
		{
			if (msg == "W")
			{
				another.transform.Translate(Vector3.forward * Time.deltaTime * moveSpeed);
			}
			if (msg == "S")
			{
				another.transform.Translate(Vector3.back * Time.deltaTime * moveSpeed);
			}
			if (msg == "A")
			{
				another.transform.Translate(Vector3.left * Time.deltaTime * moveSpeed);
			}
			if (msg == "D")
			{
				another.transform.Translate(Vector3.right * Time.deltaTime * moveSpeed);
			}
		}
	}
	// Update is called once per frame
	void Update () 
	{
		//前移
		if (Input.GetKeyDown(KeyCode.W))
		{
			Debug.Log("wwwwwwwww|");
			networkView.RPC("ProcessMove", RPCMode.All, "W");
		}
		if (Input.GetKey(KeyCode.W))
		{
			networkView.RPC("ProcessMove", RPCMode.All, "W");
		}
		//后退
	    if (Input.GetKeyDown(KeyCode.S))
		{
			Debug.Log("sssssssss!");
			networkView.RPC("ProcessMove", RPCMode.All, "S");
		}
		if (Input.GetKey(KeyCode.S))
		{
			networkView.RPC("ProcessMove", RPCMode.All, "S");
		}
		//向左移动
		if (Input.GetKeyDown(KeyCode.A))
		{
			networkView.RPC("ProcessMove", RPCMode.All, "A");
		}
		if (Input.GetKey(KeyCode.A))
		{
			networkView.RPC("ProcessMove", RPCMode.All, "A");
		}
		//向右移动
		if (Input.GetKeyDown(KeyCode.D))
		{
			networkView.RPC("ProcessMove", RPCMode.All, "D");
		}
		if (Input.GetKey(KeyCode.D))
		{
			networkView.RPC("ProcessMove", RPCMode.All, "D");
		}
	}
}

6,给接收请求的脚本所绑定的对象,添加网络视图组件(听起来有点绕口)。我们的这个脚本是绑定在主摄像机上的,所以点击主摄像机,在菜单上选择component-->miscellaneous-->Net work View。

7,绑定脚本,把脚本拖拽到摄像机上。

8,编译发布成web的:File---->Build settings---->Build And Run,选择Web Player

9,拷贝编译出来的文件夹,到另一台机器上。

10,分别在两台机器上运行服务器端和客户端。

 

你会看到同步的效果了。

附注:在一台机器上,不太容易看到效果。因为浏览器失焦后,似乎无法刷新画面,导致同步效果不明显。

Ps. 这个代码只是unity自带的网络组件示例。在实际应用中,不能直接应用。需要用unity底层支持的C#语言,重新写一套通信框架。我已经写了一套,但是没有时间发博客。改天发出来。

 

 

 

  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值