目录
Setting up the contract option 1
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:
- Let's set the name for our Network object to MoveCube
- Click the Add RPC button
- Name the new RPC MoveUp
- Click the Add RPC button
- Name the new RPC MoveDown
- 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".
- Open the newly created C# file
- Add using BeardedManStudios.Forge.Networking.Generated; to the using statements
- Derive the class from MoveCubeBehavior
- 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.
- Open the Build Settings
- Click on Player Settings...
- Open the Resolution and Presentation section
- Turn on Run In Background*
- Go back to Build Settings
- Click on Build And Run 可以不用这样做,把上面做好的unity3d工程复制一份, 一个当服务器一个当客户端
- Once the game is open, return to the Unity Editor
- Open the MultiplayerMenu scene
- Click the play button
- Click the Host (127.0.0.1:15937) button on the bottom of the game view
- Go back to the built game
- Make sure the host ip address is set to 127.0.0.1
- Make sure the host port is set to 15937
- Click the Connect button
- Select the server instance (Unity Editor) then press the up and down arrow keys
- 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
然后就可以运行了。
数据能从服务器到客户端,同时数据也能从客户端到服务器。
来源:oschina
链接:https://my.oschina.net/u/4403370/blog/4322120