Unity采用Forge Networking Remastered数据的远程传输 Basic RPC Example
目录
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
然后就可以运行了。
数据能从服务器到客户端,同时数据也能从客户端到服务器。
相关文章
- C#TextBox控件实现数据绑定的方法
- 【远程医疗】互联网医院 卫健委数据上报平台技术方案
- 超过三分之一的员工会出卖公司数据
- 【项目精选】基于Vue + ECharts的数据可视化系统的设计与实现(论文+源码+视频)
- 把本地仓库数据push到github远程仓库
- 快速构建大数据缓存
- 新一代可级联的以太网远程I/O数据采集模块
- 金鸽工业以太网远程I/O数据采集模块 (产品系列:MxxxT)
- Spark视频王家林大神第8课:彻底理解大数据机器学习
- 大数据 IMF 传奇 困扰很久的问题解决!ecliplse 远程提交程序到虚拟机 hadoop集群 ,ecliplse 没有显示输出 的问题解决!
- 大数据Spark “蘑菇云”行动前传第8课:零基础实战Scala最常用数据结构Map和Tuple及Spark源码解析
- [系统]win10远程桌面其他电脑出现如下错误,由于数据加密错误,这个会话讲结束,请重新连接到远程计算机
- 证券业数据大集中及其风险控制分析
- [CoreOS 转载] CoreOS实践指南(五):分布式数据存储Etcd(上)
- 大数据面试题(一):HDFS核心高频面试题
- 客快物流大数据项目(一百一十六):远程调用 Spring Cloud Feign
- 大数据必学Java基础(五十三):Colletion接口
- 经典面试问题: Top K 之 ---- 海量数据找出现次数最多或,不重复的。