Unity采用Forge Networking Remastered数据的远程传输Basic Moving Cube Example

目录

1. 关于Forge Networking Remastered的介绍

2 在unity asset store中找 Forge Networking

3, 下载导入,参数官方文档操作即可

Basic Moving Cube Example

Network Contract Wizard

Extending Generated Classes

下面这个脚本要自己重新写

Scene Setup

Test

操作关键图示


1. 关于Forge Networking Remastered的介绍

https://github.com/BeardedManStudios/ForgeNetworkingRemastered/wiki

2 在unity asset store中找 Forge Networking

3, 下载导入,参数官方文档操作即可

Basic Moving Cube Example

https://github.com/BeardedManStudios/ForgeNetworkingRemastered/wiki/Basic-Moving-Cube-Example

In this example, we are going to make a simple game where we have a cube in the scene that is owned by the server and all the clients can see it being moved by the server.

只能实现服务器向客户端数据传输

Network Contract Wizard

Now that we know that we need to sync the position and rotation of a cube, we can design our network contract for that object. We will first open the Network Contract Wizard which is a UI provided by the Bearded Man Studios team to make it easy to design your network contracts in an easy way. To open this menu, go into Unity and select "Window->Forge Networking->Network Contract Wizard".

 

Once you have opened this editor you will be presented with a list of all the Network Objects currently available, to learn more about this please see the document on the Network Contract Wizard as we will just be going over how to create a network object through the contract wizard. To start, click on the "Create" button near the top and you will be presented with the create UI. In here, we have 3 major fields of interest, the Name fields, the Fields field, and the Remote Procedure Calls field.

  1. The Name field is where we create the name for our Network Object and behavior, this is a friendly name that should be written in "Pascal case" to follow the C# convention since it is going to be a part of the class names that are generated.
  2. The Fields section shows all of the various fields that our network should be aware of. In this case, we are going to want to make a position and rotation field which are Vector3 and Quaternion respectively. Note, you can name these fields whatever you like, these are just friendly variable names for you (and your team) to know what they are for when used
  3. The Remote Procedure Calls field is where you will design any Remote Procedure Call (RPC) function signatures. We are not going to go over this field in this tutorial as we do not need it for the goal we are attempting to accomplish.

Let's begin by naming our Network Object:

  1. Let's set the name for our Network object to BasicCube
  2. Click the Add Field button
  3. Name the new field position
  4. Set the type to Vector3
  5. Click the Interpolate button
  6. Set the interpolate time (the text field that pops up after clicking the Interpolate button) as 0.15
  7. Click the Add Field button
  8. Name the new field rotation
  9. Set the type to Quaternion
  10. Click the Interpolate button
  11. Set the interpolate time (the text field that pops up after clicking the Interpolate button) as 0.15
  12. Click the Save & Compile button

Extending Generated Classes

When we use the Network Contract Wizard (NCW) we are actually generating a lot of network code based on what has been input into the editor fields, this actually cuts out a lot of work that you would have to do by hand. There is one class in particular that we want to extend from, this class name will be BasicCubeBehavior. The naming convention for this generated class is _____Behavior where "_____" is the name we typed into the NCW. Let's now create a C# file in Unity and write our basic game logic, we will name this file BasicCube.

  1. Open the newly created C# file
  2. Add using BeardedManStudios.Forge.Networking.Generated; to the using statements
  3. Derive the class from BasicCubeBehavior
  4. Write the rest of the logic for the cube as seen below

下面这个脚本要自己重新写

BasicCube.cs

using UnityEngine;

using BeardedManStudios.Forge.Networking.Generated;
public class BasicCube : BasicCubeBehavior
{
	/// <summary>
	/// The speed that the cube will move by when the user presses a
	/// Horizontal or Vertical mapped key
	/// </summary>
	public float speed = 5.0f;

	private void Update()
	{
		// If unity's Update() runs, before the object is
		// instantiated in the network, then simply don't
		// continue, otherwise a bug/error will happen.
		// 
		// Unity's Update() running, before this object is instantiated
		// on the network is **very** rare, but better be safe 100%
		if (networkObject == null)
			return;
		
		// If we are not the owner of this network object then we should
		// move this cube to the position/rotation dictated by the owner
		if (!networkObject.IsOwner)
		{
			transform.position = networkObject.position;
			transform.rotation = networkObject.rotation;
			return;
		}

		// Let the owner move the cube around with the arrow keys
		transform.position += new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")).normalized * speed * Time.deltaTime;

		// If we are the owner of the object we should send the new position
		// and rotation across the network for receivers to move to in the above code
		networkObject.position = transform.position;
		networkObject.rotation = transform.rotation;

		// Note: Forge Networking takes care of only sending the delta, so there
		// is no need for you to do that manually
	}
}

 

networkObject 连接成功后可以运行程序否则退出

As you can see from the code snippet above, you can determine if the current player is the owner of the object using the networkObject.IsOwner boolean comparison. This will allow you to make code specifically based on the owner of the object. In this case, since the cube is going to be in the scene at start, it's owner is the server. In the snippet above the client (non owner) will update the transform position and rotation of the cube (the object this script is going to be attached to) to the position and rotation received from the server. Since we turned on interpolation, all of the smoothing is done "behind the scenes". Now the server in this case will just assign the position and rotation variables of the networkObject. These are the two fields we created in the NCW by the way. All generated network objects from the NCW will have a networkObject member variable that you can access from the deriving child. Whenever you assign a field of this object it is replicated across the network if the assigning user is the owner of the object.

Scene Setup

新建场景,并将

  1. Attach our BasicCube script to this cube

Test

Now that we have setup our scene and everything else, it is time to test the game.

  1. Open the Build Settings
  2. Click on Player Settings...
  3. Open the Resolution and Presentation section
  4. Turn on Run In Background*
  5. Go back to Build Settings
  6. Click on Build And Run  这一步可以不做,直接把unity工程复制一份,一个server一个client
  7. Once the game is open, return to the Unity Editor
  8. Open the MultiplayerMenu scene
  9. Click the play button
  10. Click the Host (127.0.0.1:15937) button on the bottom of the game view
  11. Go back to the built game
  12. Make sure the host ip address is set to 127.0.0.1
  13. Make sure the host port is set to 15937
  14. Click the Connect button
  15. Select the server game instance (Unity Editor)

Now if you move around the cube in the editor, you will see the movements replicated to the clients.

操作关键图示

MultiplayerMenu是自带的

只运行MultiplayerMenu这个场景,2单击表示这个是服务器,运行后

物体开始运动

 

客户端则操作连接服务器

单击connect后数据就传过来了。

如果要多加一个client,再复制一份程序运行ip和port用服务器的就可以了。

这个只能完成从服务器到客户端的数据传输,不能从客户端到服务器。

要解决这个问题参考

Basic RPC Example

https://github.com/BeardedManStudios/ForgeNetworkingRemastered/wiki/Basic-RPC-Example

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值