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

目录

Setting up the contract option 1

Extending Generated Classes

Code if option 1 was selected

Scene Setup

Test

关键操作图示


In this example we are going to go over how to use the built in RPC methods inside of Forge Networking Remastered. In this example we are going to make a scene that already has a cube in it, then if anyone presses the up arrow key it will move the cube up, if anyone presses the down arrow key, it will move the cube down.

Setting up the contract option 1

In this option, we will create 2 RPC methods with no arguments. One RPC is to move the cube up and the other is to move the cube down.

NOTE: Only pick option 1 or 2 to follow

Let's begin by naming our Network Object:

  1. Let's set the name for our Network object to MoveCube
  2. Click the Add RPC button
  3. Name the new RPC MoveUp
  4. Click the Add RPC button
  5. Name the new RPC MoveDown
  6. 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 that we want to extend from, this class name will be " MoveCubeBehavior". 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 " MoveCube".

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

Code if option 1 was selected

MoveCube.cs

using UnityEngine;
using BeardedManStudios.Forge.Networking.Generated;
using BeardedManStudios.Forge.Networking;
using BeardedManStudios.Forge.Networking.Unity;
public class MoveCube : MoveCubeBehavior
{
	private void Update()
	{
		// Move the cube up in world space if the up arrow was pressed
		if (Input.GetKeyDown(KeyCode.UpArrow))
			networkObject.SendRpc(RPC_MOVE_UP, Receivers.All);
		
		// Move the cube down in world space if the down arrow was pressed
		else if (Input.GetKeyDown(KeyCode.DownArrow))
			networkObject.SendRpc(RPC_MOVE_DOWN, Receivers.All);
	}
    
	/// <summary>
	/// Used to move the cube that this script is attached to up
	/// </summary>
	/// <param name="args">null</param>
	public override void MoveUp(RpcArgs args)     
	{
		// RPC calls are not made from the main thread for performance, since we
		// are interacting with Unity engine objects, we will need to make sure
		// to run the logic on the main thread
		MainThreadManager.Run(() =>
		{
			transform.position += Vector3.up;
		});
    }
	
    /// <summary>
	/// Used to move the cube that this script is attached to down
	/// </summary>
	/// <param name="args">null</param>
	public override void MoveDown(RpcArgs args)     
	{
		// RPC calls are not made from the main thread for performance, since we
		// are interacting with Unity engine objects, we will need to make sure
		// to run the logic on the main thread
		MainThreadManager.Run(() =>
		{
			transform.position += Vector3.down;
		});
	}
}

As you can see from the code snippet above an RPC is called using the networkObject.SendRPC method. The first argument is the name of the method and the second argument is the receivers of the object which could be set to things like AllBuffered, Others, etc. The moment the RPC method is called it is sent on the network to be replicated to the other clients (including server if called from a client).

Note: _In this example, it doesn't use a buffered call and it does not actually synchronize the position, so the client should be connected before the cube is moved.

Scene Setup

设置一个能测试的unity参考

为物体添加Attach our MoveCube 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   可以不用这样做,把上面做好的unity3d工程复制一份, 一个当服务器一个当客户端
  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 instance (Unity Editor) then press the up and down arrow keys
  16. Select the client instance then press the up and down arrow keys

You will see the server movements replicated to the client and the client movements replicated to the server

关键操作图示

两个场景第一个是自带的MultiplayerMenu,第二个自己的

假设上面复制后服务器的工程为server, 客户端的是client

server和client的工程都首先运行MultiplayerMeau的场景,然后sever的运行后选择host*******,client的选择connect.client连接服务器时要输入服务器的地址和port,我测试是在同个电脑上运行的,所以是127.0.0.1

然后就可以运行了。

数据能从服务器到客户端,同时数据也能从客户端到服务器。

猜你喜欢

转载自blog.csdn.net/moonlightpeng/article/details/106948323
今日推荐