目录

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 ownerif (!networkObject.IsOwner){transform.position = networkObject.position;transform.rotation = networkObject.rotation;return;}// Let the owner move the cube around with the arrow keystransform.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 codenetworkObject.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

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

  1. STM32F103学习记录——通过额外添加串口接口来接入ZigBee/蓝牙模块实现数据的远程传输

    目录 项目场景 硬件准备 实现过程 实现缘由 原理展现 成果展现 USB-TTL模块有线数据展示 ZigBee模块远程传输数据展示 最后总结 项目场景 本人在做STM32硬件开发项目中,传输数据这一块 ...

  2. 树莓派中SIM7600G-H 4G DONGLE模块使用记录(二)使用花生壳服务器实现数据的远程传输

    项目场景: 在之前对树莓派进行PPP拨号上网/4G上网后,需要对树莓派中采集到的数据远程上传,并能接收云端软件下达的指令.然而在实现过程中,我们并不能够直接通过ITCP/IP通信实现数据的远程传输,还 ...

  3. 数据中心远程集中解决方案有哪些?

    传统IDC管理方式已不能顺应需求 传统管理方式已不能顺应当前IDC管理高度集中的需要.线缆繁多且排列复杂.分散管理.效率低下,控制室到机房受距离限制,协助困难.同时IDC物理防范的缺乏,进出机房次数繁 ...

  4. linux 内存取证_【取证流程】电子数据证据远程勘验

    原创:弘连网络 电子数据证据远程勘验在日常的取证工作中必不可少,但由于存在信息安全差.数据可能被篡改的问题. 取证过程中,有明确的取证要求来确保取证过程的规范显得至关重要,今天我们就一起来回顾下遇到远 ...

  5. Oracle备份与恢复 expdp/impdp数据泵远程导入导出

    Oracle备份与恢复 expdp/impdp数据泵远程导入导出 Oracle在10g的时候有一个很好用的导出工具expdp(数据泵) 但是这个工具好用的同时,有一个局限,就是必须用本地的用户才可以导 ...

  6. Android学习笔记---26_采用JSON格式返回数据给资讯客户端,效率上要高于xml文件解析和传输

    2013-03-22 26_采用JSON格式返回数据给资讯客户端 ---------------------------------------- 1.因为在android中,使用xml格式传输数据的 ...

  7. Android学习笔记---20_采用ContentProvider对外共享数据, UriMatcher类使用介绍 ,ContentUris类使用介绍,使用ContentResolver操作Conte

    20_采用ContentProvider对外共享数据 ------------------------------------------ 1.比如通讯录,就是通过ContentProvider,实现 ...

  8. 如何在Unity里使用OptiTrack定位数据

    如何在Unity里使用OptiTrack定位数据 一.下载软件和插件 登录Optitrack官网 http://www.optitrack.com.cn/downloads/motive.html 1 ...

  9. 使用数传电台无线远程传输雷达数据的参考方案

    本文介绍使用数传电台无线远程传输雷达数据的参考方案. 方案概述 本方案为使用数传电台方式无线远程传输北醒TF系列雷达数据的参考方案.本方案中所使用的除TF激光雷达外的设备和工具并非北醒官方产品,不提供 ...

  10. Unity中遍历大量的数据

    Unity中遍历大量的数据 如果需要在脚本中使用同样的操作去一一判别大量的数据,C#倒是提供了一种不错的机制,读者或许会有兴趣,就是"循环".C#里有3种用于实现循环的语句,分别是 ...

最新文章

  1. Java Remote Debug(远程调试)
  2. 《数据结构与抽象:Java语言描述(原书第4版)》一2.2.1 可变大小数组
  3. 字段和字段的参数,查询的13个方法,但标的双下划线外键和多对多操作
  4. in use 大学英语4word_2015年7月中国医科大学《大学英语4》在线作业答案
  5. Kotlin 继续助力 Android 开发,并计划涉足更多领域
  6. 使用生成器创建新的迭代模式
  7. 模板:线段树优化建图
  8. 167. Two Sum II - Input array is sorted (C, C++, Python)
  9. python complex 如何取出实数部分_【PYthon报错】np.complex128数字的虚数部分为0j
  10. [转载] 深度测评Python的3种“字符串格式化”方法,看看你喜欢哪一种?
  11. Visual Studio里的BUG??
  12. 《HarmonyOS开发 – 小凌派-RK2206开发笔记》第5章 使用WiFi联网
  13. C语言反汇编 - 多维数组与指针
  14. 阿里云物联平台产品、设备及物模型添加模拟数据(实操)
  15. Wrong namespace. Expected ‘com.baizhi.mapper.UserMapper‘ but found ‘com.com.baizhi.mapper.UserMappe
  16. c语言内部收益率,内部收益率 (C++代码)
  17. 如果你已经这样了,那你必须要跳槽了。
  18. B-010 详细解析电源滤波电容的选取与计算
  19. 哈密顿算子及拉普拉斯算子的基本性质及证明
  20. Linux 防火墙操作指令

热门文章

  1. 多任务计时器anytime
  2. Day_05用结构体 从内存中 读取配置信息
  3. 简述计算机网络安全的内容,网络常用的安全机制有那些,网络安全期末复习题解说.doc...
  4. python发送邮件带附件_python 发送带附件邮件
  5. android message to iphone,这款应用可以将苹果的iMessage带到安卓系统
  6. mysql中set names_深入理解mysql SET NAMES和mysql(i)_set_charset的区别
  7. java初级指令集,《Java虛擬機規范》-字節碼指令集
  8. 客户说发货慢怎么回复_给客户发完报价没回复,怎么办?
  9. Spring Cloud微服务分布式云架构源码结构
  10. 【Matlab】Plot的用法详解